Add edit_add_window() function.
[midnight-commander.git] / src / editor / editcmd.c
blob13fbfad52dfcad386484bb3cd92e9a38dd5906fa
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/charsets.h"
64 #include "lib/event.h" /* mc_event_raise() */
66 #include "src/history.h"
67 #include "src/setup.h" /* option_tab_spacing */
68 #include "src/main.h" /* mactos_t */
69 #include "src/selcodepage.h"
70 #include "src/keybind-defaults.h"
71 #include "src/util.h" /* check_for_default() */
72 #include "src/filemanager/layout.h" /* mc_refresh() */
74 #include "edit-impl.h"
75 #include "editwidget.h"
76 #include "editcmd_dialogs.h"
77 #include "etags.h"
79 /*** global variables ****************************************************************************/
81 /* search and replace: */
82 int search_create_bookmark = FALSE;
84 /* queries on a save */
85 int edit_confirm_save = 1;
87 /*** file scope macro definitions ****************************************************************/
89 #define space_width 1
91 #define TEMP_BUF_LEN 1024
93 #define INPUT_INDEX 9
95 /* thanks to Liviu Daia <daia@stoilow.imar.ro> for getting this
96 (and the above) routines to work properly - paul */
98 #define is_digit(x) ((x) >= '0' && (x) <= '9')
100 #define MAIL_DLG_HEIGHT 12
102 #define MAX_WORD_COMPLETIONS 100 /* in listbox */
104 /*** file scope type declarations ****************************************************************/
106 /*** file scope variables ************************************************************************/
108 /*** file scope functions ************************************************************************/
109 /* --------------------------------------------------------------------------------------------- */
111 /* If 0 (quick save) then a) create/truncate <filename> file,
112 b) save to <filename>;
113 if 1 (safe save) then a) save to <tempnam>,
114 b) rename <tempnam> to <filename>;
115 if 2 (do backups) then a) save to <tempnam>,
116 b) rename <filename> to <filename.backup_ext>,
117 c) rename <tempnam> to <filename>. */
119 /* returns 0 on error, -1 on abort */
121 static int
122 edit_save_file (WEdit * edit, const vfs_path_t * filename_vpath)
124 char *p;
125 gchar *tmp;
126 long filelen = 0;
127 int this_save_mode, fd = -1;
128 vfs_path_t *real_filename_vpath;
129 vfs_path_t *savename_vpath = NULL;
130 const char *start_filename;
131 const vfs_path_element_t *vpath_element;
133 vpath_element = vfs_path_get_by_index (filename_vpath, 0);
134 if (vpath_element == NULL)
135 return 0;
137 start_filename = vpath_element->path;
138 if (*start_filename == '\0')
139 return 0;
141 if (*start_filename != PATH_SEP && edit->dir_vpath != NULL)
143 real_filename_vpath = vfs_path_append_vpath_new (edit->dir_vpath, filename_vpath, NULL);
145 else
147 real_filename_vpath = vfs_path_clone (filename_vpath);
150 this_save_mode = option_save_mode;
151 if (this_save_mode != EDIT_QUICK_SAVE)
153 if (!vfs_file_is_local (real_filename_vpath)
154 || (fd = mc_open (real_filename_vpath, O_RDONLY | O_BINARY)) == -1)
157 * The file does not exists yet, so no safe save or
158 * backup are necessary.
160 this_save_mode = EDIT_QUICK_SAVE;
162 if (fd != -1)
163 mc_close (fd);
166 if (this_save_mode == EDIT_QUICK_SAVE && !edit->skip_detach_prompt)
168 int rv;
169 struct stat sb;
171 rv = mc_stat (real_filename_vpath, &sb);
172 if (rv == 0 && sb.st_nlink > 1)
174 rv = edit_query_dialog3 (_("Warning"),
175 _("File has hard-links. Detach before saving?"),
176 _("&Yes"), _("&No"), _("&Cancel"));
177 switch (rv)
179 case 0:
180 this_save_mode = EDIT_SAFE_SAVE;
181 /* fallthrough */
182 case 1:
183 edit->skip_detach_prompt = 1;
184 break;
185 default:
186 vfs_path_free (real_filename_vpath);
187 return -1;
191 /* Prevent overwriting changes from other editor sessions. */
192 if (rv == 0 && edit->stat1.st_mtime != 0 && edit->stat1.st_mtime != sb.st_mtime)
194 /* The default action is "Cancel". */
195 query_set_sel (1);
197 rv = edit_query_dialog2 (_("Warning"),
198 _("The file has been modified in the meantime. Save anyway?"),
199 _("&Yes"), _("&Cancel"));
200 if (rv != 0)
202 vfs_path_free (real_filename_vpath);
203 return -1;
208 if (this_save_mode != EDIT_QUICK_SAVE)
210 char *savedir, *saveprefix;
212 savedir = vfs_path_tokens_get (real_filename_vpath, 0, -1);
213 if (savedir == NULL)
214 savedir = g_strdup (".");
216 saveprefix = mc_build_filename (savedir, "cooledit", NULL);
217 g_free (savedir);
218 fd = mc_mkstemps (&savename_vpath, saveprefix, NULL);
219 g_free (saveprefix);
220 if (savename_vpath == NULL)
222 vfs_path_free (real_filename_vpath);
223 return 0;
225 /* FIXME:
226 * Close for now because mc_mkstemps use pure open system call
227 * to create temporary file and it needs to be reopened by
228 * VFS-aware mc_open().
230 close (fd);
232 else
233 savename_vpath = vfs_path_clone (real_filename_vpath);
235 (void) mc_chown (savename_vpath, edit->stat1.st_uid, edit->stat1.st_gid);
236 (void) mc_chmod (savename_vpath, edit->stat1.st_mode);
238 fd = mc_open (savename_vpath, O_CREAT | O_WRONLY | O_TRUNC | O_BINARY, edit->stat1.st_mode);
239 if (fd == -1)
240 goto error_save;
242 /* pipe save */
243 p = edit_get_write_filter (savename_vpath, real_filename_vpath);
244 if (p != NULL)
246 FILE *file;
248 mc_close (fd);
249 file = (FILE *) popen (p, "w");
251 if (file)
253 filelen = edit_write_stream (edit, file);
254 #if 1
255 pclose (file);
256 #else
257 if (pclose (file) != 0)
259 tmp = g_strdup_printf (_("Error writing to pipe: %s"), p);
260 edit_error_dialog (_("Error"), tmp);
261 g_free (tmp);
262 g_free (p);
263 goto error_save;
265 #endif
267 else
269 tmp = g_strdup_printf (_("Cannot open pipe for writing: %s"), p);
270 edit_error_dialog (_("Error"), get_sys_error (tmp));
271 g_free (p);
272 g_free (tmp);
273 goto error_save;
275 g_free (p);
277 else if (edit->lb == LB_ASIS)
278 { /* do not change line breaks */
279 long buf;
280 buf = 0;
281 filelen = edit->last_byte;
282 while (buf <= (edit->curs1 >> S_EDIT_BUF_SIZE) - 1)
284 if (mc_write (fd, (char *) edit->buffers1[buf], EDIT_BUF_SIZE) != EDIT_BUF_SIZE)
286 mc_close (fd);
287 goto error_save;
289 buf++;
291 if (mc_write
292 (fd, (char *) edit->buffers1[buf],
293 edit->curs1 & M_EDIT_BUF_SIZE) != (edit->curs1 & M_EDIT_BUF_SIZE))
295 filelen = -1;
297 else if (edit->curs2)
299 edit->curs2--;
300 buf = (edit->curs2 >> S_EDIT_BUF_SIZE);
301 if (mc_write
302 (fd,
303 (char *) edit->buffers2[buf] + EDIT_BUF_SIZE -
304 (edit->curs2 & M_EDIT_BUF_SIZE) - 1,
305 1 + (edit->curs2 & M_EDIT_BUF_SIZE)) != 1 + (edit->curs2 & M_EDIT_BUF_SIZE))
307 filelen = -1;
309 else
311 while (--buf >= 0)
313 if (mc_write (fd, (char *) edit->buffers2[buf], EDIT_BUF_SIZE) != EDIT_BUF_SIZE)
315 filelen = -1;
316 break;
320 edit->curs2++;
322 if (mc_close (fd))
323 goto error_save;
325 /* Update the file information, especially the mtime. */
326 if (mc_stat (savename_vpath, &edit->stat1) == -1)
327 goto error_save;
329 else
330 { /* change line breaks */
331 FILE *file;
332 const vfs_path_element_t *path_element;
334 mc_close (fd);
336 path_element = vfs_path_get_by_index (savename_vpath, -1);
337 file = (FILE *) fopen (path_element->path, "w");
338 if (file != NULL)
340 filelen = edit_write_stream (edit, file);
341 fclose (file);
343 else
345 char *msg;
347 msg = g_strdup_printf (_("Cannot open file for writing: %s"), path_element->path);
348 edit_error_dialog (_("Error"), msg);
349 g_free (msg);
350 goto error_save;
354 if (filelen != edit->last_byte)
355 goto error_save;
357 if (this_save_mode == EDIT_DO_BACKUP)
359 vfs_path_t *tmp_vpath;
360 gboolean ok;
362 #ifdef HAVE_ASSERT_H
363 assert (option_backup_ext != NULL);
364 #endif
365 tmp_vpath = vfs_path_append_new (real_filename_vpath, option_backup_ext, (char *) NULL);
366 ok = (mc_rename (real_filename_vpath, tmp_vpath) != -1);
367 vfs_path_free (tmp_vpath);
368 if (!ok)
369 goto error_save;
372 if (this_save_mode != EDIT_QUICK_SAVE)
373 if (mc_rename (savename_vpath, real_filename_vpath) == -1)
374 goto error_save;
376 vfs_path_free (real_filename_vpath);
377 vfs_path_free (savename_vpath);
378 return 1;
379 error_save:
380 /* FIXME: Is this safe ?
381 * if (this_save_mode != EDIT_QUICK_SAVE)
382 * mc_unlink (savename);
384 vfs_path_free (real_filename_vpath);
385 vfs_path_free (savename_vpath);
386 return 0;
389 /* --------------------------------------------------------------------------------------------- */
391 static gboolean
392 edit_check_newline (WEdit * edit)
394 return !(option_check_nl_at_eof && edit->last_byte > 0
395 && edit_get_byte (edit, edit->last_byte - 1) != '\n'
396 && edit_query_dialog2 (_("Warning"),
397 _("The file you are saving is not finished with a newline"),
398 _("C&ontinue"), _("&Cancel")));
401 /* --------------------------------------------------------------------------------------------- */
403 static vfs_path_t *
404 edit_get_save_file_as (WEdit * edit)
406 #define DLG_WIDTH 64
407 #define DLG_HEIGHT 14
409 static LineBreaks cur_lb = LB_ASIS;
411 char *filename = vfs_path_to_str (edit->filename_vpath);
412 char *filename_res;
414 const char *lb_names[LB_NAMES] = {
415 N_("&Do not change"),
416 N_("&Unix format (LF)"),
417 N_("&Windows/DOS format (CR LF)"),
418 N_("&Macintosh format (CR)")
421 QuickWidget quick_widgets[] = {
422 QUICK_BUTTON (6, 10, DLG_HEIGHT - 3, DLG_HEIGHT, N_("&Cancel"), B_CANCEL, NULL),
423 QUICK_BUTTON (2, 10, DLG_HEIGHT - 3, DLG_HEIGHT, N_("&OK"), B_ENTER, NULL),
424 QUICK_RADIO (5, DLG_WIDTH, DLG_HEIGHT - 8, DLG_HEIGHT, LB_NAMES, lb_names, (int *) &cur_lb),
425 QUICK_LABEL (3, DLG_WIDTH, DLG_HEIGHT - 9, DLG_HEIGHT, N_("Change line breaks to:")),
426 QUICK_INPUT (3, DLG_WIDTH, DLG_HEIGHT - 11, DLG_HEIGHT, filename, DLG_WIDTH - 6, 0,
427 "save-as", &filename_res),
428 QUICK_LABEL (3, DLG_WIDTH, DLG_HEIGHT - 12, DLG_HEIGHT, N_("Enter file name:")),
429 QUICK_END
432 QuickDialog Quick_options = {
433 DLG_WIDTH, DLG_HEIGHT, -1, -1,
434 N_("Save As"), "[Save File As]",
435 quick_widgets, NULL, NULL, FALSE
438 if (quick_dialog (&Quick_options) != B_CANCEL)
440 char *fname;
441 vfs_path_t *ret_vpath;
443 edit->lb = cur_lb;
444 fname = tilde_expand (filename_res);
445 g_free (filename_res);
446 ret_vpath = vfs_path_from_str (fname);
447 g_free (fname);
448 return ret_vpath;
450 g_free (filename);
452 return NULL;
454 #undef DLG_WIDTH
455 #undef DLG_HEIGHT
458 /* --------------------------------------------------------------------------------------------- */
460 /** returns 1 on success */
462 static int
463 edit_save_cmd (WEdit * edit)
465 int res, save_lock = 0;
467 if (!edit->locked && !edit->delete_file)
468 save_lock = lock_file (edit->filename_vpath);
469 res = edit_save_file (edit, edit->filename_vpath);
471 /* Maintain modify (not save) lock on failure */
472 if ((res > 0 && edit->locked) || save_lock)
473 edit->locked = unlock_file (edit->filename_vpath);
475 /* On failure try 'save as', it does locking on its own */
476 if (!res)
477 return edit_save_as_cmd (edit);
478 edit->force |= REDRAW_COMPLETELY;
479 if (res > 0)
481 edit->delete_file = 0;
482 edit->modified = 0;
485 return 1;
488 /* --------------------------------------------------------------------------------------------- */
490 * Load file content
492 * @param edit widget object
493 * @param exp_vpath vfs file path
494 * @return TRUE if file content was successfully loaded, FALSE otherwise
497 static gboolean
498 edit_load_file_from_filename (WEdit * edit, const vfs_path_t * exp_vpath)
500 int prev_locked = edit->locked;
501 vfs_path_t *prev_filename;
502 gboolean ret = TRUE;
504 prev_filename = vfs_path_clone (edit->filename_vpath);
505 if (!edit_reload (edit, exp_vpath))
506 ret = FALSE;
507 else if (prev_locked)
508 unlock_file (prev_filename);
510 vfs_path_free (prev_filename);
511 return ret;
514 /* --------------------------------------------------------------------------------------------- */
516 static void
517 edit_load_syntax_file (WEdit * edit)
519 vfs_path_t *extdir_vpath;
520 int dir = 0;
522 if (geteuid () == 0)
524 dir = query_dialog (_("Syntax file edit"),
525 _("Which syntax file you want to edit?"), D_NORMAL, 2,
526 _("&User"), _("&System Wide"));
529 extdir_vpath =
530 vfs_path_build_filename (mc_global.sysconfig_dir, "syntax", "Syntax", (char *) NULL);
531 if (!exist_file (vfs_path_get_last_path_str (extdir_vpath)))
533 vfs_path_free (extdir_vpath);
534 extdir_vpath =
535 vfs_path_build_filename (mc_global.share_data_dir, "syntax", "Syntax", (char *) NULL);
538 if (dir == 0)
540 vfs_path_t *user_syntax_file_vpath;
542 user_syntax_file_vpath = mc_config_get_full_vpath (EDIT_SYNTAX_FILE);
543 check_for_default (extdir_vpath, user_syntax_file_vpath);
544 edit_load_file_from_filename (edit, user_syntax_file_vpath);
545 vfs_path_free (user_syntax_file_vpath);
547 else if (dir == 1)
548 edit_load_file_from_filename (edit, extdir_vpath);
550 vfs_path_free (extdir_vpath);
553 /* --------------------------------------------------------------------------------------------- */
555 static void
556 edit_load_menu_file (WEdit * edit)
558 vfs_path_t *buffer_vpath;
559 vfs_path_t *menufile_vpath;
560 int dir = 0;
562 dir = query_dialog (_("Menu edit"),
563 _("Which menu file do you want to edit?"), D_NORMAL,
564 geteuid () != 0 ? 2 : 3, _("&Local"), _("&User"), _("&System Wide"));
566 menufile_vpath = vfs_path_build_filename (mc_global.sysconfig_dir, EDIT_GLOBAL_MENU, NULL);
568 if (!exist_file (vfs_path_get_last_path_str (menufile_vpath)))
570 vfs_path_free (menufile_vpath);
571 menufile_vpath = vfs_path_build_filename (mc_global.share_data_dir, EDIT_GLOBAL_MENU, NULL);
574 switch (dir)
576 case 0:
577 buffer_vpath = vfs_path_from_str (EDIT_LOCAL_MENU);
578 check_for_default (menufile_vpath, buffer_vpath);
579 chmod (vfs_path_get_last_path_str (buffer_vpath), 0600);
580 break;
582 case 1:
583 buffer_vpath = mc_config_get_full_vpath (EDIT_HOME_MENU);
584 check_for_default (menufile_vpath, buffer_vpath);
585 break;
587 case 2:
588 buffer_vpath = vfs_path_build_filename (mc_global.sysconfig_dir, EDIT_GLOBAL_MENU, NULL);
589 if (!exist_file (vfs_path_get_last_path_str (buffer_vpath)))
591 vfs_path_free (buffer_vpath);
592 buffer_vpath =
593 vfs_path_build_filename (mc_global.share_data_dir, EDIT_GLOBAL_MENU, NULL);
595 break;
597 default:
598 vfs_path_free (menufile_vpath);
599 return;
602 edit_load_file_from_filename (edit, buffer_vpath);
604 vfs_path_free (buffer_vpath);
605 vfs_path_free (menufile_vpath);
608 /* --------------------------------------------------------------------------------------------- */
610 static void
611 edit_delete_column_of_text (WEdit * edit)
613 long p, q, r, m1, m2;
614 long b, c, d, n;
616 eval_marks (edit, &m1, &m2);
617 n = edit_move_forward (edit, m1, 0, m2) + 1;
618 c = edit_move_forward3 (edit, edit_bol (edit, m1), 0, m1);
619 d = edit_move_forward3 (edit, edit_bol (edit, m2), 0, m2);
620 b = max (min (c, d), min (edit->column1, edit->column2));
621 c = max (c, max (edit->column1, edit->column2));
623 while (n--)
625 r = edit_bol (edit, edit->curs1);
626 p = edit_move_forward3 (edit, r, b, 0);
627 q = edit_move_forward3 (edit, r, c, 0);
628 if (p < m1)
629 p = m1;
630 if (q > m2)
631 q = m2;
632 edit_cursor_move (edit, p - edit->curs1);
633 while (q > p)
635 /* delete line between margins */
636 if (edit_get_byte (edit, edit->curs1) != '\n')
637 edit_delete (edit, 1);
638 q--;
640 if (n)
641 /* move to next line except on the last delete */
642 edit_cursor_move (edit, edit_move_forward (edit, edit->curs1, 1, 0) - edit->curs1);
646 /* --------------------------------------------------------------------------------------------- */
647 /** if success return 0 */
649 static int
650 edit_block_delete (WEdit * edit)
652 long count;
653 long start_mark, end_mark;
654 int curs_pos, line_width;
655 long curs_line, c1, c2;
657 if (eval_marks (edit, &start_mark, &end_mark))
658 return 0;
659 if (edit->column_highlight && edit->mark2 < 0)
660 edit_mark_cmd (edit, 0);
661 if ((end_mark - start_mark) > option_max_undo / 2)
663 /* Warning message with a query to continue or cancel the operation */
664 if (edit_query_dialog2
665 (_("Warning"),
667 ("Block is large, you may not be able to undo this action"),
668 _("C&ontinue"), _("&Cancel")))
670 return 1;
673 c1 = min (edit->column1, edit->column2);
674 c2 = max (edit->column1, edit->column2);
675 edit->column1 = c1;
676 edit->column2 = c2;
678 edit_push_markers (edit);
680 curs_line = edit->curs_line;
682 /* calculate line width and cursor position before cut */
683 line_width = edit_move_forward3 (edit, edit_bol (edit, edit->curs1), 0,
684 edit_eol (edit, edit->curs1));
685 curs_pos = edit->curs_col + edit->over_col;
687 /* move cursor to start of selection */
688 edit_cursor_move (edit, start_mark - edit->curs1);
689 edit_scroll_screen_over_cursor (edit);
690 count = start_mark;
691 if (start_mark < end_mark)
693 if (edit->column_highlight)
695 if (edit->mark2 < 0)
696 edit_mark_cmd (edit, 0);
697 edit_delete_column_of_text (edit);
698 /* move cursor to the saved position */
699 edit_move_to_line (edit, curs_line);
700 /* calculate line width after cut */
701 line_width = edit_move_forward3 (edit, edit_bol (edit, edit->curs1), 0,
702 edit_eol (edit, edit->curs1));
703 if (option_cursor_beyond_eol && curs_pos > line_width)
704 edit->over_col = curs_pos - line_width;
706 else
708 while (count < end_mark)
710 edit_delete (edit, 1);
711 count++;
715 edit_set_markers (edit, 0, 0, 0, 0);
716 edit->force |= REDRAW_PAGE;
717 return 0;
720 /* --------------------------------------------------------------------------------------------- */
722 * Get EOL symbol for searching.
724 * @param edit editor object
725 * @return EOL symbol
728 static inline char
729 edit_search_get_current_end_line_char (const WEdit * edit)
731 switch (edit->lb)
733 case LB_MAC:
734 return '\r';
735 default:
736 return '\n';
740 /* --------------------------------------------------------------------------------------------- */
742 * Checking if search condition have BOL(^) or EOL ($) regexp special characters.
744 * @param search search object
745 * @return result of checks.
748 static edit_search_line_t
749 edit_get_search_line_type (mc_search_t * search)
751 edit_search_line_t search_line_type = 0;
753 if (search->search_type != MC_SEARCH_T_REGEX)
754 return search_line_type;
756 if (*search->original == '^')
757 search_line_type |= AT_START_LINE;
759 if (search->original[search->original_len - 1] == '$')
760 search_line_type |= AT_END_LINE;
761 return search_line_type;
764 /* --------------------------------------------------------------------------------------------- */
766 * Calculating the start position of next line.
768 * @param edit editor object
769 * @param current_pos current position
770 * @param max_pos max position
771 * @param end_string_symbol end of line symbol
772 * @return start position of next line
775 static off_t
776 edit_calculate_start_of_next_line (WEdit * edit, off_t current_pos, off_t max_pos,
777 char end_string_symbol)
779 off_t i;
781 for (i = current_pos; i < max_pos; i++)
783 current_pos++;
784 if (edit_get_byte (edit, i) == end_string_symbol)
785 break;
788 return current_pos;
791 /* --------------------------------------------------------------------------------------------- */
793 * Calculating the end position of previous line.
795 * @param edit editor object
796 * @param current_pos current position
797 * @param end_string_symbol end of line symbol
798 * @return end position of previous line
801 static off_t
802 edit_calculate_end_of_previous_line (WEdit * edit, off_t current_pos, char end_string_symbol)
804 off_t i;
806 for (i = current_pos - 1; i >= 0; i--)
807 if (edit_get_byte (edit, i) == end_string_symbol)
808 break;
810 return i;
813 /* --------------------------------------------------------------------------------------------- */
815 * Calculating the start position of previous line.
817 * @param edit editor object
818 * @param current_pos current position
819 * @param end_string_symbol end of line symbol
820 * @return start position of previous line
823 static inline off_t
824 edit_calculate_start_of_previous_line (WEdit * edit, off_t current_pos, char end_string_symbol)
826 current_pos = edit_calculate_end_of_previous_line (edit, current_pos, end_string_symbol);
827 current_pos = edit_calculate_end_of_previous_line (edit, current_pos, end_string_symbol);
829 return (current_pos + 1);
832 /* --------------------------------------------------------------------------------------------- */
834 * Calculating the start position of current line.
836 * @param edit editor object
837 * @param current_pos current position
838 * @param end_string_symbol end of line symbol
839 * @return start position of current line
842 static inline off_t
843 edit_calculate_start_of_current_line (WEdit * edit, off_t current_pos, char end_string_symbol)
845 current_pos = edit_calculate_end_of_previous_line (edit, current_pos, end_string_symbol);
847 return (current_pos + 1);
850 /* --------------------------------------------------------------------------------------------- */
852 * Fixing (if needed) search start position if 'only in selection' option present.
854 * @param edit editor object
857 static void
858 edit_search_fix_search_start_if_selection (WEdit * edit)
860 long start_mark = 0;
861 long end_mark = 0;
863 if (!edit_search_options.only_in_selection)
864 return;
866 if (eval_marks (edit, &start_mark, &end_mark) != 0)
867 return;
869 if (edit_search_options.backwards)
871 if (edit->search_start > end_mark || edit->search_start <= start_mark)
872 edit->search_start = end_mark;
874 else
876 if (edit->search_start < start_mark || edit->search_start >= end_mark)
877 edit->search_start = start_mark;
881 /* --------------------------------------------------------------------------------------------- */
883 static gboolean
884 editcmd_find (WEdit * edit, gsize * len)
886 off_t search_start = edit->search_start;
887 off_t search_end;
888 long start_mark = 0;
889 long end_mark = edit->last_byte;
890 int mark_res = 0;
891 char end_string_symbol;
893 end_string_symbol = edit_search_get_current_end_line_char (edit);
895 /* prepare for search */
896 if (edit_search_options.only_in_selection)
898 mark_res = eval_marks (edit, &start_mark, &end_mark);
899 if (mark_res != 0)
901 edit->search->error = MC_SEARCH_E_NOTFOUND;
902 edit->search->error_str = g_strdup (_("Search string not found"));
903 return FALSE;
906 /* fix the start and the end of search block positions */
907 if ((edit->search_line_type & AT_START_LINE) != 0
908 && (start_mark != 0 || edit_get_byte (edit, start_mark - 1) != end_string_symbol))
910 start_mark =
911 edit_calculate_start_of_next_line (edit, start_mark, edit->last_byte,
912 end_string_symbol);
914 if ((edit->search_line_type & AT_END_LINE) != 0
915 && (end_mark - 1 != edit->last_byte
916 || edit_get_byte (edit, end_mark) != end_string_symbol))
918 end_mark = edit_calculate_end_of_previous_line (edit, end_mark, end_string_symbol);
920 if (start_mark >= end_mark)
922 edit->search->error = MC_SEARCH_E_NOTFOUND;
923 edit->search->error_str = g_strdup (_("Search string not found"));
924 return FALSE;
927 else
929 if (edit_search_options.backwards)
930 end_mark = max (1, edit->curs1) - 1;
933 /* search */
934 if (edit_search_options.backwards)
936 /* backward search */
937 search_end = end_mark;
939 if ((edit->search_line_type & AT_START_LINE) != 0)
940 search_start =
941 edit_calculate_start_of_current_line (edit, search_start, end_string_symbol);
943 while ((int) search_start >= start_mark)
945 if (search_end > (off_t) (search_start + edit->search->original_len)
946 && mc_search_is_fixed_search_str (edit->search))
948 search_end = search_start + edit->search->original_len;
950 if (mc_search_run (edit->search, (void *) edit, search_start, search_end, len)
951 && edit->search->normal_offset == search_start)
953 return TRUE;
957 if ((edit->search_line_type & AT_START_LINE) != 0)
958 search_start =
959 edit_calculate_start_of_previous_line (edit, search_start, end_string_symbol);
960 else
961 search_start--;
963 edit->search->error_str = g_strdup (_("Search string not found"));
965 else
967 /* forward search */
968 if ((edit->search_line_type & AT_START_LINE) != 0 && search_start != start_mark)
969 search_start =
970 edit_calculate_start_of_next_line (edit, search_start, end_mark, end_string_symbol);
971 return mc_search_run (edit->search, (void *) edit, search_start, end_mark, len);
973 return FALSE;
976 /* --------------------------------------------------------------------------------------------- */
978 static char *
979 edit_replace_cmd__conv_to_display (char *str)
981 #ifdef HAVE_CHARSET
982 GString *tmp;
984 tmp = str_convert_to_display (str);
985 if (tmp != NULL)
987 if (tmp->len != 0)
988 return g_string_free (tmp, FALSE);
989 g_string_free (tmp, TRUE);
991 #endif
992 return g_strdup (str);
995 /* --------------------------------------------------------------------------------------------- */
997 static char *
998 edit_replace_cmd__conv_to_input (char *str)
1000 #ifdef HAVE_CHARSET
1001 GString *tmp;
1003 tmp = str_convert_to_input (str);
1004 if (tmp != NULL)
1006 if (tmp->len != 0)
1007 return g_string_free (tmp, FALSE);
1008 g_string_free (tmp, TRUE);
1010 #endif
1011 return g_strdup (str);
1014 /* --------------------------------------------------------------------------------------------- */
1016 static void
1017 edit_do_search (WEdit * edit)
1019 gsize len = 0;
1021 if (edit->search == NULL)
1022 edit->search_start = edit->curs1;
1024 edit_push_undo_action (edit, KEY_PRESS + edit->start_display);
1026 if (search_create_bookmark)
1028 int found = 0, books = 0;
1029 long l = 0, l_last = -1;
1030 long q = 0;
1032 search_create_bookmark = FALSE;
1033 book_mark_flush (edit, -1);
1035 while (TRUE)
1037 if (!mc_search_run (edit->search, (void *) edit, q, edit->last_byte, &len))
1038 break;
1039 if (found == 0)
1040 edit->search_start = edit->search->normal_offset;
1041 found++;
1042 l += edit_count_lines (edit, q, edit->search->normal_offset);
1043 if (l != l_last)
1045 book_mark_insert (edit, l, BOOK_MARK_FOUND_COLOR);
1046 books++;
1048 l_last = l;
1049 q = edit->search->normal_offset + 1;
1052 if (found == 0)
1053 edit_error_dialog (_("Search"), _("Search string not found"));
1054 else
1055 edit_cursor_move (edit, edit->search_start - edit->curs1);
1057 else
1059 if (edit->found_len != 0 && edit->search_start == edit->found_start + 1
1060 && edit_search_options.backwards)
1061 edit->search_start--;
1063 if (edit->found_len != 0 && edit->search_start == edit->found_start - 1
1064 && !edit_search_options.backwards)
1065 edit->search_start++;
1067 if (editcmd_find (edit, &len))
1069 edit->found_start = edit->search_start = edit->search->normal_offset;
1070 edit->found_len = len;
1071 edit->over_col = 0;
1072 edit_cursor_move (edit, edit->search_start - edit->curs1);
1073 edit_scroll_screen_over_cursor (edit);
1074 if (edit_search_options.backwards)
1075 edit->search_start--;
1076 else
1077 edit->search_start++;
1079 else
1081 edit->search_start = edit->curs1;
1082 if (edit->search->error_str != NULL)
1083 edit_error_dialog (_("Search"), edit->search->error_str);
1087 edit->force |= REDRAW_COMPLETELY;
1088 edit_scroll_screen_over_cursor (edit);
1091 /* --------------------------------------------------------------------------------------------- */
1093 static void
1094 edit_search (WEdit * edit)
1096 if (editcmd_dialog_search_show (edit))
1098 edit->search_line_type = edit_get_search_line_type (edit->search);
1099 edit_search_fix_search_start_if_selection (edit);
1100 edit_do_search (edit);
1104 /* --------------------------------------------------------------------------------------------- */
1105 /** Return a null terminated length of text. Result must be g_free'd */
1107 static unsigned char *
1108 edit_get_block (WEdit * edit, long start, long finish, int *l)
1110 unsigned char *s, *r;
1111 r = s = g_malloc0 (finish - start + 1);
1112 if (edit->column_highlight)
1114 *l = 0;
1115 /* copy from buffer, excluding chars that are out of the column 'margins' */
1116 while (start < finish)
1118 int c;
1119 long x;
1120 x = edit_move_forward3 (edit, edit_bol (edit, start), 0, start);
1121 c = edit_get_byte (edit, start);
1122 if ((x >= edit->column1 && x < edit->column2)
1123 || (x >= edit->column2 && x < edit->column1) || c == '\n')
1125 *s++ = c;
1126 (*l)++;
1128 start++;
1131 else
1133 *l = finish - start;
1134 while (start < finish)
1135 *s++ = edit_get_byte (edit, start++);
1137 *s = 0;
1138 return r;
1141 /* --------------------------------------------------------------------------------------------- */
1142 /** copies a block to clipboard file */
1144 static int
1145 edit_save_block_to_clip_file (WEdit * edit, long start, long finish)
1147 int ret;
1148 gchar *tmp;
1149 tmp = mc_config_get_full_path (EDIT_CLIP_FILE);
1150 ret = edit_save_block (edit, tmp, start, finish);
1151 g_free (tmp);
1152 return ret;
1155 /* --------------------------------------------------------------------------------------------- */
1157 static void
1158 pipe_mail (WEdit * edit, char *to, char *subject, char *cc)
1160 FILE *p = 0;
1161 char *s;
1163 to = name_quote (to, 0);
1164 subject = name_quote (subject, 0);
1165 cc = name_quote (cc, 0);
1166 s = g_strconcat ("mail -s ", subject, *cc ? " -c " : "", cc, " ", to, (char *) NULL);
1167 g_free (to);
1168 g_free (subject);
1169 g_free (cc);
1171 if (s)
1173 p = popen (s, "w");
1174 g_free (s);
1177 if (p)
1179 long i;
1180 for (i = 0; i < edit->last_byte; i++)
1181 fputc (edit_get_byte (edit, i), p);
1182 pclose (p);
1186 /* --------------------------------------------------------------------------------------------- */
1188 static gboolean
1189 is_break_char (char c)
1191 return (isspace (c) || strchr ("{}[]()<>=|/\\!?~-+`'\",.;:#$%^&*", c));
1194 /* --------------------------------------------------------------------------------------------- */
1195 /** find first character of current word */
1197 static int
1198 edit_find_word_start (WEdit * edit, long *word_start, gsize * word_len)
1200 int c, last;
1201 gsize i;
1203 /* return if at begin of file */
1204 if (edit->curs1 <= 0)
1205 return 0;
1207 c = (unsigned char) edit_get_byte (edit, edit->curs1 - 1);
1208 /* return if not at end or in word */
1209 if (is_break_char (c))
1210 return 0;
1212 /* search start of word to be completed */
1213 for (i = 2;; i++)
1215 /* return if at begin of file */
1216 if ((gsize) edit->curs1 < i)
1217 return 0;
1219 last = c;
1220 c = (unsigned char) edit_get_byte (edit, edit->curs1 - i);
1222 if (is_break_char (c))
1224 /* return if word starts with digit */
1225 if (isdigit (last))
1226 return 0;
1228 *word_start = edit->curs1 - (i - 1); /* start found */
1229 *word_len = i - 1;
1230 break;
1233 /* success */
1234 return 1;
1237 /* --------------------------------------------------------------------------------------------- */
1239 * Get current word under cursor
1241 * @param edit editor object
1242 * @param srch mc_search object
1243 * @param word_start start word position
1245 * @return newly allocated string or NULL if no any words under cursor
1248 static char *
1249 edit_collect_completions_get_current_word (WEdit * edit, mc_search_t * srch, long word_start)
1251 gsize len = 0, i;
1252 GString *temp;
1254 if (!mc_search_run (srch, (void *) edit, word_start, edit->last_byte, &len))
1255 return NULL;
1257 temp = g_string_sized_new (len);
1259 for (i = 0; i < len; i++)
1261 int chr;
1263 chr = edit_get_byte (edit, word_start + i);
1264 if (!isspace (chr))
1265 g_string_append_c (temp, chr);
1268 return g_string_free (temp, temp->len == 0);
1271 /* --------------------------------------------------------------------------------------------- */
1272 /** collect the possible completions */
1274 static gsize
1275 edit_collect_completions (WEdit * edit, long word_start, gsize word_len,
1276 char *match_expr, struct selection *compl, gsize * num)
1278 gsize len = 0;
1279 gsize max_len = 0;
1280 gsize i;
1281 int skip;
1282 GString *temp;
1283 mc_search_t *srch;
1284 long last_byte, start = -1;
1285 char *current_word;
1287 srch = mc_search_new (match_expr, -1);
1288 if (srch == NULL)
1289 return 0;
1291 if (mc_config_get_bool
1292 (mc_main_config, CONFIG_APP_SECTION, "editor_wordcompletion_collect_entire_file", 0))
1294 last_byte = edit->last_byte;
1296 else
1298 last_byte = word_start;
1301 srch->search_type = MC_SEARCH_T_REGEX;
1302 srch->is_case_sensitive = TRUE;
1303 srch->search_fn = edit_search_cmd_callback;
1305 current_word = edit_collect_completions_get_current_word (edit, srch, word_start);
1307 temp = g_string_new ("");
1309 /* collect max MAX_WORD_COMPLETIONS completions */
1310 while (mc_search_run (srch, (void *) edit, start + 1, last_byte, &len))
1312 g_string_set_size (temp, 0);
1313 start = srch->normal_offset;
1315 /* add matched completion if not yet added */
1316 for (i = 0; i < len; i++)
1318 skip = edit_get_byte (edit, start + i);
1319 if (isspace (skip))
1320 continue;
1322 /* skip current word */
1323 if (start + (long) i == word_start)
1324 break;
1326 g_string_append_c (temp, skip);
1329 if (temp->len == 0)
1330 continue;
1332 if (current_word != NULL && strcmp (current_word, temp->str) == 0)
1333 continue;
1335 skip = 0;
1337 for (i = 0; i < *num; i++)
1339 if (strncmp
1340 ((char *) &compl[i].text[word_len],
1341 (char *) &temp->str[word_len], max (len, compl[i].len) - word_len) == 0)
1343 struct selection this = compl[i];
1344 for (++i; i < *num; i++)
1346 compl[i - 1] = compl[i];
1348 compl[*num - 1] = this;
1349 skip = 1;
1350 break; /* skip it, already added */
1353 if (skip != 0)
1354 continue;
1356 if (*num == MAX_WORD_COMPLETIONS)
1358 g_free (compl[0].text);
1359 for (i = 1; i < *num; i++)
1361 compl[i - 1] = compl[i];
1363 (*num)--;
1365 #ifdef HAVE_CHARSET
1367 GString *recoded;
1368 recoded = str_convert_to_display (temp->str);
1370 if (recoded && recoded->len)
1371 g_string_assign (temp, recoded->str);
1373 g_string_free (recoded, TRUE);
1375 #endif
1376 compl[*num].text = g_strdup (temp->str);
1377 compl[*num].len = temp->len;
1378 (*num)++;
1379 start += len;
1381 /* note the maximal length needed for the completion dialog */
1382 if (len > max_len)
1383 max_len = len;
1386 mc_search_free (srch);
1387 g_string_free (temp, TRUE);
1388 g_free (current_word);
1390 return max_len;
1393 /* --------------------------------------------------------------------------------------------- */
1395 static void
1396 edit_insert_column_of_text (WEdit * edit, unsigned char *data, int size, int width,
1397 long *start_pos, long *end_pos, int *col1, int *col2)
1399 long cursor;
1400 int i, col;
1402 cursor = edit->curs1;
1403 col = edit_get_col (edit);
1405 for (i = 0; i < size; i++)
1407 if (data[i] != '\n')
1408 edit_insert (edit, data[i]);
1409 else
1410 { /* fill in and move to next line */
1411 int l;
1412 long p;
1414 if (edit_get_byte (edit, edit->curs1) != '\n')
1416 l = width - (edit_get_col (edit) - col);
1417 while (l > 0)
1419 edit_insert (edit, ' ');
1420 l -= space_width;
1423 for (p = edit->curs1;; p++)
1425 if (p == edit->last_byte)
1427 edit_cursor_move (edit, edit->last_byte - edit->curs1);
1428 edit_insert_ahead (edit, '\n');
1429 p++;
1430 break;
1432 if (edit_get_byte (edit, p) == '\n')
1434 p++;
1435 break;
1438 edit_cursor_move (edit, edit_move_forward3 (edit, p, col, 0) - edit->curs1);
1439 l = col - edit_get_col (edit);
1440 while (l >= space_width)
1442 edit_insert (edit, ' ');
1443 l -= space_width;
1448 *col1 = col;
1449 *col2 = col + width;
1450 *start_pos = cursor;
1451 *end_pos = edit->curs1;
1452 edit_cursor_move (edit, cursor - edit->curs1);
1455 /* --------------------------------------------------------------------------------------------- */
1457 static int
1458 edit_macro_comparator (gconstpointer * macro1, gconstpointer * macro2)
1460 const macros_t *m1 = (const macros_t *) macro1;
1461 const macros_t *m2 = (const macros_t *) macro2;
1463 return m1->hotkey - m2->hotkey;
1466 /* --------------------------------------------------------------------------------------------- */
1468 static void
1469 edit_macro_sort_by_hotkey (void)
1471 if (macros_list != NULL && macros_list->len != 0)
1472 g_array_sort (macros_list, (GCompareFunc) edit_macro_comparator);
1475 /* --------------------------------------------------------------------------------------------- */
1477 static gboolean
1478 edit_get_macro (WEdit * edit, int hotkey, const macros_t ** macros, guint * indx)
1480 const macros_t *array_start = &g_array_index (macros_list, struct macros_t, 0);
1481 macros_t *result;
1482 macros_t search_macro;
1484 (void) edit;
1486 search_macro.hotkey = hotkey;
1487 result = bsearch (&search_macro, macros_list->data, macros_list->len,
1488 sizeof (macros_t), (GCompareFunc) edit_macro_comparator);
1490 if (result != NULL && result->macro != NULL)
1492 *indx = (result - array_start);
1493 *macros = result;
1494 return TRUE;
1496 *indx = 0;
1497 return FALSE;
1500 /* --------------------------------------------------------------------------------------------- */
1501 /** returns FALSE on error */
1503 static gboolean
1504 edit_delete_macro (WEdit * edit, int hotkey)
1506 mc_config_t *macros_config = NULL;
1507 const char *section_name = "editor";
1508 gchar *macros_fname;
1509 guint indx;
1510 char *skeyname;
1511 const macros_t *macros = NULL;
1513 /* clear array of actions for current hotkey */
1514 while (edit_get_macro (edit, hotkey, &macros, &indx))
1516 if (macros->macro != NULL)
1517 g_array_free (macros->macro, TRUE);
1518 macros = NULL;
1519 g_array_remove_index (macros_list, indx);
1520 edit_macro_sort_by_hotkey ();
1523 macros_fname = mc_config_get_full_path (MC_MACRO_FILE);
1524 macros_config = mc_config_init (macros_fname);
1525 g_free (macros_fname);
1527 if (macros_config == NULL)
1528 return FALSE;
1530 skeyname = lookup_key_by_code (hotkey);
1531 while (mc_config_del_key (macros_config, section_name, skeyname))
1533 g_free (skeyname);
1534 mc_config_save_file (macros_config, NULL);
1535 mc_config_deinit (macros_config);
1536 return TRUE;
1540 /* --------------------------------------------------------------------------------------------- */
1541 /*** public functions ****************************************************************************/
1542 /* --------------------------------------------------------------------------------------------- */
1544 void
1545 edit_refresh_cmd (WEdit * edit)
1547 #ifdef HAVE_SLANG
1548 int color;
1550 edit_get_syntax_color (edit, -1, &color);
1551 tty_touch_screen ();
1552 mc_refresh ();
1553 #else
1554 (void) edit;
1556 clr_scr ();
1557 repaint_screen ();
1558 #endif /* !HAVE_SLANG */
1559 tty_keypad (TRUE);
1562 /* --------------------------------------------------------------------------------------------- */
1564 void
1565 edit_save_mode_cmd (void)
1567 /* diaog sizes */
1568 const int DLG_X = 38;
1569 const int DLG_Y = 13;
1571 char *str_result;
1573 const char *str[] = {
1574 N_("&Quick save"),
1575 N_("&Safe save"),
1576 N_("&Do backups with following extension:")
1579 QuickWidget widgets[] = {
1580 /* 0 */
1581 QUICK_BUTTON (18, DLG_X, DLG_Y - 3, DLG_Y, N_("&Cancel"), B_CANCEL, NULL),
1582 /* 1 */
1583 QUICK_BUTTON (6, DLG_X, DLG_Y - 3, DLG_Y, N_("&OK"), B_ENTER, NULL),
1584 /* 2 */
1585 QUICK_CHECKBOX (4, DLG_X, 8, DLG_Y, N_("Check &POSIX new line"), &option_check_nl_at_eof),
1586 /* 3 */
1587 QUICK_INPUT (8, DLG_X, 6, DLG_Y, option_backup_ext, 9, 0, "edit-backup-ext", &str_result),
1588 /* 4 */
1589 QUICK_RADIO (4, DLG_X, 3, DLG_Y, 3, str, &option_save_mode),
1590 QUICK_END
1593 QuickDialog dialog = {
1594 DLG_X, DLG_Y, -1, -1, N_("Edit Save Mode"),
1595 "[Edit Save Mode]", widgets, NULL, NULL, FALSE
1598 size_t i;
1599 size_t maxlen = 0;
1600 size_t w0, w1, b_len, w3;
1602 #ifdef HAVE_ASSERT_H
1603 assert (option_backup_ext != NULL);
1604 #endif
1606 /* OK/Cancel buttons */
1607 w0 = str_term_width1 (_(widgets[0].u.button.text)) + 3;
1608 w1 = str_term_width1 (_(widgets[1].u.button.text)) + 5; /* default button */
1609 b_len = w0 + w1 + 3;
1611 maxlen = max (b_len, (size_t) str_term_width1 (_(dialog.title)) + 2);
1613 w3 = 0;
1614 for (i = 0; i < 3; i++)
1616 #ifdef ENABLE_NLS
1617 str[i] = _(str[i]);
1618 #endif
1619 w3 = max (w3, (size_t) str_term_width1 (str[i]));
1622 maxlen = max (maxlen, w3 + 4);
1624 dialog.xlen = min ((size_t) COLS, maxlen + 8);
1626 widgets[3].u.input.len = w3;
1627 widgets[1].relative_x = (dialog.xlen - b_len) / 2;
1628 widgets[0].relative_x = widgets[1].relative_x + w0 + 2;
1630 for (i = 0; i < sizeof (widgets) / sizeof (widgets[0]); i++)
1631 widgets[i].x_divisions = dialog.xlen;
1633 if (quick_dialog (&dialog) != B_CANCEL)
1635 g_free (option_backup_ext);
1636 option_backup_ext = str_result;
1640 /* --------------------------------------------------------------------------------------------- */
1642 void
1643 edit_set_filename (WEdit * edit, const vfs_path_t * name_vpath)
1645 vfs_path_free (edit->filename_vpath);
1646 edit->filename_vpath = vfs_path_clone (name_vpath);
1648 if (edit->dir_vpath == NULL)
1649 edit->dir_vpath = vfs_path_clone (vfs_get_raw_current_dir ());
1652 /* --------------------------------------------------------------------------------------------- */
1653 /* Here we want to warn the users of overwriting an existing file,
1654 but only if they have made a change to the filename */
1655 /* returns 1 on success */
1657 edit_save_as_cmd (WEdit * edit)
1659 /* This heads the 'Save As' dialog box */
1660 vfs_path_t *exp_vpath;
1661 int save_lock = 0;
1662 int different_filename = 0;
1664 if (!edit_check_newline (edit))
1665 return 0;
1667 exp_vpath = edit_get_save_file_as (edit);
1668 edit_push_undo_action (edit, KEY_PRESS + edit->start_display);
1670 if (exp_vpath != NULL)
1672 if (vfs_path_len (exp_vpath) == 0)
1673 goto ret;
1674 else
1676 int rv;
1678 if (vfs_path_cmp (edit->filename_vpath, exp_vpath) != 0)
1680 int file;
1681 struct stat sb;
1683 if (mc_stat (exp_vpath, &sb) == 0 && !S_ISREG (sb.st_mode))
1685 edit_error_dialog (_("Save as"),
1686 get_sys_error (_
1687 ("Cannot save: destination is not a regular file")));
1688 goto ret;
1691 different_filename = 1;
1692 file = mc_open (exp_vpath, O_RDONLY | O_BINARY);
1694 if (file != -1)
1696 /* the file exists */
1697 mc_close (file);
1698 /* Overwrite the current file or cancel the operation */
1699 if (edit_query_dialog2
1700 (_("Warning"),
1701 _("A file already exists with this name"), _("&Overwrite"), _("&Cancel")))
1702 goto ret;
1704 else
1706 edit->stat1.st_mode |= S_IWUSR;
1708 save_lock = lock_file (exp_vpath);
1710 else
1712 /* filenames equal, check if already locked */
1713 if (!edit->locked && !edit->delete_file)
1714 save_lock = lock_file (exp_vpath);
1717 if (different_filename)
1720 * Allow user to write into saved (under another name) file
1721 * even if original file had r/o user permissions.
1723 edit->stat1.st_mode |= S_IWRITE;
1726 rv = edit_save_file (edit, exp_vpath);
1727 switch (rv)
1729 case 1:
1730 /* Succesful, so unlock both files */
1731 if (different_filename)
1733 if (save_lock)
1734 unlock_file (exp_vpath);
1735 if (edit->locked)
1736 edit->locked = unlock_file (edit->filename_vpath);
1738 else
1740 if (edit->locked || save_lock)
1741 edit->locked = unlock_file (edit->filename_vpath);
1744 edit_set_filename (edit, exp_vpath);
1745 if (edit->lb != LB_ASIS)
1746 edit_reload (edit, exp_vpath);
1747 edit->modified = 0;
1748 edit->delete_file = 0;
1749 if (different_filename)
1750 edit_load_syntax (edit, NULL, edit->syntax_type);
1751 vfs_path_free (exp_vpath);
1752 edit->force |= REDRAW_COMPLETELY;
1753 return 1;
1754 default:
1755 edit_error_dialog (_("Save as"), get_sys_error (_("Cannot save file")));
1756 /* fallthrough */
1757 case -1:
1758 /* Failed, so maintain modify (not save) lock */
1759 if (save_lock)
1760 unlock_file (exp_vpath);
1761 break;
1766 ret:
1767 vfs_path_free (exp_vpath);
1768 edit->force |= REDRAW_COMPLETELY;
1769 return 0;
1772 /* {{{ Macro stuff starts here */
1773 /* --------------------------------------------------------------------------------------------- */
1775 void
1776 edit_delete_macro_cmd (WEdit * edit)
1778 int hotkey;
1780 hotkey = editcmd_dialog_raw_key_query (_("Delete macro"), _("Press macro hotkey:"), 1);
1782 if (hotkey != 0 && !edit_delete_macro (edit, hotkey))
1783 message (D_ERROR, _("Delete macro"), _("Macro not deleted"));
1786 /* --------------------------------------------------------------------------------------------- */
1788 /** returns FALSE on error */
1789 gboolean
1790 edit_execute_macro (WEdit * edit, int hotkey)
1792 gboolean res = FALSE;
1794 if (hotkey != 0)
1796 const macros_t *macros;
1797 guint indx;
1799 if (edit_get_macro (edit, hotkey, &macros, &indx) &&
1800 macros->macro != NULL && macros->macro->len != 0)
1802 guint i;
1804 edit->force |= REDRAW_PAGE;
1806 for (i = 0; i < macros->macro->len; i++)
1808 const macro_action_t *m_act;
1810 m_act = &g_array_index (macros->macro, struct macro_action_t, i);
1811 edit_execute_cmd (edit, m_act->action, m_act->ch);
1812 res = TRUE;
1817 return res;
1820 /* --------------------------------------------------------------------------------------------- */
1822 /** returns FALSE on error */
1823 gboolean
1824 edit_store_macro_cmd (WEdit * edit)
1826 int i;
1827 int hotkey;
1828 GString *marcros_string;
1829 mc_config_t *macros_config = NULL;
1830 const char *section_name = "editor";
1831 gchar *macros_fname;
1832 GArray *macros; /* current macro */
1833 int tmp_act;
1834 gboolean have_macro = FALSE;
1835 char *skeyname = NULL;
1837 hotkey = editcmd_dialog_raw_key_query (_("Save macro"), _("Press the macro's new hotkey:"), 1);
1838 if (hotkey == ESC_CHAR)
1839 return FALSE;
1841 tmp_act = keybind_lookup_keymap_command (editor_map, hotkey);
1843 /* return FALSE if try assign macro into restricted hotkeys */
1844 if (tmp_act == CK_MacroStartRecord
1845 || tmp_act == CK_MacroStopRecord || tmp_act == CK_MacroStartStopRecord)
1846 return FALSE;
1848 edit_delete_macro (edit, hotkey);
1850 macros_fname = mc_config_get_full_path (MC_MACRO_FILE);
1851 macros_config = mc_config_init (macros_fname);
1852 g_free (macros_fname);
1854 if (macros_config == NULL)
1855 return FALSE;
1857 edit_push_undo_action (edit, KEY_PRESS + edit->start_display);
1859 marcros_string = g_string_sized_new (250);
1860 macros = g_array_new (TRUE, FALSE, sizeof (macro_action_t));
1862 skeyname = lookup_key_by_code (hotkey);
1864 for (i = 0; i < macro_index; i++)
1866 macro_action_t m_act;
1867 const char *action_name;
1869 action_name = keybind_lookup_actionname (record_macro_buf[i].action);
1871 if (action_name == NULL)
1872 break;
1874 m_act.action = record_macro_buf[i].action;
1875 m_act.ch = record_macro_buf[i].ch;
1876 g_array_append_val (macros, m_act);
1877 have_macro = TRUE;
1878 g_string_append_printf (marcros_string, "%s:%i;", action_name,
1879 (int) record_macro_buf[i].ch);
1881 if (have_macro)
1883 macros_t macro;
1884 macro.hotkey = hotkey;
1885 macro.macro = macros;
1886 g_array_append_val (macros_list, macro);
1887 mc_config_set_string (macros_config, section_name, skeyname, marcros_string->str);
1889 else
1890 mc_config_del_key (macros_config, section_name, skeyname);
1892 g_free (skeyname);
1893 edit_macro_sort_by_hotkey ();
1895 g_string_free (marcros_string, TRUE);
1896 mc_config_save_file (macros_config, NULL);
1897 mc_config_deinit (macros_config);
1898 return TRUE;
1901 /* --------------------------------------------------------------------------------------------- */
1903 gboolean
1904 edit_repeat_macro_cmd (WEdit * edit)
1906 int i, j;
1907 char *f;
1908 long count_repeat;
1909 char *error = NULL;
1911 f = input_dialog (_("Repeat last commands"), _("Repeat times:"), MC_HISTORY_EDIT_REPEAT, NULL);
1912 if (f == NULL || *f == '\0')
1914 g_free (f);
1915 return FALSE;
1918 count_repeat = strtol (f, &error, 0);
1920 if (*error != '\0')
1922 g_free (f);
1923 return FALSE;
1926 g_free (f);
1928 edit_push_undo_action (edit, KEY_PRESS + edit->start_display);
1929 edit->force |= REDRAW_PAGE;
1931 for (j = 0; j < count_repeat; j++)
1932 for (i = 0; i < macro_index; i++)
1933 edit_execute_cmd (edit, record_macro_buf[i].action, record_macro_buf[i].ch);
1934 edit_update_screen (edit);
1935 return TRUE;
1938 /* --------------------------------------------------------------------------------------------- */
1939 /** return FALSE on error */
1941 gboolean
1942 edit_load_macro_cmd (WEdit * edit)
1944 mc_config_t *macros_config = NULL;
1945 gchar **profile_keys, **keys;
1946 gchar **values, **curr_values;
1947 gsize len, values_len;
1948 const char *section_name = "editor";
1949 gchar *macros_fname;
1950 int hotkey;
1952 (void) edit;
1954 macros_fname = mc_config_get_full_path (MC_MACRO_FILE);
1955 macros_config = mc_config_init (macros_fname);
1956 g_free (macros_fname);
1958 if (macros_config == NULL)
1959 return FALSE;
1961 profile_keys = keys = mc_config_get_keys (macros_config, section_name, &len);
1962 while (*profile_keys != NULL)
1964 gboolean have_macro;
1965 GArray *macros;
1966 macros_t macro;
1968 macros = g_array_new (TRUE, FALSE, sizeof (macro_action_t));
1970 curr_values = values = mc_config_get_string_list (macros_config, section_name,
1971 *profile_keys, &values_len);
1972 hotkey = lookup_key (*profile_keys, NULL);
1973 have_macro = FALSE;
1975 while (*curr_values != NULL && *curr_values[0] != '\0')
1977 char **macro_pair = NULL;
1979 macro_pair = g_strsplit (*curr_values, ":", 2);
1981 if (macro_pair != NULL)
1983 macro_action_t m_act;
1984 if (macro_pair[0] == NULL || macro_pair[0][0] == '\0')
1985 m_act.action = 0;
1986 else
1988 m_act.action = keybind_lookup_action (macro_pair[0]);
1989 g_free (macro_pair[0]);
1990 macro_pair[0] = NULL;
1992 if (macro_pair[1] == NULL || macro_pair[1][0] == '\0')
1993 m_act.ch = -1;
1994 else
1996 m_act.ch = strtol (macro_pair[1], NULL, 0);
1997 g_free (macro_pair[1]);
1998 macro_pair[1] = NULL;
2000 if (m_act.action != 0)
2002 /* a shell command */
2003 if ((m_act.action / CK_PipeBlock (0)) == 1)
2005 m_act.action = CK_PipeBlock (0) + (m_act.ch > 0 ? m_act.ch : 0);
2006 m_act.ch = -1;
2008 g_array_append_val (macros, m_act);
2009 have_macro = TRUE;
2011 g_strfreev (macro_pair);
2012 macro_pair = NULL;
2014 curr_values++;
2016 if (have_macro)
2018 macro.hotkey = hotkey;
2019 macro.macro = macros;
2020 g_array_append_val (macros_list, macro);
2022 profile_keys++;
2023 g_strfreev (values);
2025 g_strfreev (keys);
2026 mc_config_deinit (macros_config);
2027 edit_macro_sort_by_hotkey ();
2028 return TRUE;
2031 /* }}} Macro stuff end here */
2033 /* --------------------------------------------------------------------------------------------- */
2034 /** returns 1 on success */
2037 edit_save_confirm_cmd (WEdit * edit)
2039 gchar *f = NULL;
2041 if (edit->filename_vpath == NULL)
2042 return edit_save_as_cmd (edit);
2044 if (!edit_check_newline (edit))
2045 return 0;
2047 if (edit_confirm_save)
2049 char *filename;
2050 gboolean ok;
2052 filename = vfs_path_to_str (edit->filename_vpath);
2053 f = g_strdup_printf (_("Confirm save file: \"%s\""), filename);
2054 g_free (filename);
2055 ok = (edit_query_dialog2 (_("Save file"), f, _("&Save"), _("&Cancel")) == 0);
2056 g_free (f);
2057 if (!ok)
2058 return 0;
2060 return edit_save_cmd (edit);
2063 /* --------------------------------------------------------------------------------------------- */
2065 /* returns TRUE on success */
2066 gboolean
2067 edit_new_cmd (WEdit * edit)
2069 if (edit->modified
2070 && edit_query_dialog2 (_("Warning"),
2071 _("Current text was modified without a file save.\n"
2072 "Continue discards these changes"),
2073 _("C&ontinue"), _("&Cancel")) == 1)
2075 edit->force |= REDRAW_COMPLETELY;
2076 return TRUE;
2079 edit->force |= REDRAW_COMPLETELY;
2081 return edit_renew (edit); /* if this gives an error, something has really screwed up */
2084 /* --------------------------------------------------------------------------------------------- */
2086 gboolean
2087 edit_load_cmd (WEdit * edit, edit_current_file_t what)
2089 gboolean ret = TRUE;
2091 if (edit->modified
2092 && edit_query_dialog2 (_("Warning"),
2093 _("Current text was modified without a file save.\n"
2094 "Continue discards these changes"), _("C&ontinue"),
2095 _("&Cancel")) == 1)
2097 edit->force |= REDRAW_COMPLETELY;
2098 return TRUE;
2101 switch (what)
2103 case EDIT_FILE_COMMON:
2105 char *filename, *exp;
2107 filename = vfs_path_to_str (edit->filename_vpath);
2108 exp = input_expand_dialog (_("Load"), _("Enter file name:"),
2109 MC_HISTORY_EDIT_LOAD, filename);
2110 g_free (filename);
2112 if (exp != NULL && *exp != '\0')
2114 vfs_path_t *exp_vpath;
2116 exp_vpath = vfs_path_from_str (exp);
2117 ret = edit_load_file_from_filename (edit, exp_vpath);
2118 vfs_path_free (exp_vpath);
2121 g_free (exp);
2123 break;
2125 case EDIT_FILE_SYNTAX:
2126 edit_load_syntax_file (edit);
2127 break;
2129 case EDIT_FILE_MENU:
2130 edit_load_menu_file (edit);
2131 break;
2133 default:
2134 break;
2137 edit->force |= REDRAW_COMPLETELY;
2138 return ret;
2141 /* --------------------------------------------------------------------------------------------- */
2143 * Close window with opened file.
2145 * @returns TRUE if file was closed.
2148 gboolean
2149 edit_close_cmd (WEdit * edit)
2151 gboolean ret;
2153 ret = (edit != NULL) && edit_ok_to_exit (edit);
2155 if (ret)
2157 Dlg_head *h = ((Widget *) edit)->owner;
2159 if (edit->locked != 0)
2160 unlock_file (edit->filename_vpath);
2162 del_widget (edit);
2164 if (edit_widget_is_editor ((Widget *) h->current->data))
2165 edit = (WEdit *) h->current->data;
2166 else
2168 edit = find_editor (h);
2169 if (edit != NULL)
2170 dlg_set_top_widget (edit);
2174 if (edit != NULL)
2175 edit->force |= REDRAW_COMPLETELY;
2177 return ret;
2180 /* --------------------------------------------------------------------------------------------- */
2182 if mark2 is -1 then marking is from mark1 to the cursor.
2183 Otherwise its between the markers. This handles this.
2184 Returns 1 if no text is marked.
2188 eval_marks (WEdit * edit, long *start_mark, long *end_mark)
2190 if (edit->mark1 != edit->mark2)
2192 long start_bol, start_eol;
2193 long end_bol, end_eol;
2194 long col1, col2;
2195 long diff1, diff2;
2196 long end_mark_curs;
2198 if (edit->end_mark_curs < 0)
2199 end_mark_curs = edit->curs1;
2200 else
2201 end_mark_curs = edit->end_mark_curs;
2203 if (edit->mark2 >= 0)
2205 *start_mark = min (edit->mark1, edit->mark2);
2206 *end_mark = max (edit->mark1, edit->mark2);
2208 else
2210 *start_mark = min (edit->mark1, end_mark_curs);
2211 *end_mark = max (edit->mark1, end_mark_curs);
2212 edit->column2 = edit->curs_col + edit->over_col;
2215 if (edit->column_highlight
2216 && (((edit->mark1 > end_mark_curs) && (edit->column1 < edit->column2))
2217 || ((edit->mark1 < end_mark_curs) && (edit->column1 > edit->column2))))
2219 start_bol = edit_bol (edit, *start_mark);
2220 start_eol = edit_eol (edit, start_bol - 1) + 1;
2221 end_bol = edit_bol (edit, *end_mark);
2222 end_eol = edit_eol (edit, *end_mark);
2223 col1 = min (edit->column1, edit->column2);
2224 col2 = max (edit->column1, edit->column2);
2226 diff1 =
2227 edit_move_forward3 (edit, start_bol, col2, 0) - edit_move_forward3 (edit, start_bol,
2228 col1, 0);
2229 diff2 =
2230 edit_move_forward3 (edit, end_bol, col2, 0) - edit_move_forward3 (edit, end_bol,
2231 col1, 0);
2233 *start_mark -= diff1;
2234 *end_mark += diff2;
2235 *start_mark = max (*start_mark, start_eol);
2236 *end_mark = min (*end_mark, end_eol);
2238 return 0;
2240 else
2242 *start_mark = *end_mark = 0;
2243 edit->column2 = edit->column1 = 0;
2244 return 1;
2248 /* --------------------------------------------------------------------------------------------- */
2250 void
2251 edit_insert_over (WEdit * edit)
2253 int i;
2255 for (i = 0; i < edit->over_col; i++)
2257 edit_insert (edit, ' ');
2259 edit->over_col = 0;
2262 /* --------------------------------------------------------------------------------------------- */
2265 edit_insert_column_of_text_from_file (WEdit * edit, int file,
2266 long *start_pos, long *end_pos, int *col1, int *col2)
2268 long cursor;
2269 int col;
2270 int blocklen = -1, width = 0;
2271 unsigned char *data;
2273 cursor = edit->curs1;
2274 col = edit_get_col (edit);
2275 data = g_malloc0 (TEMP_BUF_LEN);
2277 while ((blocklen = mc_read (file, (char *) data, TEMP_BUF_LEN)) > 0)
2279 int i;
2280 for (width = 0; width < blocklen; width++)
2282 if (data[width] == '\n')
2283 break;
2285 for (i = 0; i < blocklen; i++)
2287 if (data[i] == '\n')
2288 { /* fill in and move to next line */
2289 int l;
2290 long p;
2291 if (edit_get_byte (edit, edit->curs1) != '\n')
2293 l = width - (edit_get_col (edit) - col);
2294 while (l > 0)
2296 edit_insert (edit, ' ');
2297 l -= space_width;
2300 for (p = edit->curs1;; p++)
2302 if (p == edit->last_byte)
2304 edit_cursor_move (edit, edit->last_byte - edit->curs1);
2305 edit_insert_ahead (edit, '\n');
2306 p++;
2307 break;
2309 if (edit_get_byte (edit, p) == '\n')
2311 p++;
2312 break;
2315 edit_cursor_move (edit, edit_move_forward3 (edit, p, col, 0) - edit->curs1);
2316 l = col - edit_get_col (edit);
2317 while (l >= space_width)
2319 edit_insert (edit, ' ');
2320 l -= space_width;
2322 continue;
2324 edit_insert (edit, data[i]);
2327 *col1 = col;
2328 *col2 = col + width;
2329 *start_pos = cursor;
2330 *end_pos = edit->curs1;
2331 edit_cursor_move (edit, cursor - edit->curs1);
2332 g_free (data);
2334 return blocklen;
2337 /* --------------------------------------------------------------------------------------------- */
2339 void
2340 edit_block_copy_cmd (WEdit * edit)
2342 long start_mark, end_mark, current = edit->curs1;
2343 long col_delta = 0;
2344 long mark1, mark2;
2345 int c1, c2;
2346 int size;
2347 unsigned char *copy_buf;
2349 edit_update_curs_col (edit);
2350 if (eval_marks (edit, &start_mark, &end_mark))
2351 return;
2353 copy_buf = edit_get_block (edit, start_mark, end_mark, &size);
2355 /* all that gets pushed are deletes hence little space is used on the stack */
2357 edit_push_markers (edit);
2359 if (edit->column_highlight)
2361 col_delta = abs (edit->column2 - edit->column1);
2362 edit_insert_column_of_text (edit, copy_buf, size, col_delta, &mark1, &mark2, &c1, &c2);
2364 else
2366 while (size--)
2367 edit_insert_ahead (edit, copy_buf[size]);
2370 g_free (copy_buf);
2371 edit_scroll_screen_over_cursor (edit);
2373 if (edit->column_highlight)
2374 edit_set_markers (edit, edit->curs1, mark2, c1, c2);
2375 else if (start_mark < current && end_mark > current)
2376 edit_set_markers (edit, start_mark, end_mark + end_mark - start_mark, 0, 0);
2378 edit->force |= REDRAW_PAGE;
2382 /* --------------------------------------------------------------------------------------------- */
2384 void
2385 edit_block_move_cmd (WEdit * edit)
2387 long current;
2388 unsigned char *copy_buf = NULL;
2389 long start_mark, end_mark;
2390 long line;
2392 if (eval_marks (edit, &start_mark, &end_mark))
2393 return;
2395 line = edit->curs_line;
2396 if (edit->mark2 < 0)
2397 edit_mark_cmd (edit, 0);
2398 edit_push_markers (edit);
2400 if (edit->column_highlight)
2402 long mark1, mark2;
2403 int size;
2404 int b_width = 0;
2405 int c1, c2;
2406 int x, x2;
2408 c1 = min (edit->column1, edit->column2);
2409 c2 = max (edit->column1, edit->column2);
2410 b_width = (c2 - c1);
2412 edit_update_curs_col (edit);
2414 x = edit->curs_col;
2415 x2 = x + edit->over_col;
2417 /* do nothing when cursor inside first line of selected area */
2418 if ((edit_eol (edit, edit->curs1) == edit_eol (edit, start_mark)) && (x2 > c1 && x2 <= c2))
2419 return;
2421 if (edit->curs1 > start_mark && edit->curs1 < edit_eol (edit, end_mark))
2423 if (x > c2)
2424 x -= b_width;
2425 else if (x > c1 && x <= c2)
2426 x = c1;
2428 /* save current selection into buffer */
2429 copy_buf = edit_get_block (edit, start_mark, end_mark, &size);
2431 /* remove current selection */
2432 edit_block_delete_cmd (edit);
2434 edit->over_col = max (0, edit->over_col - b_width);
2435 /* calculate the cursor pos after delete block */
2436 current = edit_move_forward3 (edit, edit_bol (edit, edit->curs1), x, 0);
2437 edit_cursor_move (edit, current - edit->curs1);
2438 edit_scroll_screen_over_cursor (edit);
2440 /* add TWS if need before block insertion */
2441 if (option_cursor_beyond_eol && edit->over_col > 0)
2442 edit_insert_over (edit);
2444 edit_insert_column_of_text (edit, copy_buf, size, b_width, &mark1, &mark2, &c1, &c2);
2445 edit_set_markers (edit, mark1, mark2, c1, c2);
2447 else
2449 long count;
2451 current = edit->curs1;
2452 copy_buf = g_malloc0 (end_mark - start_mark);
2453 edit_cursor_move (edit, start_mark - edit->curs1);
2454 edit_scroll_screen_over_cursor (edit);
2455 count = start_mark;
2456 while (count < end_mark)
2458 copy_buf[end_mark - count - 1] = edit_delete (edit, 1);
2459 count++;
2461 edit_scroll_screen_over_cursor (edit);
2462 edit_cursor_move (edit,
2463 current - edit->curs1 -
2464 (((current - edit->curs1) > 0) ? end_mark - start_mark : 0));
2465 edit_scroll_screen_over_cursor (edit);
2466 while (count-- > start_mark)
2467 edit_insert_ahead (edit, copy_buf[end_mark - count - 1]);
2468 edit_set_markers (edit, edit->curs1, edit->curs1 + end_mark - start_mark, 0, 0);
2471 edit_scroll_screen_over_cursor (edit);
2472 g_free (copy_buf);
2473 edit->force |= REDRAW_PAGE;
2476 /* --------------------------------------------------------------------------------------------- */
2477 /** returns 1 if canceelled by user */
2480 edit_block_delete_cmd (WEdit * edit)
2482 long start_mark, end_mark;
2483 if (eval_marks (edit, &start_mark, &end_mark))
2485 edit_delete_line (edit);
2486 return 0;
2488 return edit_block_delete (edit);
2491 /* --------------------------------------------------------------------------------------------- */
2492 /** call with edit = 0 before shutdown to close memory leaks */
2494 void
2495 edit_replace_cmd (WEdit * edit, int again)
2497 /* 1 = search string, 2 = replace with */
2498 static char *saved1 = NULL; /* saved default[123] */
2499 static char *saved2 = NULL;
2500 char *input1 = NULL; /* user input from the dialog */
2501 char *input2 = NULL;
2502 GString *input2_str = NULL;
2503 char *disp1 = NULL;
2504 char *disp2 = NULL;
2505 long times_replaced = 0;
2506 gboolean once_found = FALSE;
2508 if (!edit)
2510 g_free (saved1), saved1 = NULL;
2511 g_free (saved2), saved2 = NULL;
2512 return;
2515 edit->force |= REDRAW_COMPLETELY;
2517 if (again && !saved1 && !saved2)
2518 again = 0;
2520 if (again)
2522 input1 = g_strdup (saved1 ? saved1 : "");
2523 input2 = g_strdup (saved2 ? saved2 : "");
2525 else
2527 char *tmp_inp1, *tmp_inp2;
2529 disp1 = edit_replace_cmd__conv_to_display (saved1 ? saved1 : (char *) "");
2530 disp2 = edit_replace_cmd__conv_to_display (saved2 ? saved2 : (char *) "");
2532 edit_push_undo_action (edit, KEY_PRESS + edit->start_display);
2534 editcmd_dialog_replace_show (edit, disp1, disp2, &input1, &input2);
2536 g_free (disp1);
2537 g_free (disp2);
2539 if (input1 == NULL || *input1 == '\0')
2541 edit->force = REDRAW_COMPLETELY;
2542 goto cleanup;
2545 tmp_inp1 = input1;
2546 tmp_inp2 = input2;
2547 input1 = edit_replace_cmd__conv_to_input (input1);
2548 input2 = edit_replace_cmd__conv_to_input (input2);
2549 g_free (tmp_inp1);
2550 g_free (tmp_inp2);
2552 g_free (saved1), saved1 = g_strdup (input1);
2553 g_free (saved2), saved2 = g_strdup (input2);
2555 mc_search_free (edit->search);
2556 edit->search = NULL;
2559 input2_str = g_string_new (input2);
2561 if (!edit->search)
2563 edit->search = mc_search_new (input1, -1);
2564 if (edit->search == NULL)
2566 edit->search_start = edit->curs1;
2567 goto cleanup;
2569 edit->search->search_type = edit_search_options.type;
2570 edit->search->is_all_charsets = edit_search_options.all_codepages;
2571 edit->search->is_case_sensitive = edit_search_options.case_sens;
2572 edit->search->whole_words = edit_search_options.whole_words;
2573 edit->search->search_fn = edit_search_cmd_callback;
2574 edit->search_line_type = edit_get_search_line_type (edit->search);
2575 edit_search_fix_search_start_if_selection (edit);
2578 if (edit->found_len && edit->search_start == edit->found_start + 1
2579 && edit_search_options.backwards)
2580 edit->search_start--;
2582 if (edit->found_len && edit->search_start == edit->found_start - 1
2583 && !edit_search_options.backwards)
2584 edit->search_start++;
2588 gsize len = 0;
2590 if (!editcmd_find (edit, &len))
2592 if (!(edit->search->error == MC_SEARCH_E_OK ||
2593 (once_found && edit->search->error == MC_SEARCH_E_NOTFOUND)))
2595 edit_error_dialog (_("Search"), edit->search->error_str);
2597 break;
2599 once_found = TRUE;
2601 edit->search_start = edit->search->normal_offset;
2602 /*returns negative on not found or error in pattern */
2604 if ((edit->search_start >= 0) && (edit->search_start < edit->last_byte))
2606 gsize i;
2607 GString *repl_str;
2609 edit->found_start = edit->search_start;
2610 i = edit->found_len = len;
2612 edit_cursor_move (edit, edit->search_start - edit->curs1);
2613 edit_scroll_screen_over_cursor (edit);
2615 if (edit->replace_mode == 0)
2617 int l;
2618 int prompt;
2620 l = edit->curs_row - edit->widget.lines / 3;
2621 if (l > 0)
2622 edit_scroll_downward (edit, l);
2623 if (l < 0)
2624 edit_scroll_upward (edit, -l);
2626 edit_scroll_screen_over_cursor (edit);
2627 edit->force |= REDRAW_PAGE;
2628 edit_render_keypress (edit);
2630 /*so that undo stops at each query */
2631 edit_push_key_press (edit);
2632 /* and prompt 2/3 down */
2633 disp1 = edit_replace_cmd__conv_to_display (saved1);
2634 disp2 = edit_replace_cmd__conv_to_display (saved2);
2635 prompt = editcmd_dialog_replace_prompt_show (edit, disp1, disp2, -1, -1);
2636 g_free (disp1);
2637 g_free (disp2);
2639 if (prompt == B_REPLACE_ALL)
2640 edit->replace_mode = 1;
2641 else if (prompt == B_SKIP_REPLACE)
2643 if (edit_search_options.backwards)
2644 edit->search_start--;
2645 else
2646 edit->search_start++;
2647 continue; /* loop */
2649 else if (prompt == B_CANCEL)
2651 edit->replace_mode = -1;
2652 break; /* loop */
2656 repl_str = mc_search_prepare_replace_str (edit->search, input2_str);
2658 if (edit->search->error != MC_SEARCH_E_OK)
2660 edit_error_dialog (_("Replace"), edit->search->error_str);
2661 g_string_free (repl_str, TRUE);
2662 break;
2665 /* delete then insert new */
2666 for (i = 0; i < len; i++)
2667 edit_delete (edit, 1);
2669 for (i = 0; i < repl_str->len; i++)
2670 edit_insert (edit, repl_str->str[i]);
2672 edit->found_len = repl_str->len;
2673 g_string_free (repl_str, TRUE);
2674 times_replaced++;
2676 /* so that we don't find the same string again */
2677 if (edit_search_options.backwards)
2679 edit->search_start--;
2681 else
2683 edit->search_start += edit->found_len + (len == 0 ? 1 : 0);
2685 if (edit->search_start >= edit->last_byte)
2686 break;
2689 edit_scroll_screen_over_cursor (edit);
2691 else
2693 /* try and find from right here for next search */
2694 edit->search_start = edit->curs1;
2695 edit_update_curs_col (edit);
2697 edit->force |= REDRAW_PAGE;
2698 edit_render_keypress (edit);
2700 if (times_replaced == 0)
2701 query_dialog (_("Replace"), _("Search string not found"), D_NORMAL, 1, _("&OK"));
2702 break;
2705 while (edit->replace_mode >= 0);
2707 edit_scroll_screen_over_cursor (edit);
2708 edit->force |= REDRAW_COMPLETELY;
2709 edit_render_keypress (edit);
2711 if ((edit->replace_mode == 1) && (times_replaced != 0))
2712 message (D_NORMAL, _("Replace"), _("%ld replacements made"), times_replaced);
2714 cleanup:
2715 g_free (input1);
2716 g_free (input2);
2717 if (input2_str != NULL)
2718 g_string_free (input2_str, TRUE);
2721 /* --------------------------------------------------------------------------------------------- */
2723 mc_search_cbret_t
2724 edit_search_cmd_callback (const void *user_data, gsize char_offset, int *current_char)
2726 *current_char = edit_get_byte ((WEdit *) user_data, (long) char_offset);
2727 return MC_SEARCH_CB_OK;
2730 /* --------------------------------------------------------------------------------------------- */
2732 void
2733 edit_search_cmd (WEdit * edit, gboolean again)
2736 if (edit == NULL)
2737 return;
2739 if (!again)
2740 edit_search (edit);
2741 else if (edit->last_search_string != NULL)
2742 edit_do_search (edit);
2743 else
2745 /* find last search string in history */
2746 GList *history;
2748 history = history_get (MC_HISTORY_SHARED_SEARCH);
2749 if (history != NULL && history->data != NULL)
2751 edit->last_search_string = (char *) history->data;
2752 history->data = NULL;
2753 history = g_list_first (history);
2754 g_list_foreach (history, (GFunc) g_free, NULL);
2755 g_list_free (history);
2757 edit->search = mc_search_new (edit->last_search_string, -1);
2758 if (edit->search == NULL)
2760 /* if not... then ask for an expression */
2761 g_free (edit->last_search_string);
2762 edit->last_search_string = NULL;
2763 edit_search (edit);
2765 else
2767 edit->search->search_type = edit_search_options.type;
2768 edit->search->is_all_charsets = edit_search_options.all_codepages;
2769 edit->search->is_case_sensitive = edit_search_options.case_sens;
2770 edit->search->whole_words = edit_search_options.whole_words;
2771 edit->search->search_fn = edit_search_cmd_callback;
2772 edit->search_line_type = edit_get_search_line_type (edit->search);
2773 edit_do_search (edit);
2776 else
2778 /* if not... then ask for an expression */
2779 g_free (edit->last_search_string);
2780 edit->last_search_string = NULL;
2781 edit_search (edit);
2787 /* --------------------------------------------------------------------------------------------- */
2789 * Check if it's OK to close the editor. If there are unsaved changes,
2790 * ask user. Return 1 if it's OK to exit, 0 to continue editing.
2793 gboolean
2794 edit_ok_to_exit (WEdit * edit)
2796 int act;
2798 if (!edit->modified)
2799 return TRUE;
2801 if (!mc_global.midnight_shutdown)
2803 if (!edit_check_newline (edit))
2804 return FALSE;
2806 query_set_sel (2);
2807 act = edit_query_dialog3 (_("Quit"), _("File was modified. Save with exit?"),
2808 _("&Yes"), _("&No"), _("&Cancel quit"));
2810 else
2812 act =
2813 edit_query_dialog2 (_("Quit"),
2814 _("Midnight Commander is being shut down.\nSave modified file?"),
2815 _("&Yes"), _("&No"));
2817 /* Esc is No */
2818 if (act == -1)
2819 act = 1;
2822 switch (act)
2824 case 0: /* Yes */
2825 edit_push_markers (edit);
2826 edit_set_markers (edit, 0, 0, 0, 0);
2827 if (!edit_save_cmd (edit) || mc_global.midnight_shutdown)
2828 return mc_global.midnight_shutdown;
2829 break;
2830 case 1: /* No */
2831 break;
2832 case 2: /* Cancel quit */
2833 case -1: /* Esc */
2834 return FALSE;
2837 return TRUE;
2840 /* --------------------------------------------------------------------------------------------- */
2841 /** save block, returns 1 on success */
2844 edit_save_block (WEdit * edit, const char *filename, long start, long finish)
2846 int len, file;
2847 vfs_path_t *vpath;
2849 vpath = vfs_path_from_str (filename);
2850 file = mc_open (vpath, O_CREAT | O_WRONLY | O_TRUNC,
2851 S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH | O_BINARY);
2852 vfs_path_free (vpath);
2853 if (file == -1)
2854 return 0;
2856 if (edit->column_highlight)
2858 int r;
2860 r = mc_write (file, VERTICAL_MAGIC, sizeof (VERTICAL_MAGIC));
2861 if (r > 0)
2863 unsigned char *block, *p;
2865 p = block = edit_get_block (edit, start, finish, &len);
2866 while (len)
2868 r = mc_write (file, p, len);
2869 if (r < 0)
2870 break;
2871 p += r;
2872 len -= r;
2874 g_free (block);
2877 else
2879 unsigned char *buf;
2880 int i = start, end;
2882 len = finish - start;
2883 buf = g_malloc0 (TEMP_BUF_LEN);
2884 while (start != finish)
2886 end = min (finish, start + TEMP_BUF_LEN);
2887 for (; i < end; i++)
2888 buf[i - start] = edit_get_byte (edit, i);
2889 len -= mc_write (file, (char *) buf, end - start);
2890 start = end;
2892 g_free (buf);
2894 mc_close (file);
2895 if (len)
2896 return 0;
2897 return 1;
2900 /* --------------------------------------------------------------------------------------------- */
2902 void
2903 edit_paste_from_history (WEdit * edit)
2905 (void) edit;
2906 edit_error_dialog (_("Error"), _("This function is not implemented"));
2909 /* --------------------------------------------------------------------------------------------- */
2912 edit_copy_to_X_buf_cmd (WEdit * edit)
2914 long start_mark, end_mark;
2915 if (eval_marks (edit, &start_mark, &end_mark))
2916 return 0;
2917 if (!edit_save_block_to_clip_file (edit, start_mark, end_mark))
2919 edit_error_dialog (_("Copy to clipboard"), get_sys_error (_("Unable to save to file")));
2920 return 1;
2922 /* try use external clipboard utility */
2923 mc_event_raise (MCEVENT_GROUP_CORE, "clipboard_file_to_ext_clip", NULL);
2925 return 0;
2928 /* --------------------------------------------------------------------------------------------- */
2931 edit_cut_to_X_buf_cmd (WEdit * edit)
2933 long start_mark, end_mark;
2934 if (eval_marks (edit, &start_mark, &end_mark))
2935 return 0;
2936 if (!edit_save_block_to_clip_file (edit, start_mark, end_mark))
2938 edit_error_dialog (_("Cut to clipboard"), _("Unable to save to file"));
2939 return 1;
2941 /* try use external clipboard utility */
2942 mc_event_raise (MCEVENT_GROUP_CORE, "clipboard_file_to_ext_clip", NULL);
2944 edit_block_delete_cmd (edit);
2945 edit_mark_cmd (edit, 1);
2946 return 0;
2949 /* --------------------------------------------------------------------------------------------- */
2951 void
2952 edit_paste_from_X_buf_cmd (WEdit * edit)
2954 vfs_path_t *tmp;
2956 /* try use external clipboard utility */
2957 mc_event_raise (MCEVENT_GROUP_CORE, "clipboard_file_from_ext_clip", NULL);
2958 tmp = mc_config_get_full_vpath (EDIT_CLIP_FILE);
2959 edit_insert_file (edit, tmp);
2960 vfs_path_free (tmp);
2964 /* --------------------------------------------------------------------------------------------- */
2966 * Ask user for the line and go to that line.
2967 * Negative numbers mean line from the end (i.e. -1 is the last line).
2970 void
2971 edit_goto_cmd (WEdit * edit)
2973 char *f;
2974 static long line = 0; /* line as typed, saved as default */
2975 long l;
2976 char *error;
2977 char s[32];
2979 g_snprintf (s, sizeof (s), "%ld", line);
2980 f = input_dialog (_("Goto line"), _("Enter line:"), MC_HISTORY_EDIT_GOTO_LINE, line ? s : "");
2981 if (!f)
2982 return;
2984 if (!*f)
2986 g_free (f);
2987 return;
2990 l = strtol (f, &error, 0);
2991 if (*error)
2993 g_free (f);
2994 return;
2997 line = l;
2998 if (l < 0)
2999 l = edit->total_lines + l + 2;
3000 edit_move_display (edit, l - edit->widget.lines / 2 - 1);
3001 edit_move_to_line (edit, l - 1);
3002 edit->force |= REDRAW_COMPLETELY;
3003 g_free (f);
3007 /* --------------------------------------------------------------------------------------------- */
3008 /** Return 1 on success */
3011 edit_save_block_cmd (WEdit * edit)
3013 long start_mark, end_mark;
3014 char *exp, *tmp;
3016 if (eval_marks (edit, &start_mark, &end_mark))
3017 return 1;
3019 tmp = mc_config_get_full_path (EDIT_CLIP_FILE);
3020 exp =
3021 input_expand_dialog (_("Save block"), _("Enter file name:"),
3022 MC_HISTORY_EDIT_SAVE_BLOCK, tmp);
3023 g_free (tmp);
3024 edit_push_undo_action (edit, KEY_PRESS + edit->start_display);
3025 if (exp)
3027 if (!*exp)
3029 g_free (exp);
3030 return 0;
3032 else
3034 if (edit_save_block (edit, exp, start_mark, end_mark))
3036 g_free (exp);
3037 edit->force |= REDRAW_COMPLETELY;
3038 return 1;
3040 else
3042 g_free (exp);
3043 edit_error_dialog (_("Save block"), get_sys_error (_("Cannot save file")));
3047 edit->force |= REDRAW_COMPLETELY;
3048 return 0;
3052 /* --------------------------------------------------------------------------------------------- */
3054 /** returns TRUE on success */
3055 gboolean
3056 edit_insert_file_cmd (WEdit * edit)
3058 char *tmp;
3059 char *exp;
3060 gboolean ret = FALSE;
3062 tmp = mc_config_get_full_path (EDIT_CLIP_FILE);
3063 exp = input_expand_dialog (_("Insert file"), _("Enter file name:"),
3064 MC_HISTORY_EDIT_INSERT_FILE, tmp);
3065 g_free (tmp);
3067 edit_push_undo_action (edit, KEY_PRESS + edit->start_display);
3069 if (exp != NULL && *exp != '\0')
3071 vfs_path_t *exp_vpath;
3073 exp_vpath = vfs_path_from_str (exp);
3074 ret = (edit_insert_file (edit, exp_vpath) >= 0);
3075 vfs_path_free (exp_vpath);
3077 if (!ret)
3078 edit_error_dialog (_("Insert file"), get_sys_error (_("Cannot insert file")));
3081 g_free (exp);
3083 edit->force |= REDRAW_COMPLETELY;
3084 return ret;
3087 /* --------------------------------------------------------------------------------------------- */
3088 /** sorts a block, returns -1 on system fail, 1 on cancel and 0 on success */
3091 edit_sort_cmd (WEdit * edit)
3093 static char *old = 0;
3094 char *exp, *tmp, *tmp_edit_block_name, *tmp_edit_temp_name;
3095 long start_mark, end_mark;
3096 int e;
3098 if (eval_marks (edit, &start_mark, &end_mark))
3100 edit_error_dialog (_("Sort block"), _("You must first highlight a block of text"));
3101 return 0;
3104 tmp = mc_config_get_full_path (EDIT_BLOCK_FILE);
3105 edit_save_block (edit, tmp, start_mark, end_mark);
3106 g_free (tmp);
3108 exp = input_dialog (_("Run sort"),
3109 _("Enter sort options (see manpage) separated by whitespace:"),
3110 MC_HISTORY_EDIT_SORT, (old != NULL) ? old : "");
3112 if (!exp)
3113 return 1;
3114 g_free (old);
3115 old = exp;
3116 tmp_edit_block_name = mc_config_get_full_path (EDIT_BLOCK_FILE);
3117 tmp_edit_temp_name = mc_config_get_full_path (EDIT_TEMP_FILE);
3118 tmp =
3119 g_strconcat (" sort ", exp, " ", tmp_edit_block_name,
3120 " > ", tmp_edit_temp_name, (char *) NULL);
3121 g_free (tmp_edit_temp_name);
3122 g_free (tmp_edit_block_name);
3124 e = system (tmp);
3125 g_free (tmp);
3126 if (e)
3128 if (e == -1 || e == 127)
3130 edit_error_dialog (_("Sort"), get_sys_error (_("Cannot execute sort command")));
3132 else
3134 char q[8];
3135 sprintf (q, "%d ", e);
3136 tmp = g_strdup_printf (_("Sort returned non-zero: %s"), q);
3137 edit_error_dialog (_("Sort"), tmp);
3138 g_free (tmp);
3140 return -1;
3143 edit->force |= REDRAW_COMPLETELY;
3145 if (edit_block_delete_cmd (edit))
3146 return 1;
3149 vfs_path_t *tmp_vpath;
3151 tmp_vpath = mc_config_get_full_vpath (EDIT_TEMP_FILE);
3152 edit_insert_file (edit, tmp_vpath);
3153 vfs_path_free (tmp_vpath);
3155 return 0;
3158 /* --------------------------------------------------------------------------------------------- */
3160 * Ask user for a command, execute it and paste its output back to the
3161 * editor.
3165 edit_ext_cmd (WEdit * edit)
3167 char *exp, *tmp, *tmp_edit_temp_file;
3168 int e;
3170 exp =
3171 input_dialog (_("Paste output of external command"),
3172 _("Enter shell command(s):"), MC_HISTORY_EDIT_PASTE_EXTCMD, NULL);
3174 if (!exp)
3175 return 1;
3177 tmp_edit_temp_file = mc_config_get_full_path (EDIT_TEMP_FILE);
3178 tmp = g_strconcat (exp, " > ", tmp_edit_temp_file, (char *) NULL);
3179 g_free (tmp_edit_temp_file);
3180 e = system (tmp);
3181 g_free (tmp);
3182 g_free (exp);
3184 if (e)
3186 edit_error_dialog (_("External command"), get_sys_error (_("Cannot execute command")));
3187 return -1;
3190 edit->force |= REDRAW_COMPLETELY;
3193 vfs_path_t *tmp_vpath;
3195 tmp_vpath = mc_config_get_full_vpath (EDIT_TEMP_FILE);
3196 edit_insert_file (edit, tmp_vpath);
3197 vfs_path_free (tmp_vpath);
3199 return 0;
3202 /* --------------------------------------------------------------------------------------------- */
3203 /** if block is 1, a block must be highlighted and the shell command
3204 processes it. If block is 0 the shell command is a straight system
3205 command, that just produces some output which is to be inserted */
3207 void
3208 edit_block_process_cmd (WEdit * edit, int macro_number)
3210 char *fname;
3211 char *macros_fname = NULL;
3213 fname = g_strdup_printf ("%s.%i.sh", MC_EXTMACRO_FILE, macro_number);
3214 macros_fname = g_build_filename (mc_config_get_data_path (), fname, (char *) NULL);
3215 user_menu (edit, macros_fname, 0);
3216 g_free (fname);
3217 g_free (macros_fname);
3218 edit->force |= REDRAW_COMPLETELY;
3221 /* --------------------------------------------------------------------------------------------- */
3223 void
3224 edit_mail_dialog (WEdit * edit)
3226 char *tmail_to;
3227 char *tmail_subject;
3228 char *tmail_cc;
3230 static char *mail_cc_last = 0;
3231 static char *mail_subject_last = 0;
3232 static char *mail_to_last = 0;
3234 QuickWidget quick_widgets[] = {
3235 /* 0 */ QUICK_BUTTON (6, 10, 9, MAIL_DLG_HEIGHT, N_("&Cancel"), B_CANCEL, NULL),
3236 /* 1 */ QUICK_BUTTON (2, 10, 9, MAIL_DLG_HEIGHT, N_("&OK"), B_ENTER, NULL),
3237 /* 2 */ QUICK_INPUT (3, 50, 8, MAIL_DLG_HEIGHT, "", 44, 0, "mail-dlg-input", &tmail_cc),
3238 /* 3 */ QUICK_LABEL (3, 50, 7, MAIL_DLG_HEIGHT, N_("Copies to")),
3239 /* 4 */ QUICK_INPUT (3, 50, 6, MAIL_DLG_HEIGHT, "", 44, 0, "mail-dlg-input-2",
3240 &tmail_subject),
3241 /* 5 */ QUICK_LABEL (3, 50, 5, MAIL_DLG_HEIGHT, N_("Subject")),
3242 /* 6 */ QUICK_INPUT (3, 50, 4, MAIL_DLG_HEIGHT, "", 44, 0, "mail-dlg-input-3", &tmail_to),
3243 /* 7 */ QUICK_LABEL (3, 50, 3, MAIL_DLG_HEIGHT, N_("To")),
3244 /* 8 */ QUICK_LABEL (3, 50, 2, MAIL_DLG_HEIGHT, N_("mail -s <subject> -c <cc> <to>")),
3245 QUICK_END
3248 QuickDialog Quick_input = {
3249 50, MAIL_DLG_HEIGHT, -1, -1, N_("Mail"),
3250 "[Input Line Keys]", quick_widgets, NULL, NULL, FALSE
3253 quick_widgets[2].u.input.text = mail_cc_last ? mail_cc_last : "";
3254 quick_widgets[4].u.input.text = mail_subject_last ? mail_subject_last : "";
3255 quick_widgets[6].u.input.text = mail_to_last ? mail_to_last : "";
3257 if (quick_dialog (&Quick_input) != B_CANCEL)
3259 g_free (mail_cc_last);
3260 g_free (mail_subject_last);
3261 g_free (mail_to_last);
3262 mail_cc_last = tmail_cc;
3263 mail_subject_last = tmail_subject;
3264 mail_to_last = tmail_to;
3265 pipe_mail (edit, mail_to_last, mail_subject_last, mail_cc_last);
3270 /*******************/
3271 /* Word Completion */
3272 /*******************/
3274 /* --------------------------------------------------------------------------------------------- */
3276 * Complete current word using regular expression search
3277 * backwards beginning at the current cursor position.
3280 void
3281 edit_complete_word_cmd (WEdit * edit)
3283 gsize i, max_len, word_len = 0, num_compl = 0;
3284 long word_start = 0;
3285 unsigned char *bufpos;
3286 char *match_expr;
3287 struct selection compl[MAX_WORD_COMPLETIONS]; /* completions */
3289 /* search start of word to be completed */
3290 if (!edit_find_word_start (edit, &word_start, &word_len))
3291 return;
3293 /* prepare match expression */
3294 bufpos = &edit->buffers1[word_start >> S_EDIT_BUF_SIZE][word_start & M_EDIT_BUF_SIZE];
3296 /* match_expr = g_strdup_printf ("\\b%.*s[a-zA-Z_0-9]+", word_len, bufpos); */
3297 match_expr =
3298 g_strdup_printf
3299 ("(^|\\s+|\\b)%.*s[^\\s\\.=\\+\\[\\]\\(\\)\\,\\;\\:\\\"\\'\\-\\?\\/\\|\\\\\\{\\}\\*\\&\\^\\%%\\$#@\\!]+",
3300 (int) word_len, bufpos);
3302 /* collect the possible completions */
3303 /* start search from begin to end of file */
3304 max_len =
3305 edit_collect_completions (edit, word_start, word_len, match_expr,
3306 (struct selection *) &compl, &num_compl);
3308 if (num_compl > 0)
3310 /* insert completed word if there is only one match */
3311 if (num_compl == 1)
3313 for (i = word_len; i < compl[0].len; i++)
3314 edit_insert (edit, *(compl[0].text + i));
3316 /* more than one possible completion => ask the user */
3317 else
3319 /* !!! usually only a beep is expected and when <ALT-TAB> is !!! */
3320 /* !!! pressed again the selection dialog pops up, but that !!! */
3321 /* !!! seems to require a further internal state !!! */
3322 /*tty_beep (); */
3324 /* let the user select the preferred completion */
3325 editcmd_dialog_completion_show (edit, max_len, word_len,
3326 (struct selection *) &compl, num_compl);
3330 g_free (match_expr);
3331 /* release memory before return */
3332 for (i = 0; i < num_compl; i++)
3333 g_free (compl[i].text);
3336 /* --------------------------------------------------------------------------------------------- */
3338 void
3339 edit_select_codepage_cmd (WEdit * edit)
3341 #ifdef HAVE_CHARSET
3342 if (do_select_codepage ())
3343 edit_set_codeset (edit);
3345 edit->force = REDRAW_COMPLETELY;
3346 edit_refresh_cmd (edit);
3347 #else
3348 (void) edit;
3349 #endif
3352 /* --------------------------------------------------------------------------------------------- */
3354 void
3355 edit_insert_literal_cmd (WEdit * edit)
3357 int char_for_insertion = editcmd_dialog_raw_key_query (_("Insert literal"),
3358 _("Press any key:"), 0);
3359 edit_execute_key_command (edit, -1, ascii_alpha_to_cntrl (char_for_insertion));
3362 /* --------------------------------------------------------------------------------------------- */
3364 void
3365 edit_begin_end_macro_cmd (WEdit * edit)
3367 /* edit is a pointer to the widget */
3368 if (edit != NULL)
3370 unsigned long command = macro_index < 0 ? CK_MacroStartRecord : CK_MacroStopRecord;
3371 edit_execute_key_command (edit, command, -1);
3375 /* --------------------------------------------------------------------------------------------- */
3377 void
3378 edit_begin_end_repeat_cmd (WEdit * edit)
3380 /* edit is a pointer to the widget */
3381 if (edit != NULL)
3383 unsigned long command = macro_index < 0 ? CK_RepeatStartRecord : CK_RepeatStopRecord;
3384 edit_execute_key_command (edit, command, -1);
3388 /* --------------------------------------------------------------------------------------------- */
3390 gboolean
3391 edit_load_forward_cmd (WEdit * edit)
3393 if (edit->modified
3394 && edit_query_dialog2 (_("Warning"),
3395 _("Current text was modified without a file save.\n"
3396 "Continue discards these changes"), _("C&ontinue"),
3397 _("&Cancel")) == 1)
3399 edit->force |= REDRAW_COMPLETELY;
3400 return TRUE;
3403 if (edit_stack_iterator + 1 >= MAX_HISTORY_MOVETO)
3404 return FALSE;
3406 if (edit_history_moveto[edit_stack_iterator + 1].line < 1)
3407 return FALSE;
3409 edit_stack_iterator++;
3410 if (edit_history_moveto[edit_stack_iterator].filename_vpath != NULL)
3411 return edit_reload_line (edit, edit_history_moveto[edit_stack_iterator].filename_vpath,
3412 edit_history_moveto[edit_stack_iterator].line);
3414 return FALSE;
3417 /* --------------------------------------------------------------------------------------------- */
3419 gboolean
3420 edit_load_back_cmd (WEdit * edit)
3422 if (edit->modified
3423 && edit_query_dialog2 (_("Warning"),
3424 _("Current text was modified without a file save.\n"
3425 "Continue discards these changes"), _("C&ontinue"),
3426 _("&Cancel")) == 1)
3428 edit->force |= REDRAW_COMPLETELY;
3429 return TRUE;
3432 if (edit_stack_iterator < 0)
3433 return FALSE;
3435 edit_stack_iterator--;
3436 if (edit_history_moveto[edit_stack_iterator].filename_vpath != NULL)
3437 return edit_reload_line (edit, edit_history_moveto[edit_stack_iterator].filename_vpath,
3438 edit_history_moveto[edit_stack_iterator].line);
3440 return FALSE;
3443 /* --------------------------------------------------------------------------------------------- */
3445 void
3446 edit_get_match_keyword_cmd (WEdit * edit)
3448 gsize word_len = 0, max_len = 0;
3449 int num_def = 0;
3450 int i;
3451 long word_start = 0;
3452 unsigned char *bufpos;
3453 char *match_expr;
3454 char *path = NULL;
3455 char *ptr = NULL;
3456 char *tagfile = NULL;
3458 etags_hash_t def_hash[MAX_DEFINITIONS];
3460 for (i = 0; i < MAX_DEFINITIONS; i++)
3462 def_hash[i].filename = NULL;
3465 /* search start of word to be completed */
3466 if (!edit_find_word_start (edit, &word_start, &word_len))
3467 return;
3469 /* prepare match expression */
3470 bufpos = &edit->buffers1[word_start >> S_EDIT_BUF_SIZE][word_start & M_EDIT_BUF_SIZE];
3471 match_expr = g_strdup_printf ("%.*s", (int) word_len, bufpos);
3473 ptr = g_get_current_dir ();
3474 path = g_strconcat (ptr, G_DIR_SEPARATOR_S, (char *) NULL);
3475 g_free (ptr);
3477 /* Recursive search file 'TAGS' in parent dirs */
3480 ptr = g_path_get_dirname (path);
3481 g_free (path);
3482 path = ptr;
3483 g_free (tagfile);
3484 tagfile = mc_build_filename (path, TAGS_NAME, (char *) NULL);
3485 if (exist_file (tagfile))
3486 break;
3488 while (strcmp (path, G_DIR_SEPARATOR_S) != 0);
3490 if (tagfile)
3492 num_def =
3493 etags_set_definition_hash (tagfile, path, match_expr, (etags_hash_t *) & def_hash);
3494 g_free (tagfile);
3496 g_free (path);
3498 max_len = MAX_WIDTH_DEF_DIALOG;
3499 word_len = 0;
3500 if (num_def > 0)
3502 editcmd_dialog_select_definition_show (edit, match_expr, max_len, word_len,
3503 (etags_hash_t *) & def_hash, num_def);
3505 g_free (match_expr);
3508 /* --------------------------------------------------------------------------------------------- */