2 Editor high level editing commands
4 Copyright (C) 1996, 1997, 1998, 2001, 2002, 2003, 2004, 2005, 2006,
6 The Free Software Foundation, Inc.
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/>.
28 * \brief Source: editor high level editing commands
33 /* #define PIPE_BLOCKS_SO_READ_BYTE_BY_BYTE */
44 #include <sys/types.h>
52 #include "lib/global.h"
53 #include "lib/tty/tty.h"
54 #include "lib/tty/key.h" /* XCTRL */
55 #include "lib/mcconfig.h"
57 #include "lib/strutil.h" /* utf string functions */
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"
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 ****************************************************************/
90 #define TEMP_BUF_LEN 1024
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 */
121 edit_save_file (WEdit
* edit
, const vfs_path_t
* filename_vpath
)
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
)
136 start_filename
= vpath_element
->path
;
137 if (*start_filename
== '\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
);
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
;
165 if (this_save_mode
== EDIT_QUICK_SAVE
&& !edit
->skip_detach_prompt
)
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"));
179 this_save_mode
= EDIT_SAFE_SAVE
;
182 edit
->skip_detach_prompt
= 1;
185 vfs_path_free (real_filename_vpath
);
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". */
196 rv
= edit_query_dialog2 (_("Warning"),
197 _("The file has been modified in the meantime. Save anyway?"),
198 _("&Yes"), _("&Cancel"));
201 vfs_path_free (real_filename_vpath
);
207 if (this_save_mode
!= EDIT_QUICK_SAVE
)
209 char *savedir
, *saveprefix
;
211 savedir
= vfs_path_tokens_get (real_filename_vpath
, 0, -1);
213 savedir
= g_strdup (".");
215 saveprefix
= mc_build_filename (savedir
, "cooledit", NULL
);
217 fd
= mc_mkstemps (&savename_vpath
, saveprefix
, NULL
);
219 if (savename_vpath
== NULL
)
221 vfs_path_free (real_filename_vpath
);
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().
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
);
242 p
= edit_get_write_filter (savename_vpath
, real_filename_vpath
);
248 file
= (FILE *) popen (p
, "w");
252 filelen
= edit_write_stream (edit
, file
);
256 if (pclose (file
) != 0)
258 tmp
= g_strdup_printf (_("Error writing to pipe: %s"), p
);
259 edit_error_dialog (_("Error"), tmp
);
268 tmp
= g_strdup_printf (_("Cannot open pipe for writing: %s"), p
);
269 edit_error_dialog (_("Error"), get_sys_error (tmp
));
276 else if (edit
->lb
== LB_ASIS
)
277 { /* do not change line breaks */
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
)
291 (fd
, (char *) edit
->buffers1
[buf
],
292 edit
->curs1
& M_EDIT_BUF_SIZE
) != (edit
->curs1
& M_EDIT_BUF_SIZE
))
296 else if (edit
->curs2
)
299 buf
= (edit
->curs2
>> S_EDIT_BUF_SIZE
);
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
))
312 if (mc_write (fd
, (char *) edit
->buffers2
[buf
], EDIT_BUF_SIZE
) != EDIT_BUF_SIZE
)
324 /* Update the file information, especially the mtime. */
325 if (mc_stat (savename_vpath
, &edit
->stat1
) == -1)
329 { /* change line breaks */
331 const vfs_path_element_t
*path_element
;
335 path_element
= vfs_path_get_by_index (savename_vpath
, -1);
336 file
= (FILE *) fopen (path_element
->path
, "w");
339 filelen
= edit_write_stream (edit
, file
);
346 msg
= g_strdup_printf (_("Cannot open file for writing: %s"), path_element
->path
);
347 edit_error_dialog (_("Error"), msg
);
353 if (filelen
!= edit
->last_byte
)
356 if (this_save_mode
== EDIT_DO_BACKUP
)
358 vfs_path_t
*tmp_vpath
;
362 assert (option_backup_ext
!= NULL
);
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
);
371 if (this_save_mode
!= EDIT_QUICK_SAVE
)
372 if (mc_rename (savename_vpath
, real_filename_vpath
) == -1)
375 vfs_path_free (real_filename_vpath
);
376 vfs_path_free (savename_vpath
);
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
);
388 /* --------------------------------------------------------------------------------------------- */
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 /* --------------------------------------------------------------------------------------------- */
403 edit_get_save_file_as (WEdit
* edit
)
406 #define DLG_HEIGHT 14
408 static LineBreaks cur_lb
= LB_ASIS
;
410 char *filename
= vfs_path_to_str (edit
->filename_vpath
);
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:")),
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
)
440 vfs_path_t
*ret_vpath
;
443 fname
= tilde_expand (filename_res
);
444 g_free (filename_res
);
445 ret_vpath
= vfs_path_from_str (fname
);
457 /* --------------------------------------------------------------------------------------------- */
459 /** returns 1 on success */
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 */
476 return edit_save_as_cmd (edit
);
477 edit
->force
|= REDRAW_COMPLETELY
;
480 edit
->delete_file
= 0;
487 /* --------------------------------------------------------------------------------------------- */
488 /** returns 1 on error */
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
;
496 prev_filename
= vfs_path_clone (edit
->filename_vpath
);
497 if (!edit_reload (edit
, exp_vpath
))
499 vfs_path_free (prev_filename
);
505 vfs_path_t
*fullpath
;
507 fullpath
= vfs_path_append_vpath_new (edit
->dir_vpath
, prev_filename
, (char *) NULL
);
508 unlock_file (fullpath
);
509 vfs_path_free (fullpath
);
511 vfs_path_free (prev_filename
);
515 /* --------------------------------------------------------------------------------------------- */
518 edit_load_syntax_file (WEdit
* edit
)
520 vfs_path_t
*extdir_vpath
;
525 dir
= query_dialog (_("Syntax file edit"),
526 _("Which syntax file you want to edit?"), D_NORMAL
, 2,
527 _("&User"), _("&System Wide"));
531 vfs_path_build_filename (mc_global
.sysconfig_dir
, "syntax", "Syntax", (char *) NULL
);
532 if (!exist_file (vfs_path_get_last_path_str (extdir_vpath
)))
534 vfs_path_free (extdir_vpath
);
536 vfs_path_build_filename (mc_global
.share_data_dir
, "syntax", "Syntax", (char *) NULL
);
541 vfs_path_t
*user_syntax_file_vpath
;
543 user_syntax_file_vpath
= mc_config_get_full_vpath (EDIT_SYNTAX_FILE
);
544 check_for_default (extdir_vpath
, user_syntax_file_vpath
);
545 edit_load_file_from_filename (edit
, user_syntax_file_vpath
);
546 vfs_path_free (user_syntax_file_vpath
);
549 edit_load_file_from_filename (edit
, extdir_vpath
);
551 vfs_path_free (extdir_vpath
);
554 /* --------------------------------------------------------------------------------------------- */
557 edit_load_menu_file (WEdit
* edit
)
559 vfs_path_t
*buffer_vpath
;
560 vfs_path_t
*menufile_vpath
;
563 dir
= query_dialog (_("Menu edit"),
564 _("Which menu file do you want to edit?"), D_NORMAL
,
565 geteuid () != 0 ? 2 : 3, _("&Local"), _("&User"), _("&System Wide"));
567 menufile_vpath
= vfs_path_build_filename (mc_global
.sysconfig_dir
, EDIT_GLOBAL_MENU
, NULL
);
569 if (!exist_file (vfs_path_get_last_path_str (menufile_vpath
)))
571 vfs_path_free (menufile_vpath
);
572 menufile_vpath
= vfs_path_build_filename (mc_global
.share_data_dir
, EDIT_GLOBAL_MENU
, NULL
);
578 buffer_vpath
= vfs_path_from_str (EDIT_LOCAL_MENU
);
579 check_for_default (menufile_vpath
, buffer_vpath
);
580 chmod (vfs_path_get_last_path_str (buffer_vpath
), 0600);
584 buffer_vpath
= mc_config_get_full_vpath (EDIT_HOME_MENU
);
585 check_for_default (menufile_vpath
, buffer_vpath
);
589 buffer_vpath
= vfs_path_build_filename (mc_global
.sysconfig_dir
, EDIT_GLOBAL_MENU
, NULL
);
590 if (!exist_file (vfs_path_get_last_path_str (buffer_vpath
)))
592 vfs_path_free (buffer_vpath
);
594 vfs_path_build_filename (mc_global
.share_data_dir
, EDIT_GLOBAL_MENU
, NULL
);
599 vfs_path_free (menufile_vpath
);
603 edit_load_file_from_filename (edit
, buffer_vpath
);
605 vfs_path_free (buffer_vpath
);
606 vfs_path_free (menufile_vpath
);
609 /* --------------------------------------------------------------------------------------------- */
612 edit_delete_column_of_text (WEdit
* edit
)
614 long p
, q
, r
, m1
, m2
;
617 eval_marks (edit
, &m1
, &m2
);
618 n
= edit_move_forward (edit
, m1
, 0, m2
) + 1;
619 c
= edit_move_forward3 (edit
, edit_bol (edit
, m1
), 0, m1
);
620 d
= edit_move_forward3 (edit
, edit_bol (edit
, m2
), 0, m2
);
621 b
= max (min (c
, d
), min (edit
->column1
, edit
->column2
));
622 c
= max (c
, max (edit
->column1
, edit
->column2
));
626 r
= edit_bol (edit
, edit
->curs1
);
627 p
= edit_move_forward3 (edit
, r
, b
, 0);
628 q
= edit_move_forward3 (edit
, r
, c
, 0);
633 edit_cursor_move (edit
, p
- edit
->curs1
);
636 /* delete line between margins */
637 if (edit_get_byte (edit
, edit
->curs1
) != '\n')
638 edit_delete (edit
, 1);
642 /* move to next line except on the last delete */
643 edit_cursor_move (edit
, edit_move_forward (edit
, edit
->curs1
, 1, 0) - edit
->curs1
);
647 /* --------------------------------------------------------------------------------------------- */
648 /** if success return 0 */
651 edit_block_delete (WEdit
* edit
)
654 long start_mark
, end_mark
;
655 int curs_pos
, line_width
;
656 long curs_line
, c1
, c2
;
658 if (eval_marks (edit
, &start_mark
, &end_mark
))
660 if (edit
->column_highlight
&& edit
->mark2
< 0)
661 edit_mark_cmd (edit
, 0);
662 if ((end_mark
- start_mark
) > option_max_undo
/ 2)
664 /* Warning message with a query to continue or cancel the operation */
665 if (edit_query_dialog2
668 ("Block is large, you may not be able to undo this action"),
669 _("C&ontinue"), _("&Cancel")))
674 c1
= min (edit
->column1
, edit
->column2
);
675 c2
= max (edit
->column1
, edit
->column2
);
679 edit_push_markers (edit
);
681 curs_line
= edit
->curs_line
;
683 /* calculate line width and cursor position before cut */
684 line_width
= edit_move_forward3 (edit
, edit_bol (edit
, edit
->curs1
), 0,
685 edit_eol (edit
, edit
->curs1
));
686 curs_pos
= edit
->curs_col
+ edit
->over_col
;
688 /* move cursor to start of selection */
689 edit_cursor_move (edit
, start_mark
- edit
->curs1
);
690 edit_scroll_screen_over_cursor (edit
);
692 if (start_mark
< end_mark
)
694 if (edit
->column_highlight
)
697 edit_mark_cmd (edit
, 0);
698 edit_delete_column_of_text (edit
);
699 /* move cursor to the saved position */
700 edit_move_to_line (edit
, curs_line
);
701 /* calculate line width after cut */
702 line_width
= edit_move_forward3 (edit
, edit_bol (edit
, edit
->curs1
), 0,
703 edit_eol (edit
, edit
->curs1
));
704 if (option_cursor_beyond_eol
&& curs_pos
> line_width
)
705 edit
->over_col
= curs_pos
- line_width
;
709 while (count
< end_mark
)
711 edit_delete (edit
, 1);
716 edit_set_markers (edit
, 0, 0, 0, 0);
717 edit
->force
|= REDRAW_PAGE
;
721 /* --------------------------------------------------------------------------------------------- */
724 editcmd_find (WEdit
* edit
, gsize
* len
)
726 off_t search_start
= edit
->search_start
;
729 long end_mark
= edit
->last_byte
;
732 if (edit_search_options
.only_in_selection
)
734 mark_res
= eval_marks (edit
, &start_mark
, &end_mark
);
737 edit
->search
->error
= MC_SEARCH_E_NOTFOUND
;
738 edit
->search
->error_str
= g_strdup (_("Search string not found"));
741 if (edit_search_options
.backwards
)
743 if (search_start
> end_mark
|| search_start
<= start_mark
)
745 search_start
= end_mark
;
750 if (search_start
< start_mark
|| search_start
>= end_mark
)
752 search_start
= start_mark
;
758 if (edit_search_options
.backwards
)
759 end_mark
= max (1, edit
->curs1
) - 1;
761 if (edit_search_options
.backwards
)
763 search_end
= end_mark
;
764 while ((int) search_start
>= start_mark
)
766 if (search_end
> (off_t
) (search_start
+ edit
->search
->original_len
) &&
767 mc_search_is_fixed_search_str (edit
->search
))
769 search_end
= search_start
+ edit
->search
->original_len
;
771 if (mc_search_run (edit
->search
, (void *) edit
, search_start
, search_end
, len
)
772 && edit
->search
->normal_offset
== search_start
)
778 edit
->search
->error_str
= g_strdup (_("Search string not found"));
782 return mc_search_run (edit
->search
, (void *) edit
, search_start
, end_mark
, len
);
787 /* --------------------------------------------------------------------------------------------- */
790 edit_replace_cmd__conv_to_display (char *str
)
795 tmp
= str_convert_to_display (str
);
799 return g_string_free (tmp
, FALSE
);
800 g_string_free (tmp
, TRUE
);
803 return g_strdup (str
);
806 /* --------------------------------------------------------------------------------------------- */
809 edit_replace_cmd__conv_to_input (char *str
)
814 tmp
= str_convert_to_input (str
);
818 return g_string_free (tmp
, FALSE
);
819 g_string_free (tmp
, TRUE
);
822 return g_strdup (str
);
825 /* --------------------------------------------------------------------------------------------- */
828 edit_do_search (WEdit
* edit
)
832 if (edit
->search
== NULL
)
833 edit
->search_start
= edit
->curs1
;
835 edit_push_undo_action (edit
, KEY_PRESS
+ edit
->start_display
);
837 if (search_create_bookmark
)
839 int found
= 0, books
= 0;
840 long l
= 0, l_last
= -1;
843 search_create_bookmark
= FALSE
;
844 book_mark_flush (edit
, -1);
848 if (!mc_search_run (edit
->search
, (void *) edit
, q
, edit
->last_byte
, &len
))
851 edit
->search_start
= edit
->search
->normal_offset
;
853 l
+= edit_count_lines (edit
, q
, edit
->search
->normal_offset
);
856 book_mark_insert (edit
, l
, BOOK_MARK_FOUND_COLOR
);
860 q
= edit
->search
->normal_offset
+ 1;
864 edit_error_dialog (_("Search"), _("Search string not found"));
866 edit_cursor_move (edit
, edit
->search_start
- edit
->curs1
);
870 if (edit
->found_len
!= 0 && edit
->search_start
== edit
->found_start
+ 1
871 && edit_search_options
.backwards
)
872 edit
->search_start
--;
874 if (edit
->found_len
!= 0 && edit
->search_start
== edit
->found_start
- 1
875 && !edit_search_options
.backwards
)
876 edit
->search_start
++;
878 if (editcmd_find (edit
, &len
))
880 edit
->found_start
= edit
->search_start
= edit
->search
->normal_offset
;
881 edit
->found_len
= len
;
883 edit_cursor_move (edit
, edit
->search_start
- edit
->curs1
);
884 edit_scroll_screen_over_cursor (edit
);
885 if (edit_search_options
.backwards
)
886 edit
->search_start
--;
888 edit
->search_start
++;
892 edit
->search_start
= edit
->curs1
;
893 if (edit
->search
->error_str
!= NULL
)
894 edit_error_dialog (_("Search"), edit
->search
->error_str
);
898 edit
->force
|= REDRAW_COMPLETELY
;
899 edit_scroll_screen_over_cursor (edit
);
902 /* --------------------------------------------------------------------------------------------- */
905 edit_search (WEdit
* edit
)
907 if (editcmd_dialog_search_show (edit
))
908 edit_do_search (edit
);
911 /* --------------------------------------------------------------------------------------------- */
912 /** Return a null terminated length of text. Result must be g_free'd */
914 static unsigned char *
915 edit_get_block (WEdit
* edit
, long start
, long finish
, int *l
)
917 unsigned char *s
, *r
;
918 r
= s
= g_malloc0 (finish
- start
+ 1);
919 if (edit
->column_highlight
)
922 /* copy from buffer, excluding chars that are out of the column 'margins' */
923 while (start
< finish
)
927 x
= edit_move_forward3 (edit
, edit_bol (edit
, start
), 0, start
);
928 c
= edit_get_byte (edit
, start
);
929 if ((x
>= edit
->column1
&& x
< edit
->column2
)
930 || (x
>= edit
->column2
&& x
< edit
->column1
) || c
== '\n')
941 while (start
< finish
)
942 *s
++ = edit_get_byte (edit
, start
++);
948 /* --------------------------------------------------------------------------------------------- */
949 /** copies a block to clipboard file */
952 edit_save_block_to_clip_file (WEdit
* edit
, long start
, long finish
)
956 tmp
= mc_config_get_full_path (EDIT_CLIP_FILE
);
957 ret
= edit_save_block (edit
, tmp
, start
, finish
);
962 /* --------------------------------------------------------------------------------------------- */
965 pipe_mail (WEdit
* edit
, char *to
, char *subject
, char *cc
)
970 to
= name_quote (to
, 0);
971 subject
= name_quote (subject
, 0);
972 cc
= name_quote (cc
, 0);
973 s
= g_strconcat ("mail -s ", subject
, *cc
? " -c " : "", cc
, " ", to
, (char *) NULL
);
987 for (i
= 0; i
< edit
->last_byte
; i
++)
988 fputc (edit_get_byte (edit
, i
), p
);
993 /* --------------------------------------------------------------------------------------------- */
996 is_break_char (char c
)
998 return (isspace (c
) || strchr ("{}[]()<>=|/\\!?~-+`'\",.;:#$%^&*", c
));
1001 /* --------------------------------------------------------------------------------------------- */
1002 /** find first character of current word */
1005 edit_find_word_start (WEdit
* edit
, long *word_start
, gsize
* word_len
)
1010 /* return if at begin of file */
1011 if (edit
->curs1
<= 0)
1014 c
= (unsigned char) edit_get_byte (edit
, edit
->curs1
- 1);
1015 /* return if not at end or in word */
1016 if (is_break_char (c
))
1019 /* search start of word to be completed */
1022 /* return if at begin of file */
1023 if ((gsize
) edit
->curs1
< i
)
1027 c
= (unsigned char) edit_get_byte (edit
, edit
->curs1
- i
);
1029 if (is_break_char (c
))
1031 /* return if word starts with digit */
1035 *word_start
= edit
->curs1
- (i
- 1); /* start found */
1044 /* --------------------------------------------------------------------------------------------- */
1046 * Get current word under cursor
1048 * @param edit editor object
1049 * @param srch mc_search object
1050 * @param word_start start word position
1052 * @return newly allocated string or NULL if no any words under cursor
1056 edit_collect_completions_get_current_word (WEdit
* edit
, mc_search_t
* srch
, long word_start
)
1061 if (!mc_search_run (srch
, (void *) edit
, word_start
, edit
->last_byte
, &len
))
1064 temp
= g_string_sized_new (len
);
1066 for (i
= 0; i
< len
; i
++)
1070 chr
= edit_get_byte (edit
, word_start
+ i
);
1072 g_string_append_c (temp
, chr
);
1075 return g_string_free (temp
, temp
->len
== 0);
1078 /* --------------------------------------------------------------------------------------------- */
1079 /** collect the possible completions */
1082 edit_collect_completions (WEdit
* edit
, long word_start
, gsize word_len
,
1083 char *match_expr
, struct selection
*compl, gsize
* num
)
1091 long last_byte
, start
= -1;
1094 srch
= mc_search_new (match_expr
, -1);
1098 if (mc_config_get_bool
1099 (mc_main_config
, CONFIG_APP_SECTION
, "editor_wordcompletion_collect_entire_file", 0))
1101 last_byte
= edit
->last_byte
;
1105 last_byte
= word_start
;
1108 srch
->search_type
= MC_SEARCH_T_REGEX
;
1109 srch
->is_case_sensitive
= TRUE
;
1110 srch
->search_fn
= edit_search_cmd_callback
;
1112 current_word
= edit_collect_completions_get_current_word (edit
, srch
, word_start
);
1114 temp
= g_string_new ("");
1116 /* collect max MAX_WORD_COMPLETIONS completions */
1117 while (mc_search_run (srch
, (void *) edit
, start
+ 1, last_byte
, &len
))
1119 g_string_set_size (temp
, 0);
1120 start
= srch
->normal_offset
;
1122 /* add matched completion if not yet added */
1123 for (i
= 0; i
< len
; i
++)
1125 skip
= edit_get_byte (edit
, start
+ i
);
1129 /* skip current word */
1130 if (start
+ (long) i
== word_start
)
1133 g_string_append_c (temp
, skip
);
1139 if (current_word
!= NULL
&& strcmp (current_word
, temp
->str
) == 0)
1144 for (i
= 0; i
< *num
; i
++)
1147 ((char *) &compl[i
].text
[word_len
],
1148 (char *) &temp
->str
[word_len
], max (len
, compl[i
].len
) - word_len
) == 0)
1150 struct selection
this = compl[i
];
1151 for (++i
; i
< *num
; i
++)
1153 compl[i
- 1] = compl[i
];
1155 compl[*num
- 1] = this;
1157 break; /* skip it, already added */
1163 if (*num
== MAX_WORD_COMPLETIONS
)
1165 g_free (compl[0].text
);
1166 for (i
= 1; i
< *num
; i
++)
1168 compl[i
- 1] = compl[i
];
1175 recoded
= str_convert_to_display (temp
->str
);
1177 if (recoded
&& recoded
->len
)
1178 g_string_assign (temp
, recoded
->str
);
1180 g_string_free (recoded
, TRUE
);
1183 compl[*num
].text
= g_strdup (temp
->str
);
1184 compl[*num
].len
= temp
->len
;
1188 /* note the maximal length needed for the completion dialog */
1193 mc_search_free (srch
);
1194 g_string_free (temp
, TRUE
);
1195 g_free (current_word
);
1200 /* --------------------------------------------------------------------------------------------- */
1203 edit_insert_column_of_text (WEdit
* edit
, unsigned char *data
, int size
, int width
,
1204 long *start_pos
, long *end_pos
, int *col1
, int *col2
)
1209 cursor
= edit
->curs1
;
1210 col
= edit_get_col (edit
);
1212 for (i
= 0; i
< size
; i
++)
1214 if (data
[i
] != '\n')
1215 edit_insert (edit
, data
[i
]);
1217 { /* fill in and move to next line */
1221 if (edit_get_byte (edit
, edit
->curs1
) != '\n')
1223 l
= width
- (edit_get_col (edit
) - col
);
1226 edit_insert (edit
, ' ');
1230 for (p
= edit
->curs1
;; p
++)
1232 if (p
== edit
->last_byte
)
1234 edit_cursor_move (edit
, edit
->last_byte
- edit
->curs1
);
1235 edit_insert_ahead (edit
, '\n');
1239 if (edit_get_byte (edit
, p
) == '\n')
1245 edit_cursor_move (edit
, edit_move_forward3 (edit
, p
, col
, 0) - edit
->curs1
);
1246 l
= col
- edit_get_col (edit
);
1247 while (l
>= space_width
)
1249 edit_insert (edit
, ' ');
1256 *col2
= col
+ width
;
1257 *start_pos
= cursor
;
1258 *end_pos
= edit
->curs1
;
1259 edit_cursor_move (edit
, cursor
- edit
->curs1
);
1262 /* --------------------------------------------------------------------------------------------- */
1265 edit_macro_comparator (gconstpointer
* macro1
, gconstpointer
* macro2
)
1267 const macros_t
*m1
= (const macros_t
*) macro1
;
1268 const macros_t
*m2
= (const macros_t
*) macro2
;
1270 return m1
->hotkey
- m2
->hotkey
;
1273 /* --------------------------------------------------------------------------------------------- */
1276 edit_macro_sort_by_hotkey (void)
1278 if (macros_list
!= NULL
&& macros_list
->len
!= 0)
1279 g_array_sort (macros_list
, (GCompareFunc
) edit_macro_comparator
);
1282 /* --------------------------------------------------------------------------------------------- */
1285 edit_get_macro (WEdit
* edit
, int hotkey
, const macros_t
** macros
, guint
* indx
)
1287 const macros_t
*array_start
= &g_array_index (macros_list
, struct macros_t
, 0);
1289 macros_t search_macro
;
1293 search_macro
.hotkey
= hotkey
;
1294 result
= bsearch (&search_macro
, macros_list
->data
, macros_list
->len
,
1295 sizeof (macros_t
), (GCompareFunc
) edit_macro_comparator
);
1297 if (result
!= NULL
&& result
->macro
!= NULL
)
1299 *indx
= (result
- array_start
);
1307 /* --------------------------------------------------------------------------------------------- */
1308 /** returns FALSE on error */
1311 edit_delete_macro (WEdit
* edit
, int hotkey
)
1313 mc_config_t
*macros_config
= NULL
;
1314 const char *section_name
= "editor";
1315 gchar
*macros_fname
;
1318 const macros_t
*macros
= NULL
;
1320 /* clear array of actions for current hotkey */
1321 while (edit_get_macro (edit
, hotkey
, ¯os
, &indx
))
1323 if (macros
->macro
!= NULL
)
1324 g_array_free (macros
->macro
, TRUE
);
1326 g_array_remove_index (macros_list
, indx
);
1327 edit_macro_sort_by_hotkey ();
1330 macros_fname
= mc_config_get_full_path (MC_MACRO_FILE
);
1331 macros_config
= mc_config_init (macros_fname
);
1332 g_free (macros_fname
);
1334 if (macros_config
== NULL
)
1337 skeyname
= lookup_key_by_code (hotkey
);
1338 while (mc_config_del_key (macros_config
, section_name
, skeyname
))
1341 mc_config_save_file (macros_config
, NULL
);
1342 mc_config_deinit (macros_config
);
1347 /* --------------------------------------------------------------------------------------------- */
1348 /*** public functions ****************************************************************************/
1349 /* --------------------------------------------------------------------------------------------- */
1352 edit_help_cmd (WEdit
* edit
)
1354 ev_help_t event_data
= { NULL
, "[Internal File Editor]" };
1355 mc_event_raise (MCEVENT_GROUP_CORE
, "help", &event_data
);
1357 edit
->force
|= REDRAW_COMPLETELY
;
1360 /* --------------------------------------------------------------------------------------------- */
1363 edit_refresh_cmd (WEdit
* edit
)
1368 edit_get_syntax_color (edit
, -1, &color
);
1369 tty_touch_screen ();
1376 #endif /* !HAVE_SLANG */
1380 /* --------------------------------------------------------------------------------------------- */
1383 menu_save_mode_cmd (void)
1386 const int DLG_X
= 38;
1387 const int DLG_Y
= 13;
1391 const char *str
[] = {
1394 N_("&Do backups with following extension:")
1397 QuickWidget widgets
[] = {
1399 QUICK_BUTTON (18, DLG_X
, DLG_Y
- 3, DLG_Y
, N_("&Cancel"), B_CANCEL
, NULL
),
1401 QUICK_BUTTON (6, DLG_X
, DLG_Y
- 3, DLG_Y
, N_("&OK"), B_ENTER
, NULL
),
1403 QUICK_CHECKBOX (4, DLG_X
, 8, DLG_Y
, N_("Check &POSIX new line"), &option_check_nl_at_eof
),
1405 QUICK_INPUT (8, DLG_X
, 6, DLG_Y
, option_backup_ext
, 9, 0, "edit-backup-ext", &str_result
),
1407 QUICK_RADIO (4, DLG_X
, 3, DLG_Y
, 3, str
, &option_save_mode
),
1411 QuickDialog dialog
= {
1412 DLG_X
, DLG_Y
, -1, -1, N_("Edit Save Mode"),
1413 "[Edit Save Mode]", widgets
, NULL
, FALSE
1418 size_t w0
, w1
, b_len
, w3
;
1420 #ifdef HAVE_ASSERT_H
1421 assert (option_backup_ext
!= NULL
);
1424 /* OK/Cancel buttons */
1425 w0
= str_term_width1 (_(widgets
[0].u
.button
.text
)) + 3;
1426 w1
= str_term_width1 (_(widgets
[1].u
.button
.text
)) + 5; /* default button */
1427 b_len
= w0
+ w1
+ 3;
1429 maxlen
= max (b_len
, (size_t) str_term_width1 (_(dialog
.title
)) + 2);
1432 for (i
= 0; i
< 3; i
++)
1437 w3
= max (w3
, (size_t) str_term_width1 (str
[i
]));
1440 maxlen
= max (maxlen
, w3
+ 4);
1442 dialog
.xlen
= min ((size_t) COLS
, maxlen
+ 8);
1444 widgets
[3].u
.input
.len
= w3
;
1445 widgets
[1].relative_x
= (dialog
.xlen
- b_len
) / 2;
1446 widgets
[0].relative_x
= widgets
[1].relative_x
+ w0
+ 2;
1448 for (i
= 0; i
< sizeof (widgets
) / sizeof (widgets
[0]); i
++)
1449 widgets
[i
].x_divisions
= dialog
.xlen
;
1451 if (quick_dialog (&dialog
) != B_CANCEL
)
1453 g_free (option_backup_ext
);
1454 option_backup_ext
= str_result
;
1458 /* --------------------------------------------------------------------------------------------- */
1461 edit_set_filename (WEdit
* edit
, const vfs_path_t
* name_vpath
)
1463 vfs_path_free (edit
->filename_vpath
);
1464 edit
->filename_vpath
= vfs_path_clone (name_vpath
);
1466 if (edit
->dir_vpath
== NULL
)
1467 edit
->dir_vpath
= vfs_path_clone (vfs_get_raw_current_dir ());
1470 /* --------------------------------------------------------------------------------------------- */
1471 /* Here we want to warn the users of overwriting an existing file,
1472 but only if they have made a change to the filename */
1473 /* returns 1 on success */
1475 edit_save_as_cmd (WEdit
* edit
)
1477 /* This heads the 'Save As' dialog box */
1478 vfs_path_t
*exp_vpath
;
1480 int different_filename
= 0;
1482 if (!edit_check_newline (edit
))
1485 exp_vpath
= edit_get_save_file_as (edit
);
1486 edit_push_undo_action (edit
, KEY_PRESS
+ edit
->start_display
);
1488 if (exp_vpath
!= NULL
)
1490 if (vfs_path_len (exp_vpath
) == 0)
1496 if (vfs_path_cmp (edit
->filename_vpath
, exp_vpath
) != 0)
1500 different_filename
= 1;
1501 file
= mc_open (exp_vpath
, O_RDONLY
| O_BINARY
);
1505 /* the file exists */
1507 /* Overwrite the current file or cancel the operation */
1508 if (edit_query_dialog2
1510 _("A file already exists with this name"), _("&Overwrite"), _("&Cancel")))
1515 edit
->stat1
.st_mode
|= S_IWUSR
;
1517 save_lock
= lock_file (exp_vpath
);
1521 /* filenames equal, check if already locked */
1522 if (!edit
->locked
&& !edit
->delete_file
)
1523 save_lock
= lock_file (exp_vpath
);
1526 if (different_filename
)
1529 * Allow user to write into saved (under another name) file
1530 * even if original file had r/o user permissions.
1532 edit
->stat1
.st_mode
|= S_IWRITE
;
1535 rv
= edit_save_file (edit
, exp_vpath
);
1539 /* Succesful, so unlock both files */
1540 if (different_filename
)
1543 unlock_file (exp_vpath
);
1545 edit
->locked
= unlock_file (edit
->filename_vpath
);
1549 if (edit
->locked
|| save_lock
)
1550 edit
->locked
= unlock_file (edit
->filename_vpath
);
1553 edit_set_filename (edit
, exp_vpath
);
1554 if (edit
->lb
!= LB_ASIS
)
1555 edit_reload (edit
, exp_vpath
);
1557 edit
->delete_file
= 0;
1558 if (different_filename
)
1559 edit_load_syntax (edit
, NULL
, edit
->syntax_type
);
1560 vfs_path_free (exp_vpath
);
1561 edit
->force
|= REDRAW_COMPLETELY
;
1564 edit_error_dialog (_("Save as"), get_sys_error (_("Cannot save file")));
1567 /* Failed, so maintain modify (not save) lock */
1569 unlock_file (exp_vpath
);
1576 vfs_path_free (exp_vpath
);
1577 edit
->force
|= REDRAW_COMPLETELY
;
1581 /* {{{ Macro stuff starts here */
1582 /* --------------------------------------------------------------------------------------------- */
1585 edit_delete_macro_cmd (WEdit
* edit
)
1589 hotkey
= editcmd_dialog_raw_key_query (_("Delete macro"), _("Press macro hotkey:"), 1);
1591 if (hotkey
!= 0 && !edit_delete_macro (edit
, hotkey
))
1592 message (D_ERROR
, _("Delete macro"), _("Macro not deleted"));
1595 /* --------------------------------------------------------------------------------------------- */
1597 /** returns FALSE on error */
1599 edit_execute_macro (WEdit
* edit
, int hotkey
)
1601 gboolean res
= FALSE
;
1605 const macros_t
*macros
;
1608 if (edit_get_macro (edit
, hotkey
, ¯os
, &indx
) &&
1609 macros
->macro
!= NULL
&& macros
->macro
->len
!= 0)
1613 edit
->force
|= REDRAW_PAGE
;
1615 for (i
= 0; i
< macros
->macro
->len
; i
++)
1617 const macro_action_t
*m_act
;
1619 m_act
= &g_array_index (macros
->macro
, struct macro_action_t
, i
);
1620 edit_execute_cmd (edit
, m_act
->action
, m_act
->ch
);
1629 /* --------------------------------------------------------------------------------------------- */
1631 /** returns FALSE on error */
1633 edit_store_macro_cmd (WEdit
* edit
)
1637 GString
*marcros_string
;
1638 mc_config_t
*macros_config
= NULL
;
1639 const char *section_name
= "editor";
1640 gchar
*macros_fname
;
1641 GArray
*macros
; /* current macro */
1643 gboolean have_macro
= FALSE
;
1644 char *skeyname
= NULL
;
1646 hotkey
= editcmd_dialog_raw_key_query (_("Save macro"), _("Press the macro's new hotkey:"), 1);
1647 if (hotkey
== ESC_CHAR
)
1650 tmp_act
= keybind_lookup_keymap_command (editor_map
, hotkey
);
1652 /* return FALSE if try assign macro into restricted hotkeys */
1653 if (tmp_act
== CK_MacroStartRecord
1654 || tmp_act
== CK_MacroStopRecord
|| tmp_act
== CK_MacroStartStopRecord
)
1657 edit_delete_macro (edit
, hotkey
);
1659 macros_fname
= mc_config_get_full_path (MC_MACRO_FILE
);
1660 macros_config
= mc_config_init (macros_fname
);
1661 g_free (macros_fname
);
1663 if (macros_config
== NULL
)
1666 edit_push_undo_action (edit
, KEY_PRESS
+ edit
->start_display
);
1668 marcros_string
= g_string_sized_new (250);
1669 macros
= g_array_new (TRUE
, FALSE
, sizeof (macro_action_t
));
1671 skeyname
= lookup_key_by_code (hotkey
);
1673 for (i
= 0; i
< macro_index
; i
++)
1675 macro_action_t m_act
;
1676 const char *action_name
;
1678 action_name
= keybind_lookup_actionname (record_macro_buf
[i
].action
);
1680 if (action_name
== NULL
)
1683 m_act
.action
= record_macro_buf
[i
].action
;
1684 m_act
.ch
= record_macro_buf
[i
].ch
;
1685 g_array_append_val (macros
, m_act
);
1687 g_string_append_printf (marcros_string
, "%s:%i;", action_name
,
1688 (int) record_macro_buf
[i
].ch
);
1693 macro
.hotkey
= hotkey
;
1694 macro
.macro
= macros
;
1695 g_array_append_val (macros_list
, macro
);
1696 mc_config_set_string (macros_config
, section_name
, skeyname
, marcros_string
->str
);
1699 mc_config_del_key (macros_config
, section_name
, skeyname
);
1702 edit_macro_sort_by_hotkey ();
1704 g_string_free (marcros_string
, TRUE
);
1705 mc_config_save_file (macros_config
, NULL
);
1706 mc_config_deinit (macros_config
);
1710 /* --------------------------------------------------------------------------------------------- */
1713 edit_repeat_macro_cmd (WEdit
* edit
)
1720 f
= input_dialog (_("Repeat last commands"), _("Repeat times:"), MC_HISTORY_EDIT_REPEAT
, NULL
);
1721 if (f
== NULL
|| *f
== '\0')
1727 count_repeat
= strtol (f
, &error
, 0);
1737 edit_push_undo_action (edit
, KEY_PRESS
+ edit
->start_display
);
1738 edit
->force
|= REDRAW_PAGE
;
1740 for (j
= 0; j
< count_repeat
; j
++)
1741 for (i
= 0; i
< macro_index
; i
++)
1742 edit_execute_cmd (edit
, record_macro_buf
[i
].action
, record_macro_buf
[i
].ch
);
1743 edit_update_screen (edit
);
1747 /* --------------------------------------------------------------------------------------------- */
1748 /** return FALSE on error */
1751 edit_load_macro_cmd (WEdit
* edit
)
1753 mc_config_t
*macros_config
= NULL
;
1754 gchar
**profile_keys
, **keys
;
1755 gchar
**values
, **curr_values
;
1756 gsize len
, values_len
;
1757 const char *section_name
= "editor";
1758 gchar
*macros_fname
;
1763 macros_fname
= mc_config_get_full_path (MC_MACRO_FILE
);
1764 macros_config
= mc_config_init (macros_fname
);
1765 g_free (macros_fname
);
1767 if (macros_config
== NULL
)
1770 profile_keys
= keys
= mc_config_get_keys (macros_config
, section_name
, &len
);
1771 while (*profile_keys
!= NULL
)
1773 gboolean have_macro
;
1777 macros
= g_array_new (TRUE
, FALSE
, sizeof (macro_action_t
));
1779 curr_values
= values
= mc_config_get_string_list (macros_config
, section_name
,
1780 *profile_keys
, &values_len
);
1781 hotkey
= lookup_key (*profile_keys
, NULL
);
1784 while (*curr_values
!= NULL
&& *curr_values
[0] != '\0')
1786 char **macro_pair
= NULL
;
1788 macro_pair
= g_strsplit (*curr_values
, ":", 2);
1790 if (macro_pair
!= NULL
)
1792 macro_action_t m_act
;
1793 if (macro_pair
[0] == NULL
|| macro_pair
[0][0] == '\0')
1797 m_act
.action
= keybind_lookup_action (macro_pair
[0]);
1798 g_free (macro_pair
[0]);
1799 macro_pair
[0] = NULL
;
1801 if (macro_pair
[1] == NULL
|| macro_pair
[1][0] == '\0')
1805 m_act
.ch
= strtol (macro_pair
[1], NULL
, 0);
1806 g_free (macro_pair
[1]);
1807 macro_pair
[1] = NULL
;
1809 if (m_act
.action
!= 0)
1811 /* a shell command */
1812 if ((m_act
.action
/ CK_PipeBlock (0)) == 1)
1814 m_act
.action
= CK_PipeBlock (0) + (m_act
.ch
> 0 ? m_act
.ch
: 0);
1817 g_array_append_val (macros
, m_act
);
1820 g_strfreev (macro_pair
);
1827 macro
.hotkey
= hotkey
;
1828 macro
.macro
= macros
;
1829 g_array_append_val (macros_list
, macro
);
1832 g_strfreev (values
);
1835 mc_config_deinit (macros_config
);
1836 edit_macro_sort_by_hotkey ();
1840 /* }}} Macro stuff end here */
1842 /* --------------------------------------------------------------------------------------------- */
1843 /** returns 1 on success */
1846 edit_save_confirm_cmd (WEdit
* edit
)
1850 if (edit
->filename_vpath
== NULL
)
1851 return edit_save_as_cmd (edit
);
1853 if (!edit_check_newline (edit
))
1856 if (edit_confirm_save
)
1861 filename
= vfs_path_to_str (edit
->filename_vpath
);
1862 f
= g_strdup_printf (_("Confirm save file: \"%s\""), filename
);
1864 ok
= (edit_query_dialog2 (_("Save file"), f
, _("&Save"), _("&Cancel")) == 0);
1869 return edit_save_cmd (edit
);
1872 /* --------------------------------------------------------------------------------------------- */
1873 /** returns 1 on success */
1876 edit_new_cmd (WEdit
* edit
)
1880 if (edit_query_dialog2
1883 ("Current text was modified without a file save.\nContinue discards these changes"),
1884 _("C&ontinue"), _("&Cancel")))
1886 edit
->force
|= REDRAW_COMPLETELY
;
1890 edit
->force
|= REDRAW_COMPLETELY
;
1892 return edit_renew (edit
); /* if this gives an error, something has really screwed up */
1895 /* --------------------------------------------------------------------------------------------- */
1898 edit_load_cmd (WEdit
* edit
, edit_current_file_t what
)
1901 && (edit_query_dialog2
1903 _("Current text was modified without a file save.\n"
1904 "Continue discards these changes"), _("C&ontinue"), _("&Cancel")) == 1))
1906 edit
->force
|= REDRAW_COMPLETELY
;
1912 case EDIT_FILE_COMMON
:
1914 char *filename
, *exp
;
1916 filename
= vfs_path_to_str (edit
->filename_vpath
);
1917 exp
= input_expand_dialog (_("Load"), _("Enter file name:"),
1918 MC_HISTORY_EDIT_LOAD
, filename
);
1925 vfs_path_t
*exp_vpath
;
1927 exp_vpath
= vfs_path_from_str (exp
);
1928 edit_load_file_from_filename (edit
, exp_vpath
);
1929 vfs_path_free (exp_vpath
);
1936 case EDIT_FILE_SYNTAX
:
1937 edit_load_syntax_file (edit
);
1940 case EDIT_FILE_MENU
:
1941 edit_load_menu_file (edit
);
1948 edit
->force
|= REDRAW_COMPLETELY
;
1952 /* --------------------------------------------------------------------------------------------- */
1954 if mark2 is -1 then marking is from mark1 to the cursor.
1955 Otherwise its between the markers. This handles this.
1956 Returns 1 if no text is marked.
1960 eval_marks (WEdit
* edit
, long *start_mark
, long *end_mark
)
1962 if (edit
->mark1
!= edit
->mark2
)
1964 long start_bol
, start_eol
;
1965 long end_bol
, end_eol
;
1970 if (edit
->end_mark_curs
< 0)
1971 end_mark_curs
= edit
->curs1
;
1973 end_mark_curs
= edit
->end_mark_curs
;
1975 if (edit
->mark2
>= 0)
1977 *start_mark
= min (edit
->mark1
, edit
->mark2
);
1978 *end_mark
= max (edit
->mark1
, edit
->mark2
);
1982 *start_mark
= min (edit
->mark1
, end_mark_curs
);
1983 *end_mark
= max (edit
->mark1
, end_mark_curs
);
1984 edit
->column2
= edit
->curs_col
+ edit
->over_col
;
1987 if (edit
->column_highlight
1988 && (((edit
->mark1
> end_mark_curs
) && (edit
->column1
< edit
->column2
))
1989 || ((edit
->mark1
< end_mark_curs
) && (edit
->column1
> edit
->column2
))))
1991 start_bol
= edit_bol (edit
, *start_mark
);
1992 start_eol
= edit_eol (edit
, start_bol
- 1) + 1;
1993 end_bol
= edit_bol (edit
, *end_mark
);
1994 end_eol
= edit_eol (edit
, *end_mark
);
1995 col1
= min (edit
->column1
, edit
->column2
);
1996 col2
= max (edit
->column1
, edit
->column2
);
1999 edit_move_forward3 (edit
, start_bol
, col2
, 0) - edit_move_forward3 (edit
, start_bol
,
2002 edit_move_forward3 (edit
, end_bol
, col2
, 0) - edit_move_forward3 (edit
, end_bol
,
2005 *start_mark
-= diff1
;
2007 *start_mark
= max (*start_mark
, start_eol
);
2008 *end_mark
= min (*end_mark
, end_eol
);
2014 *start_mark
= *end_mark
= 0;
2015 edit
->column2
= edit
->column1
= 0;
2020 /* --------------------------------------------------------------------------------------------- */
2023 edit_insert_over (WEdit
* edit
)
2027 for (i
= 0; i
< edit
->over_col
; i
++)
2029 edit_insert (edit
, ' ');
2034 /* --------------------------------------------------------------------------------------------- */
2037 edit_insert_column_of_text_from_file (WEdit
* edit
, int file
,
2038 long *start_pos
, long *end_pos
, int *col1
, int *col2
)
2042 int blocklen
= -1, width
= 0;
2043 unsigned char *data
;
2045 cursor
= edit
->curs1
;
2046 col
= edit_get_col (edit
);
2047 data
= g_malloc0 (TEMP_BUF_LEN
);
2049 while ((blocklen
= mc_read (file
, (char *) data
, TEMP_BUF_LEN
)) > 0)
2052 for (width
= 0; width
< blocklen
; width
++)
2054 if (data
[width
] == '\n')
2057 for (i
= 0; i
< blocklen
; i
++)
2059 if (data
[i
] == '\n')
2060 { /* fill in and move to next line */
2063 if (edit_get_byte (edit
, edit
->curs1
) != '\n')
2065 l
= width
- (edit_get_col (edit
) - col
);
2068 edit_insert (edit
, ' ');
2072 for (p
= edit
->curs1
;; p
++)
2074 if (p
== edit
->last_byte
)
2076 edit_cursor_move (edit
, edit
->last_byte
- edit
->curs1
);
2077 edit_insert_ahead (edit
, '\n');
2081 if (edit_get_byte (edit
, p
) == '\n')
2087 edit_cursor_move (edit
, edit_move_forward3 (edit
, p
, col
, 0) - edit
->curs1
);
2088 l
= col
- edit_get_col (edit
);
2089 while (l
>= space_width
)
2091 edit_insert (edit
, ' ');
2096 edit_insert (edit
, data
[i
]);
2100 *col2
= col
+ width
;
2101 *start_pos
= cursor
;
2102 *end_pos
= edit
->curs1
;
2103 edit_cursor_move (edit
, cursor
- edit
->curs1
);
2109 /* --------------------------------------------------------------------------------------------- */
2112 edit_block_copy_cmd (WEdit
* edit
)
2114 long start_mark
, end_mark
, current
= edit
->curs1
;
2119 unsigned char *copy_buf
;
2121 edit_update_curs_col (edit
);
2122 if (eval_marks (edit
, &start_mark
, &end_mark
))
2125 copy_buf
= edit_get_block (edit
, start_mark
, end_mark
, &size
);
2127 /* all that gets pushed are deletes hence little space is used on the stack */
2129 edit_push_markers (edit
);
2131 if (edit
->column_highlight
)
2133 col_delta
= abs (edit
->column2
- edit
->column1
);
2134 edit_insert_column_of_text (edit
, copy_buf
, size
, col_delta
, &mark1
, &mark2
, &c1
, &c2
);
2139 edit_insert_ahead (edit
, copy_buf
[size
]);
2143 edit_scroll_screen_over_cursor (edit
);
2145 if (edit
->column_highlight
)
2146 edit_set_markers (edit
, edit
->curs1
, mark2
, c1
, c2
);
2147 else if (start_mark
< current
&& end_mark
> current
)
2148 edit_set_markers (edit
, start_mark
, end_mark
+ end_mark
- start_mark
, 0, 0);
2150 edit
->force
|= REDRAW_PAGE
;
2154 /* --------------------------------------------------------------------------------------------- */
2157 edit_block_move_cmd (WEdit
* edit
)
2160 unsigned char *copy_buf
= NULL
;
2161 long start_mark
, end_mark
;
2164 if (eval_marks (edit
, &start_mark
, &end_mark
))
2167 line
= edit
->curs_line
;
2168 if (edit
->mark2
< 0)
2169 edit_mark_cmd (edit
, 0);
2170 edit_push_markers (edit
);
2172 if (edit
->column_highlight
)
2180 c1
= min (edit
->column1
, edit
->column2
);
2181 c2
= max (edit
->column1
, edit
->column2
);
2182 b_width
= (c2
- c1
);
2184 edit_update_curs_col (edit
);
2187 x2
= x
+ edit
->over_col
;
2189 /* do nothing when cursor inside first line of selected area */
2190 if ((edit_eol (edit
, edit
->curs1
) == edit_eol (edit
, start_mark
)) && (x2
> c1
&& x2
<= c2
))
2193 if (edit
->curs1
> start_mark
&& edit
->curs1
< edit_eol (edit
, end_mark
))
2197 else if (x
> c1
&& x
<= c2
)
2200 /* save current selection into buffer */
2201 copy_buf
= edit_get_block (edit
, start_mark
, end_mark
, &size
);
2203 /* remove current selection */
2204 edit_block_delete_cmd (edit
);
2206 edit
->over_col
= max (0, edit
->over_col
- b_width
);
2207 /* calculate the cursor pos after delete block */
2208 current
= edit_move_forward3 (edit
, edit_bol (edit
, edit
->curs1
), x
, 0);
2209 edit_cursor_move (edit
, current
- edit
->curs1
);
2210 edit_scroll_screen_over_cursor (edit
);
2212 /* add TWS if need before block insertion */
2213 if (option_cursor_beyond_eol
&& edit
->over_col
> 0)
2214 edit_insert_over (edit
);
2216 edit_insert_column_of_text (edit
, copy_buf
, size
, b_width
, &mark1
, &mark2
, &c1
, &c2
);
2217 edit_set_markers (edit
, mark1
, mark2
, c1
, c2
);
2223 current
= edit
->curs1
;
2224 copy_buf
= g_malloc0 (end_mark
- start_mark
);
2225 edit_cursor_move (edit
, start_mark
- edit
->curs1
);
2226 edit_scroll_screen_over_cursor (edit
);
2228 while (count
< end_mark
)
2230 copy_buf
[end_mark
- count
- 1] = edit_delete (edit
, 1);
2233 edit_scroll_screen_over_cursor (edit
);
2234 edit_cursor_move (edit
,
2235 current
- edit
->curs1
-
2236 (((current
- edit
->curs1
) > 0) ? end_mark
- start_mark
: 0));
2237 edit_scroll_screen_over_cursor (edit
);
2238 while (count
-- > start_mark
)
2239 edit_insert_ahead (edit
, copy_buf
[end_mark
- count
- 1]);
2240 edit_set_markers (edit
, edit
->curs1
, edit
->curs1
+ end_mark
- start_mark
, 0, 0);
2243 edit_scroll_screen_over_cursor (edit
);
2245 edit
->force
|= REDRAW_PAGE
;
2248 /* --------------------------------------------------------------------------------------------- */
2249 /** returns 1 if canceelled by user */
2252 edit_block_delete_cmd (WEdit
* edit
)
2254 long start_mark
, end_mark
;
2255 if (eval_marks (edit
, &start_mark
, &end_mark
))
2257 edit_delete_line (edit
);
2260 return edit_block_delete (edit
);
2263 /* --------------------------------------------------------------------------------------------- */
2264 /** call with edit = 0 before shutdown to close memory leaks */
2267 edit_replace_cmd (WEdit
* edit
, int again
)
2269 /* 1 = search string, 2 = replace with */
2270 static char *saved1
= NULL
; /* saved default[123] */
2271 static char *saved2
= NULL
;
2272 char *input1
= NULL
; /* user input from the dialog */
2273 char *input2
= NULL
;
2276 long times_replaced
= 0;
2277 gboolean once_found
= FALSE
;
2281 g_free (saved1
), saved1
= NULL
;
2282 g_free (saved2
), saved2
= NULL
;
2286 edit
->force
|= REDRAW_COMPLETELY
;
2288 if (again
&& !saved1
&& !saved2
)
2293 input1
= g_strdup (saved1
? saved1
: "");
2294 input2
= g_strdup (saved2
? saved2
: "");
2298 char *tmp_inp1
, *tmp_inp2
;
2299 disp1
= edit_replace_cmd__conv_to_display (saved1
? saved1
: (char *) "");
2300 disp2
= edit_replace_cmd__conv_to_display (saved2
? saved2
: (char *) "");
2302 edit_push_undo_action (edit
, KEY_PRESS
+ edit
->start_display
);
2304 editcmd_dialog_replace_show (edit
, disp1
, disp2
, &input1
, &input2
);
2309 if (input1
== NULL
|| *input1
== '\0')
2311 edit
->force
= REDRAW_COMPLETELY
;
2317 input1
= edit_replace_cmd__conv_to_input (input1
);
2318 input2
= edit_replace_cmd__conv_to_input (input2
);
2322 g_free (saved1
), saved1
= g_strdup (input1
);
2323 g_free (saved2
), saved2
= g_strdup (input2
);
2325 mc_search_free (edit
->search
);
2326 edit
->search
= NULL
;
2331 edit
->search
= mc_search_new (input1
, -1);
2332 if (edit
->search
== NULL
)
2334 edit
->search_start
= edit
->curs1
;
2337 edit
->search
->search_type
= edit_search_options
.type
;
2338 edit
->search
->is_all_charsets
= edit_search_options
.all_codepages
;
2339 edit
->search
->is_case_sensitive
= edit_search_options
.case_sens
;
2340 edit
->search
->whole_words
= edit_search_options
.whole_words
;
2341 edit
->search
->search_fn
= edit_search_cmd_callback
;
2344 if (edit
->found_len
&& edit
->search_start
== edit
->found_start
+ 1
2345 && edit_search_options
.backwards
)
2346 edit
->search_start
--;
2348 if (edit
->found_len
&& edit
->search_start
== edit
->found_start
- 1
2349 && !edit_search_options
.backwards
)
2350 edit
->search_start
++;
2356 if (!editcmd_find (edit
, &len
))
2358 if (!(edit
->search
->error
== MC_SEARCH_E_OK
||
2359 (once_found
&& edit
->search
->error
== MC_SEARCH_E_NOTFOUND
)))
2361 edit_error_dialog (_("Search"), edit
->search
->error_str
);
2367 edit
->search_start
= edit
->search
->normal_offset
;
2368 /*returns negative on not found or error in pattern */
2370 if ((edit
->search_start
>= 0) && (edit
->search_start
< edit
->last_byte
))
2373 GString
*tmp_str
, *repl_str
;
2375 edit
->found_start
= edit
->search_start
;
2376 i
= edit
->found_len
= len
;
2378 edit_cursor_move (edit
, edit
->search_start
- edit
->curs1
);
2379 edit_scroll_screen_over_cursor (edit
);
2381 if (edit
->replace_mode
== 0)
2386 l
= edit
->curs_row
- edit
->widget
.lines
/ 3;
2388 edit_scroll_downward (edit
, l
);
2390 edit_scroll_upward (edit
, -l
);
2392 edit_scroll_screen_over_cursor (edit
);
2393 edit
->force
|= REDRAW_PAGE
;
2394 edit_render_keypress (edit
);
2396 /*so that undo stops at each query */
2397 edit_push_key_press (edit
);
2398 /* and prompt 2/3 down */
2399 disp1
= edit_replace_cmd__conv_to_display (saved1
);
2400 disp2
= edit_replace_cmd__conv_to_display (saved2
);
2401 prompt
= editcmd_dialog_replace_prompt_show (edit
, disp1
, disp2
, -1, -1);
2405 if (prompt
== B_REPLACE_ALL
)
2406 edit
->replace_mode
= 1;
2407 else if (prompt
== B_SKIP_REPLACE
)
2409 if (edit_search_options
.backwards
)
2410 edit
->search_start
--;
2412 edit
->search_start
++;
2413 continue; /* loop */
2415 else if (prompt
== B_CANCEL
)
2417 edit
->replace_mode
= -1;
2422 /* don't process string each time */
2423 tmp_str
= g_string_new (input2
);
2424 repl_str
= mc_search_prepare_replace_str (edit
->search
, tmp_str
);
2425 g_string_free (tmp_str
, TRUE
);
2427 if (edit
->search
->error
!= MC_SEARCH_E_OK
)
2429 edit_error_dialog (_("Replace"), edit
->search
->error_str
);
2430 g_string_free (repl_str
, TRUE
);
2434 /* delete then insert new */
2435 for (i
= 0; i
< len
; i
++)
2436 edit_delete (edit
, 1);
2438 for (i
= 0; i
< repl_str
->len
; i
++)
2439 edit_insert (edit
, repl_str
->str
[i
]);
2441 edit
->found_len
= repl_str
->len
;
2442 g_string_free (repl_str
, TRUE
);
2445 /* so that we don't find the same string again */
2446 if (edit_search_options
.backwards
)
2447 edit
->search_start
--;
2450 edit
->search_start
+= edit
->found_len
;
2452 if (edit
->search_start
>= edit
->last_byte
)
2456 edit_scroll_screen_over_cursor (edit
);
2460 /* try and find from right here for next search */
2461 edit
->search_start
= edit
->curs1
;
2462 edit_update_curs_col (edit
);
2464 edit
->force
|= REDRAW_PAGE
;
2465 edit_render_keypress (edit
);
2467 if (times_replaced
== 0)
2468 query_dialog (_("Replace"), _("Search string not found"), D_NORMAL
, 1, _("&OK"));
2472 while (edit
->replace_mode
>= 0);
2474 edit_scroll_screen_over_cursor (edit
);
2475 edit
->force
|= REDRAW_COMPLETELY
;
2476 edit_render_keypress (edit
);
2478 if ((edit
->replace_mode
== 1) && (times_replaced
!= 0))
2479 message (D_NORMAL
, _("Replace"), _("%ld replacements made"), times_replaced
);
2486 /* --------------------------------------------------------------------------------------------- */
2489 edit_search_cmd_callback (const void *user_data
, gsize char_offset
)
2491 return edit_get_byte ((WEdit
*) user_data
, (long) char_offset
);
2494 /* --------------------------------------------------------------------------------------------- */
2497 edit_search_cmd (WEdit
* edit
, gboolean again
)
2504 else if (edit
->last_search_string
!= NULL
)
2505 edit_do_search (edit
);
2508 /* find last search string in history */
2511 history
= history_get (MC_HISTORY_SHARED_SEARCH
);
2512 if (history
!= NULL
&& history
->data
!= NULL
)
2514 edit
->last_search_string
= (char *) history
->data
;
2515 history
->data
= NULL
;
2516 history
= g_list_first (history
);
2517 g_list_foreach (history
, (GFunc
) g_free
, NULL
);
2518 g_list_free (history
);
2520 edit
->search
= mc_search_new (edit
->last_search_string
, -1);
2521 if (edit
->search
== NULL
)
2523 /* if not... then ask for an expression */
2524 g_free (edit
->last_search_string
);
2525 edit
->last_search_string
= NULL
;
2530 edit
->search
->search_type
= edit_search_options
.type
;
2531 edit
->search
->is_all_charsets
= edit_search_options
.all_codepages
;
2532 edit
->search
->is_case_sensitive
= edit_search_options
.case_sens
;
2533 edit
->search
->whole_words
= edit_search_options
.whole_words
;
2534 edit
->search
->search_fn
= edit_search_cmd_callback
;
2535 edit_do_search (edit
);
2540 /* if not... then ask for an expression */
2541 g_free (edit
->last_search_string
);
2542 edit
->last_search_string
= NULL
;
2549 /* --------------------------------------------------------------------------------------------- */
2551 * Check if it's OK to close the editor. If there are unsaved changes,
2552 * ask user. Return 1 if it's OK to exit, 0 to continue editing.
2556 edit_ok_to_exit (WEdit
* edit
)
2560 if (!edit
->modified
)
2563 if (!mc_global
.widget
.midnight_shutdown
)
2565 if (!edit_check_newline (edit
))
2569 act
= edit_query_dialog3 (_("Quit"), _("File was modified. Save with exit?"),
2570 _("&Yes"), _("&No"), _("&Cancel quit"));
2575 edit_query_dialog2 (_("Quit"),
2576 _("Midnight Commander is being shut down.\nSave modified file?"),
2577 _("&Yes"), _("&No"));
2587 edit_push_markers (edit
);
2588 edit_set_markers (edit
, 0, 0, 0, 0);
2589 if (!edit_save_cmd (edit
) || mc_global
.widget
.midnight_shutdown
)
2590 return mc_global
.widget
.midnight_shutdown
;
2594 case 2: /* Cancel quit */
2602 /* --------------------------------------------------------------------------------------------- */
2603 /** save block, returns 1 on success */
2606 edit_save_block (WEdit
* edit
, const char *filename
, long start
, long finish
)
2611 vpath
= vfs_path_from_str (filename
);
2612 file
= mc_open (vpath
, O_CREAT
| O_WRONLY
| O_TRUNC
,
2613 S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IROTH
| O_BINARY
);
2614 vfs_path_free (vpath
);
2618 if (edit
->column_highlight
)
2622 r
= mc_write (file
, VERTICAL_MAGIC
, sizeof (VERTICAL_MAGIC
));
2625 unsigned char *block
, *p
;
2627 p
= block
= edit_get_block (edit
, start
, finish
, &len
);
2630 r
= mc_write (file
, p
, len
);
2644 len
= finish
- start
;
2645 buf
= g_malloc0 (TEMP_BUF_LEN
);
2646 while (start
!= finish
)
2648 end
= min (finish
, start
+ TEMP_BUF_LEN
);
2649 for (; i
< end
; i
++)
2650 buf
[i
- start
] = edit_get_byte (edit
, i
);
2651 len
-= mc_write (file
, (char *) buf
, end
- start
);
2662 /* --------------------------------------------------------------------------------------------- */
2665 edit_paste_from_history (WEdit
* edit
)
2668 edit_error_dialog (_("Error"), _("This function is not implemented"));
2671 /* --------------------------------------------------------------------------------------------- */
2674 edit_copy_to_X_buf_cmd (WEdit
* edit
)
2676 long start_mark
, end_mark
;
2677 if (eval_marks (edit
, &start_mark
, &end_mark
))
2679 if (!edit_save_block_to_clip_file (edit
, start_mark
, end_mark
))
2681 edit_error_dialog (_("Copy to clipboard"), get_sys_error (_("Unable to save to file")));
2684 /* try use external clipboard utility */
2685 mc_event_raise (MCEVENT_GROUP_CORE
, "clipboard_file_to_ext_clip", NULL
);
2690 /* --------------------------------------------------------------------------------------------- */
2693 edit_cut_to_X_buf_cmd (WEdit
* edit
)
2695 long start_mark
, end_mark
;
2696 if (eval_marks (edit
, &start_mark
, &end_mark
))
2698 if (!edit_save_block_to_clip_file (edit
, start_mark
, end_mark
))
2700 edit_error_dialog (_("Cut to clipboard"), _("Unable to save to file"));
2703 /* try use external clipboard utility */
2704 mc_event_raise (MCEVENT_GROUP_CORE
, "clipboard_file_to_ext_clip", NULL
);
2706 edit_block_delete_cmd (edit
);
2707 edit_mark_cmd (edit
, 1);
2711 /* --------------------------------------------------------------------------------------------- */
2714 edit_paste_from_X_buf_cmd (WEdit
* edit
)
2718 /* try use external clipboard utility */
2719 mc_event_raise (MCEVENT_GROUP_CORE
, "clipboard_file_from_ext_clip", NULL
);
2720 tmp
= mc_config_get_full_vpath (EDIT_CLIP_FILE
);
2721 edit_insert_file (edit
, tmp
);
2722 vfs_path_free (tmp
);
2726 /* --------------------------------------------------------------------------------------------- */
2728 * Ask user for the line and go to that line.
2729 * Negative numbers mean line from the end (i.e. -1 is the last line).
2733 edit_goto_cmd (WEdit
* edit
)
2736 static long line
= 0; /* line as typed, saved as default */
2741 g_snprintf (s
, sizeof (s
), "%ld", line
);
2742 f
= input_dialog (_("Goto line"), _("Enter line:"), MC_HISTORY_EDIT_GOTO_LINE
, line
? s
: "");
2752 l
= strtol (f
, &error
, 0);
2761 l
= edit
->total_lines
+ l
+ 2;
2762 edit_move_display (edit
, l
- edit
->widget
.lines
/ 2 - 1);
2763 edit_move_to_line (edit
, l
- 1);
2764 edit
->force
|= REDRAW_COMPLETELY
;
2769 /* --------------------------------------------------------------------------------------------- */
2770 /** Return 1 on success */
2773 edit_save_block_cmd (WEdit
* edit
)
2775 long start_mark
, end_mark
;
2778 if (eval_marks (edit
, &start_mark
, &end_mark
))
2781 tmp
= mc_config_get_full_path (EDIT_CLIP_FILE
);
2783 input_expand_dialog (_("Save block"), _("Enter file name:"),
2784 MC_HISTORY_EDIT_SAVE_BLOCK
, tmp
);
2786 edit_push_undo_action (edit
, KEY_PRESS
+ edit
->start_display
);
2796 if (edit_save_block (edit
, exp
, start_mark
, end_mark
))
2799 edit
->force
|= REDRAW_COMPLETELY
;
2805 edit_error_dialog (_("Save block"), get_sys_error (_("Cannot save file")));
2809 edit
->force
|= REDRAW_COMPLETELY
;
2814 /* --------------------------------------------------------------------------------------------- */
2815 /** returns TRUE on success */
2818 edit_insert_file_cmd (WEdit
* edit
)
2822 gboolean ret
= FALSE
;
2824 tmp
= mc_config_get_full_path (EDIT_CLIP_FILE
);
2825 exp
= input_expand_dialog (_("Insert file"), _("Enter file name:"),
2826 MC_HISTORY_EDIT_INSERT_FILE
, tmp
);
2829 edit_push_undo_action (edit
, KEY_PRESS
+ edit
->start_display
);
2831 if (exp
!= NULL
&& *exp
!= '\0')
2833 vfs_path_t
*exp_vpath
;
2835 exp_vpath
= vfs_path_from_str (exp
);
2836 ret
= (edit_insert_file (edit
, exp_vpath
) >= 0);
2837 vfs_path_free (exp_vpath
);
2840 edit_error_dialog (_("Insert file"), get_sys_error (_("Cannot insert file")));
2845 edit
->force
|= REDRAW_COMPLETELY
;
2849 /* --------------------------------------------------------------------------------------------- */
2850 /** sorts a block, returns -1 on system fail, 1 on cancel and 0 on success */
2853 edit_sort_cmd (WEdit
* edit
)
2855 static char *old
= 0;
2856 char *exp
, *tmp
, *tmp_edit_block_name
, *tmp_edit_temp_name
;
2857 long start_mark
, end_mark
;
2860 if (eval_marks (edit
, &start_mark
, &end_mark
))
2862 edit_error_dialog (_("Sort block"), _("You must first highlight a block of text"));
2866 tmp
= mc_config_get_full_path (EDIT_BLOCK_FILE
);
2867 edit_save_block (edit
, tmp
, start_mark
, end_mark
);
2870 exp
= input_dialog (_("Run sort"),
2871 _("Enter sort options (see manpage) separated by whitespace:"),
2872 MC_HISTORY_EDIT_SORT
, (old
!= NULL
) ? old
: "");
2878 tmp_edit_block_name
= mc_config_get_full_path (EDIT_BLOCK_FILE
);
2879 tmp_edit_temp_name
= mc_config_get_full_path (EDIT_TEMP_FILE
);
2881 g_strconcat (" sort ", exp
, " ", tmp_edit_block_name
,
2882 " > ", tmp_edit_temp_name
, (char *) NULL
);
2883 g_free (tmp_edit_temp_name
);
2884 g_free (tmp_edit_block_name
);
2890 if (e
== -1 || e
== 127)
2892 edit_error_dialog (_("Sort"), get_sys_error (_("Cannot execute sort command")));
2897 sprintf (q
, "%d ", e
);
2898 tmp
= g_strdup_printf (_("Sort returned non-zero: %s"), q
);
2899 edit_error_dialog (_("Sort"), tmp
);
2905 edit
->force
|= REDRAW_COMPLETELY
;
2907 if (edit_block_delete_cmd (edit
))
2911 vfs_path_t
*tmp_vpath
;
2913 tmp_vpath
= mc_config_get_full_vpath (EDIT_TEMP_FILE
);
2914 edit_insert_file (edit
, tmp_vpath
);
2915 vfs_path_free (tmp_vpath
);
2920 /* --------------------------------------------------------------------------------------------- */
2922 * Ask user for a command, execute it and paste its output back to the
2927 edit_ext_cmd (WEdit
* edit
)
2929 char *exp
, *tmp
, *tmp_edit_temp_file
;
2933 input_dialog (_("Paste output of external command"),
2934 _("Enter shell command(s):"), MC_HISTORY_EDIT_PASTE_EXTCMD
, NULL
);
2939 tmp_edit_temp_file
= mc_config_get_full_path (EDIT_TEMP_FILE
);
2940 tmp
= g_strconcat (exp
, " > ", tmp_edit_temp_file
, (char *) NULL
);
2941 g_free (tmp_edit_temp_file
);
2948 edit_error_dialog (_("External command"), get_sys_error (_("Cannot execute command")));
2952 edit
->force
|= REDRAW_COMPLETELY
;
2955 vfs_path_t
*tmp_vpath
;
2957 tmp_vpath
= mc_config_get_full_vpath (EDIT_TEMP_FILE
);
2958 edit_insert_file (edit
, tmp_vpath
);
2959 vfs_path_free (tmp_vpath
);
2964 /* --------------------------------------------------------------------------------------------- */
2965 /** if block is 1, a block must be highlighted and the shell command
2966 processes it. If block is 0 the shell command is a straight system
2967 command, that just produces some output which is to be inserted */
2970 edit_block_process_cmd (WEdit
* edit
, int macro_number
)
2973 char *macros_fname
= NULL
;
2975 fname
= g_strdup_printf ("%s.%i.sh", MC_EXTMACRO_FILE
, macro_number
);
2976 macros_fname
= g_build_filename (mc_config_get_data_path (), fname
, (char *) NULL
);
2977 user_menu (edit
, macros_fname
, 0);
2979 g_free (macros_fname
);
2980 edit
->force
|= REDRAW_COMPLETELY
;
2983 /* --------------------------------------------------------------------------------------------- */
2986 edit_mail_dialog (WEdit
* edit
)
2989 char *tmail_subject
;
2992 static char *mail_cc_last
= 0;
2993 static char *mail_subject_last
= 0;
2994 static char *mail_to_last
= 0;
2996 QuickWidget quick_widgets
[] = {
2997 /* 0 */ QUICK_BUTTON (6, 10, 9, MAIL_DLG_HEIGHT
, N_("&Cancel"), B_CANCEL
, NULL
),
2998 /* 1 */ QUICK_BUTTON (2, 10, 9, MAIL_DLG_HEIGHT
, N_("&OK"), B_ENTER
, NULL
),
2999 /* 2 */ QUICK_INPUT (3, 50, 8, MAIL_DLG_HEIGHT
, "", 44, 0, "mail-dlg-input", &tmail_cc
),
3000 /* 3 */ QUICK_LABEL (3, 50, 7, MAIL_DLG_HEIGHT
, N_("Copies to")),
3001 /* 4 */ QUICK_INPUT (3, 50, 6, MAIL_DLG_HEIGHT
, "", 44, 0, "mail-dlg-input-2",
3003 /* 5 */ QUICK_LABEL (3, 50, 5, MAIL_DLG_HEIGHT
, N_("Subject")),
3004 /* 6 */ QUICK_INPUT (3, 50, 4, MAIL_DLG_HEIGHT
, "", 44, 0, "mail-dlg-input-3", &tmail_to
),
3005 /* 7 */ QUICK_LABEL (3, 50, 3, MAIL_DLG_HEIGHT
, N_("To")),
3006 /* 8 */ QUICK_LABEL (3, 50, 2, MAIL_DLG_HEIGHT
, N_("mail -s <subject> -c <cc> <to>")),
3010 QuickDialog Quick_input
= {
3011 50, MAIL_DLG_HEIGHT
, -1, -1, N_("Mail"),
3012 "[Input Line Keys]", quick_widgets
, NULL
, FALSE
3015 quick_widgets
[2].u
.input
.text
= mail_cc_last
? mail_cc_last
: "";
3016 quick_widgets
[4].u
.input
.text
= mail_subject_last
? mail_subject_last
: "";
3017 quick_widgets
[6].u
.input
.text
= mail_to_last
? mail_to_last
: "";
3019 if (quick_dialog (&Quick_input
) != B_CANCEL
)
3021 g_free (mail_cc_last
);
3022 g_free (mail_subject_last
);
3023 g_free (mail_to_last
);
3024 mail_cc_last
= tmail_cc
;
3025 mail_subject_last
= tmail_subject
;
3026 mail_to_last
= tmail_to
;
3027 pipe_mail (edit
, mail_to_last
, mail_subject_last
, mail_cc_last
);
3032 /*******************/
3033 /* Word Completion */
3034 /*******************/
3036 /* --------------------------------------------------------------------------------------------- */
3038 * Complete current word using regular expression search
3039 * backwards beginning at the current cursor position.
3043 edit_complete_word_cmd (WEdit
* edit
)
3045 gsize i
, max_len
, word_len
= 0, num_compl
= 0;
3046 long word_start
= 0;
3047 unsigned char *bufpos
;
3049 struct selection
compl[MAX_WORD_COMPLETIONS
]; /* completions */
3051 /* search start of word to be completed */
3052 if (!edit_find_word_start (edit
, &word_start
, &word_len
))
3055 /* prepare match expression */
3056 bufpos
= &edit
->buffers1
[word_start
>> S_EDIT_BUF_SIZE
][word_start
& M_EDIT_BUF_SIZE
];
3058 /* match_expr = g_strdup_printf ("\\b%.*s[a-zA-Z_0-9]+", word_len, bufpos); */
3061 ("(^|\\s+|\\b)%.*s[^\\s\\.=\\+\\[\\]\\(\\)\\,\\;\\:\\\"\\'\\-\\?\\/\\|\\\\\\{\\}\\*\\&\\^\\%%\\$#@\\!]+",
3062 (int) word_len
, bufpos
);
3064 /* collect the possible completions */
3065 /* start search from begin to end of file */
3067 edit_collect_completions (edit
, word_start
, word_len
, match_expr
,
3068 (struct selection
*) &compl, &num_compl
);
3072 /* insert completed word if there is only one match */
3075 for (i
= word_len
; i
< compl[0].len
; i
++)
3076 edit_insert (edit
, *(compl[0].text
+ i
));
3078 /* more than one possible completion => ask the user */
3081 /* !!! usually only a beep is expected and when <ALT-TAB> is !!! */
3082 /* !!! pressed again the selection dialog pops up, but that !!! */
3083 /* !!! seems to require a further internal state !!! */
3086 /* let the user select the preferred completion */
3087 editcmd_dialog_completion_show (edit
, max_len
, word_len
,
3088 (struct selection
*) &compl, num_compl
);
3092 g_free (match_expr
);
3093 /* release memory before return */
3094 for (i
= 0; i
< num_compl
; i
++)
3095 g_free (compl[i
].text
);
3098 /* --------------------------------------------------------------------------------------------- */
3101 edit_select_codepage_cmd (WEdit
* edit
)
3104 if (do_select_codepage ())
3105 edit_set_codeset (edit
);
3107 edit
->force
= REDRAW_COMPLETELY
;
3108 edit_refresh_cmd (edit
);
3114 /* --------------------------------------------------------------------------------------------- */
3117 edit_insert_literal_cmd (WEdit
* edit
)
3119 int char_for_insertion
= editcmd_dialog_raw_key_query (_("Insert literal"),
3120 _("Press any key:"), 0);
3121 edit_execute_key_command (edit
, -1, ascii_alpha_to_cntrl (char_for_insertion
));
3124 /* --------------------------------------------------------------------------------------------- */
3127 edit_begin_end_macro_cmd (WEdit
* edit
)
3129 /* edit is a pointer to the widget */
3132 unsigned long command
= macro_index
< 0 ? CK_MacroStartRecord
: CK_MacroStopRecord
;
3133 edit_execute_key_command (edit
, command
, -1);
3137 /* --------------------------------------------------------------------------------------------- */
3140 edit_begin_end_repeat_cmd (WEdit
* edit
)
3142 /* edit is a pointer to the widget */
3145 unsigned long command
= macro_index
< 0 ? CK_RepeatStartRecord
: CK_RepeatStopRecord
;
3146 edit_execute_key_command (edit
, command
, -1);
3150 /* --------------------------------------------------------------------------------------------- */
3153 edit_load_forward_cmd (WEdit
* edit
)
3157 if (edit_query_dialog2
3159 _("Current text was modified without a file save\n"
3160 "Continue discards these changes"), _("C&ontinue"), _("&Cancel")))
3162 edit
->force
|= REDRAW_COMPLETELY
;
3166 if (edit_stack_iterator
+ 1 < MAX_HISTORY_MOVETO
)
3168 if (edit_history_moveto
[edit_stack_iterator
+ 1].line
< 1)
3172 edit_stack_iterator
++;
3173 if (edit_history_moveto
[edit_stack_iterator
].filename_vpath
)
3175 edit_reload_line (edit
, edit_history_moveto
[edit_stack_iterator
].filename_vpath
,
3176 edit_history_moveto
[edit_stack_iterator
].line
);
3190 /* --------------------------------------------------------------------------------------------- */
3193 edit_load_back_cmd (WEdit
* edit
)
3197 if (edit_query_dialog2
3199 _("Current text was modified without a file save\n"
3200 "Continue discards these changes"), _("C&ontinue"), _("&Cancel")))
3202 edit
->force
|= REDRAW_COMPLETELY
;
3206 if (edit_stack_iterator
> 0)
3208 edit_stack_iterator
--;
3209 if (edit_history_moveto
[edit_stack_iterator
].filename_vpath
)
3211 edit_reload_line (edit
, edit_history_moveto
[edit_stack_iterator
].filename_vpath
,
3212 edit_history_moveto
[edit_stack_iterator
].line
);
3226 /* --------------------------------------------------------------------------------------------- */
3229 edit_get_match_keyword_cmd (WEdit
* edit
)
3231 gsize word_len
= 0, max_len
= 0;
3234 long word_start
= 0;
3235 unsigned char *bufpos
;
3239 char *tagfile
= NULL
;
3241 etags_hash_t def_hash
[MAX_DEFINITIONS
];
3243 for (i
= 0; i
< MAX_DEFINITIONS
; i
++)
3245 def_hash
[i
].filename
= NULL
;
3248 /* search start of word to be completed */
3249 if (!edit_find_word_start (edit
, &word_start
, &word_len
))
3252 /* prepare match expression */
3253 bufpos
= &edit
->buffers1
[word_start
>> S_EDIT_BUF_SIZE
][word_start
& M_EDIT_BUF_SIZE
];
3254 match_expr
= g_strdup_printf ("%.*s", (int) word_len
, bufpos
);
3256 ptr
= g_get_current_dir ();
3257 path
= g_strconcat (ptr
, G_DIR_SEPARATOR_S
, (char *) NULL
);
3260 /* Recursive search file 'TAGS' in parent dirs */
3263 ptr
= g_path_get_dirname (path
);
3267 tagfile
= mc_build_filename (path
, TAGS_NAME
, (char *) NULL
);
3268 if (exist_file (tagfile
))
3271 while (strcmp (path
, G_DIR_SEPARATOR_S
) != 0);
3276 etags_set_definition_hash (tagfile
, path
, match_expr
, (etags_hash_t
*) & def_hash
);
3281 max_len
= MAX_WIDTH_DEF_DIALOG
;
3285 editcmd_dialog_select_definition_show (edit
, match_expr
, max_len
, word_len
,
3286 (etags_hash_t
*) & def_hash
, num_def
);
3288 g_free (match_expr
);
3291 /* --------------------------------------------------------------------------------------------- */