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 */
42 #include <sys/types.h>
50 #include "lib/global.h"
51 #include "lib/tty/tty.h"
52 #include "lib/tty/key.h" /* XCTRL */
53 #include "lib/mcconfig.h"
55 #include "lib/strutil.h" /* utf string functions */
57 #include "lib/util.h" /* tilde_expand() */
58 #include "lib/vfs/vfs.h"
59 #include "lib/widget.h"
60 #include "lib/charsets.h"
61 #include "lib/event.h" /* mc_event_raise() */
63 #include "src/history.h"
64 #include "src/setup.h" /* option_tab_spacing */
65 #include "src/main.h" /* mactos_t */
66 #include "src/selcodepage.h"
67 #include "src/keybind-defaults.h"
68 #include "src/util.h" /* check_for_default() */
69 #include "src/filemanager/layout.h" /* mc_refresh() */
71 #include "edit-impl.h"
72 #include "edit-widget.h"
73 #include "editcmd_dialogs.h"
76 /*** global variables ****************************************************************************/
78 /* search and replace: */
79 int search_create_bookmark
= FALSE
;
81 /* queries on a save */
82 int edit_confirm_save
= 1;
84 /*** file scope macro definitions ****************************************************************/
88 #define TEMP_BUF_LEN 1024
92 /* thanks to Liviu Daia <daia@stoilow.imar.ro> for getting this
93 (and the above) routines to work properly - paul */
95 #define is_digit(x) ((x) >= '0' && (x) <= '9')
97 #define MAIL_DLG_HEIGHT 12
99 #define MAX_WORD_COMPLETIONS 100 /* in listbox */
101 /*** file scope type declarations ****************************************************************/
103 /*** file scope variables ************************************************************************/
105 /*** file scope functions ************************************************************************/
106 /* --------------------------------------------------------------------------------------------- */
108 /* If 0 (quick save) then a) create/truncate <filename> file,
109 b) save to <filename>;
110 if 1 (safe save) then a) save to <tempnam>,
111 b) rename <tempnam> to <filename>;
112 if 2 (do backups) then a) save to <tempnam>,
113 b) rename <filename> to <filename.backup_ext>,
114 c) rename <tempnam> to <filename>. */
116 /* returns 0 on error, -1 on abort */
119 edit_save_file (WEdit
* edit
, const vfs_path_t
* filename_vpath
)
124 int this_save_mode
, fd
= -1;
125 vfs_path_t
*real_filename_vpath
;
126 vfs_path_t
*savename_vpath
= NULL
;
127 const char *start_filename
;
129 start_filename
= vfs_path_get_by_index (filename_vpath
, 0)->path
;
130 if (filename_vpath
== NULL
|| *start_filename
== '\0')
133 if (*start_filename
!= PATH_SEP
&& edit
->dir_vpath
!= NULL
)
135 real_filename_vpath
= vfs_path_append_vpath_new (edit
->dir_vpath
, filename_vpath
, NULL
);
139 real_filename_vpath
= vfs_path_clone (filename_vpath
);
142 this_save_mode
= option_save_mode
;
143 if (this_save_mode
!= EDIT_QUICK_SAVE
)
145 if (!vfs_file_is_local (real_filename_vpath
)
146 || (fd
= mc_open (real_filename_vpath
, O_RDONLY
| O_BINARY
)) == -1)
149 * The file does not exists yet, so no safe save or
150 * backup are necessary.
152 this_save_mode
= EDIT_QUICK_SAVE
;
158 if (this_save_mode
== EDIT_QUICK_SAVE
&& !edit
->skip_detach_prompt
)
163 rv
= mc_stat (real_filename_vpath
, &sb
);
164 if (rv
== 0 && sb
.st_nlink
> 1)
166 rv
= edit_query_dialog3 (_("Warning"),
167 _("File has hard-links. Detach before saving?"),
168 _("&Yes"), _("&No"), _("&Cancel"));
172 this_save_mode
= EDIT_SAFE_SAVE
;
175 edit
->skip_detach_prompt
= 1;
178 vfs_path_free (real_filename_vpath
);
183 /* Prevent overwriting changes from other editor sessions. */
184 if (rv
== 0 && edit
->stat1
.st_mtime
!= 0 && edit
->stat1
.st_mtime
!= sb
.st_mtime
)
186 /* The default action is "Cancel". */
189 rv
= edit_query_dialog2 (_("Warning"),
190 _("The file has been modified in the meantime. Save anyway?"),
191 _("&Yes"), _("&Cancel"));
194 vfs_path_free (real_filename_vpath
);
200 if (this_save_mode
!= EDIT_QUICK_SAVE
)
202 char *savedir
, *saveprefix
;
204 savedir
= vfs_path_tokens_get (real_filename_vpath
, 0, -1);
206 savedir
= g_strdup (".");
208 saveprefix
= mc_build_filename (savedir
, "cooledit", NULL
);
210 fd
= mc_mkstemps (&savename_vpath
, saveprefix
, NULL
);
212 if (savename_vpath
== NULL
)
214 vfs_path_free (real_filename_vpath
);
218 * Close for now because mc_mkstemps use pure open system call
219 * to create temporary file and it needs to be reopened by
220 * VFS-aware mc_open().
225 savename_vpath
= vfs_path_clone (real_filename_vpath
);
227 (void) mc_chown (savename_vpath
, edit
->stat1
.st_uid
, edit
->stat1
.st_gid
);
228 (void) mc_chmod (savename_vpath
, edit
->stat1
.st_mode
);
230 fd
= mc_open (savename_vpath
, O_CREAT
| O_WRONLY
| O_TRUNC
| O_BINARY
, edit
->stat1
.st_mode
);
235 p
= edit_get_write_filter (savename_vpath
, real_filename_vpath
);
241 file
= (FILE *) popen (p
, "w");
245 filelen
= edit_write_stream (edit
, file
);
249 if (pclose (file
) != 0)
251 tmp
= g_strdup_printf (_("Error writing to pipe: %s"), p
);
252 edit_error_dialog (_("Error"), tmp
);
261 tmp
= g_strdup_printf (_("Cannot open pipe for writing: %s"), p
);
262 edit_error_dialog (_("Error"), get_sys_error (tmp
));
269 else if (edit
->lb
== LB_ASIS
)
270 { /* do not change line breaks */
273 filelen
= edit
->last_byte
;
274 while (buf
<= (edit
->curs1
>> S_EDIT_BUF_SIZE
) - 1)
276 if (mc_write (fd
, (char *) edit
->buffers1
[buf
], EDIT_BUF_SIZE
) != EDIT_BUF_SIZE
)
284 (fd
, (char *) edit
->buffers1
[buf
],
285 edit
->curs1
& M_EDIT_BUF_SIZE
) != (edit
->curs1
& M_EDIT_BUF_SIZE
))
289 else if (edit
->curs2
)
292 buf
= (edit
->curs2
>> S_EDIT_BUF_SIZE
);
295 (char *) edit
->buffers2
[buf
] + EDIT_BUF_SIZE
-
296 (edit
->curs2
& M_EDIT_BUF_SIZE
) - 1,
297 1 + (edit
->curs2
& M_EDIT_BUF_SIZE
)) != 1 + (edit
->curs2
& M_EDIT_BUF_SIZE
))
305 if (mc_write (fd
, (char *) edit
->buffers2
[buf
], EDIT_BUF_SIZE
) != EDIT_BUF_SIZE
)
317 /* Update the file information, especially the mtime. */
318 if (mc_stat (savename_vpath
, &edit
->stat1
) == -1)
322 { /* change line breaks */
324 const vfs_path_element_t
*path_element
;
328 path_element
= vfs_path_get_by_index (savename_vpath
, -1);
329 file
= (FILE *) fopen (path_element
->path
, "w");
332 filelen
= edit_write_stream (edit
, file
);
339 msg
= g_strdup_printf (_("Cannot open file for writing: %s"), path_element
->path
);
340 edit_error_dialog (_("Error"), msg
);
346 if (filelen
!= edit
->last_byte
)
349 if (this_save_mode
== EDIT_DO_BACKUP
)
351 vfs_path_t
*tmp_vpath
;
354 assert (option_backup_ext
!= NULL
);
355 tmp_vpath
= vfs_path_append_new (real_filename_vpath
, option_backup_ext
, (char *) NULL
);
356 ok
= (mc_rename (real_filename_vpath
, tmp_vpath
) != -1);
357 vfs_path_free (tmp_vpath
);
362 if (this_save_mode
!= EDIT_QUICK_SAVE
)
363 if (mc_rename (savename_vpath
, real_filename_vpath
) == -1)
366 vfs_path_free (real_filename_vpath
);
367 vfs_path_free (savename_vpath
);
370 /* FIXME: Is this safe ?
371 * if (this_save_mode != EDIT_QUICK_SAVE)
372 * mc_unlink (savename);
374 vfs_path_free (real_filename_vpath
);
375 vfs_path_free (savename_vpath
);
379 /* --------------------------------------------------------------------------------------------- */
382 edit_check_newline (WEdit
* edit
)
384 return !(option_check_nl_at_eof
&& edit
->last_byte
> 0
385 && edit_get_byte (edit
, edit
->last_byte
- 1) != '\n'
386 && edit_query_dialog2 (_("Warning"),
387 _("The file you are saving is not finished with a newline"),
388 _("C&ontinue"), _("&Cancel")));
391 /* --------------------------------------------------------------------------------------------- */
394 edit_get_save_file_as (WEdit
* edit
)
397 #define DLG_HEIGHT 14
399 static LineBreaks cur_lb
= LB_ASIS
;
401 char *filename
= vfs_path_to_str (edit
->filename_vpath
);
404 const char *lb_names
[LB_NAMES
] = {
405 N_("&Do not change"),
406 N_("&Unix format (LF)"),
407 N_("&Windows/DOS format (CR LF)"),
408 N_("&Macintosh format (CR)")
411 QuickWidget quick_widgets
[] = {
412 QUICK_BUTTON (6, 10, DLG_HEIGHT
- 3, DLG_HEIGHT
, N_("&Cancel"), B_CANCEL
, NULL
),
413 QUICK_BUTTON (2, 10, DLG_HEIGHT
- 3, DLG_HEIGHT
, N_("&OK"), B_ENTER
, NULL
),
414 QUICK_RADIO (5, DLG_WIDTH
, DLG_HEIGHT
- 8, DLG_HEIGHT
, LB_NAMES
, lb_names
, (int *) &cur_lb
),
415 QUICK_LABEL (3, DLG_WIDTH
, DLG_HEIGHT
- 9, DLG_HEIGHT
, N_("Change line breaks to:")),
416 QUICK_INPUT (3, DLG_WIDTH
, DLG_HEIGHT
- 11, DLG_HEIGHT
, filename
, DLG_WIDTH
- 6, 0,
417 "save-as", &filename_res
),
418 QUICK_LABEL (3, DLG_WIDTH
, DLG_HEIGHT
- 12, DLG_HEIGHT
, N_("Enter file name:")),
422 QuickDialog Quick_options
= {
423 DLG_WIDTH
, DLG_HEIGHT
, -1, -1,
424 N_("Save As"), "[Save File As]",
425 quick_widgets
, NULL
, FALSE
428 if (quick_dialog (&Quick_options
) != B_CANCEL
)
431 vfs_path_t
*ret_vpath
;
434 fname
= tilde_expand (filename_res
);
435 g_free (filename_res
);
436 ret_vpath
= vfs_path_from_str (fname
);
448 /* --------------------------------------------------------------------------------------------- */
450 /** returns 1 on success */
453 edit_save_cmd (WEdit
* edit
)
455 int res
, save_lock
= 0;
457 if (!edit
->locked
&& !edit
->delete_file
)
458 save_lock
= edit_lock_file (edit
);
459 res
= edit_save_file (edit
, edit
->filename_vpath
);
461 /* Maintain modify (not save) lock on failure */
462 if ((res
> 0 && edit
->locked
) || save_lock
)
463 edit
->locked
= edit_unlock_file (edit
);
465 /* On failure try 'save as', it does locking on its own */
467 return edit_save_as_cmd (edit
);
468 edit
->force
|= REDRAW_COMPLETELY
;
471 edit
->delete_file
= 0;
478 /* --------------------------------------------------------------------------------------------- */
479 /** returns 1 on error */
482 edit_load_file_from_filename (WEdit
* edit
, const vfs_path_t
* exp_vpath
)
484 int prev_locked
= edit
->locked
;
485 vfs_path_t
*prev_filename
;
487 prev_filename
= vfs_path_clone (edit
->filename_vpath
);
488 if (!edit_reload (edit
, exp_vpath
))
490 vfs_path_free (prev_filename
);
496 vfs_path_t
*fullpath
;
498 fullpath
= vfs_path_append_vpath_new (edit
->dir_vpath
, prev_filename
, (char *) NULL
);
499 unlock_file (fullpath
);
500 vfs_path_free (fullpath
);
502 vfs_path_free (prev_filename
);
506 /* --------------------------------------------------------------------------------------------- */
509 edit_load_syntax_file (WEdit
* edit
)
511 vfs_path_t
*extdir_vpath
;
516 dir
= query_dialog (_("Syntax file edit"),
517 _("Which syntax file you want to edit?"), D_NORMAL
, 2,
518 _("&User"), _("&System Wide"));
522 vfs_path_build_filename (mc_global
.sysconfig_dir
, "syntax", "Syntax", (char *) NULL
);
523 if (!exist_file (vfs_path_get_last_path_str (extdir_vpath
)))
525 vfs_path_free (extdir_vpath
);
527 vfs_path_build_filename (mc_global
.share_data_dir
, "syntax", "Syntax", (char *) NULL
);
532 vfs_path_t
*user_syntax_file_vpath
;
534 user_syntax_file_vpath
= mc_config_get_full_vpath (EDIT_SYNTAX_FILE
);
535 check_for_default (extdir_vpath
, user_syntax_file_vpath
);
536 edit_load_file_from_filename (edit
, user_syntax_file_vpath
);
537 vfs_path_free (user_syntax_file_vpath
);
540 edit_load_file_from_filename (edit
, extdir_vpath
);
542 vfs_path_free (extdir_vpath
);
545 /* --------------------------------------------------------------------------------------------- */
548 edit_load_menu_file (WEdit
* edit
)
550 vfs_path_t
*buffer_vpath
;
551 vfs_path_t
*menufile_vpath
;
554 dir
= query_dialog (_("Menu edit"),
555 _("Which menu file do you want to edit?"), D_NORMAL
,
556 geteuid () != 0 ? 2 : 3, _("&Local"), _("&User"), _("&System Wide"));
558 menufile_vpath
= vfs_path_build_filename (mc_global
.sysconfig_dir
, EDIT_GLOBAL_MENU
, NULL
);
560 if (!exist_file (vfs_path_get_last_path_str (menufile_vpath
)))
562 vfs_path_free (menufile_vpath
);
563 menufile_vpath
= vfs_path_build_filename (mc_global
.share_data_dir
, EDIT_GLOBAL_MENU
, NULL
);
569 buffer_vpath
= vfs_path_from_str (EDIT_LOCAL_MENU
);
570 check_for_default (menufile_vpath
, buffer_vpath
);
571 chmod (vfs_path_get_last_path_str (buffer_vpath
), 0600);
575 buffer_vpath
= mc_config_get_full_vpath (EDIT_HOME_MENU
);
576 check_for_default (menufile_vpath
, buffer_vpath
);
580 buffer_vpath
= vfs_path_build_filename (mc_global
.sysconfig_dir
, EDIT_GLOBAL_MENU
, NULL
);
581 if (!exist_file (vfs_path_get_last_path_str (buffer_vpath
)))
583 vfs_path_free (buffer_vpath
);
585 vfs_path_build_filename (mc_global
.share_data_dir
, EDIT_GLOBAL_MENU
, NULL
);
590 vfs_path_free (menufile_vpath
);
594 edit_load_file_from_filename (edit
, buffer_vpath
);
596 vfs_path_free (buffer_vpath
);
597 vfs_path_free (menufile_vpath
);
600 /* --------------------------------------------------------------------------------------------- */
603 edit_delete_column_of_text (WEdit
* edit
)
605 long p
, q
, r
, m1
, m2
;
608 eval_marks (edit
, &m1
, &m2
);
609 n
= edit_move_forward (edit
, m1
, 0, m2
) + 1;
610 c
= edit_move_forward3 (edit
, edit_bol (edit
, m1
), 0, m1
);
611 d
= edit_move_forward3 (edit
, edit_bol (edit
, m2
), 0, m2
);
612 b
= max (min (c
, d
), min (edit
->column1
, edit
->column2
));
613 c
= max (c
, max (edit
->column1
, edit
->column2
));
617 r
= edit_bol (edit
, edit
->curs1
);
618 p
= edit_move_forward3 (edit
, r
, b
, 0);
619 q
= edit_move_forward3 (edit
, r
, c
, 0);
624 edit_cursor_move (edit
, p
- edit
->curs1
);
627 /* delete line between margins */
628 if (edit_get_byte (edit
, edit
->curs1
) != '\n')
629 edit_delete (edit
, 1);
633 /* move to next line except on the last delete */
634 edit_cursor_move (edit
, edit_move_forward (edit
, edit
->curs1
, 1, 0) - edit
->curs1
);
638 /* --------------------------------------------------------------------------------------------- */
639 /** if success return 0 */
642 edit_block_delete (WEdit
* edit
)
645 long start_mark
, end_mark
;
646 int curs_pos
, line_width
;
647 long curs_line
, c1
, c2
;
649 if (eval_marks (edit
, &start_mark
, &end_mark
))
651 if (edit
->column_highlight
&& edit
->mark2
< 0)
652 edit_mark_cmd (edit
, 0);
653 if ((end_mark
- start_mark
) > option_max_undo
/ 2)
655 /* Warning message with a query to continue or cancel the operation */
656 if (edit_query_dialog2
659 ("Block is large, you may not be able to undo this action"),
660 _("C&ontinue"), _("&Cancel")))
665 c1
= min (edit
->column1
, edit
->column2
);
666 c2
= max (edit
->column1
, edit
->column2
);
670 edit_push_markers (edit
);
672 curs_line
= edit
->curs_line
;
674 /* calculate line width and cursor position before cut */
675 line_width
= edit_move_forward3 (edit
, edit_bol (edit
, edit
->curs1
), 0,
676 edit_eol (edit
, edit
->curs1
));
677 curs_pos
= edit
->curs_col
+ edit
->over_col
;
679 /* move cursor to start of selection */
680 edit_cursor_move (edit
, start_mark
- edit
->curs1
);
681 edit_scroll_screen_over_cursor (edit
);
683 if (start_mark
< end_mark
)
685 if (edit
->column_highlight
)
688 edit_mark_cmd (edit
, 0);
689 edit_delete_column_of_text (edit
);
690 /* move cursor to the saved position */
691 edit_move_to_line (edit
, curs_line
);
692 /* calculate line width after cut */
693 line_width
= edit_move_forward3 (edit
, edit_bol (edit
, edit
->curs1
), 0,
694 edit_eol (edit
, edit
->curs1
));
695 if (option_cursor_beyond_eol
&& curs_pos
> line_width
)
696 edit
->over_col
= curs_pos
- line_width
;
700 while (count
< end_mark
)
702 edit_delete (edit
, 1);
707 edit_set_markers (edit
, 0, 0, 0, 0);
708 edit
->force
|= REDRAW_PAGE
;
712 /* --------------------------------------------------------------------------------------------- */
715 editcmd_find (WEdit
* edit
, gsize
* len
)
717 off_t search_start
= edit
->search_start
;
720 long end_mark
= edit
->last_byte
;
723 if (edit_search_options
.only_in_selection
)
725 mark_res
= eval_marks (edit
, &start_mark
, &end_mark
);
728 edit
->search
->error
= MC_SEARCH_E_NOTFOUND
;
729 edit
->search
->error_str
= g_strdup (_("Search string not found"));
732 if (edit_search_options
.backwards
)
734 if (search_start
> end_mark
|| search_start
<= start_mark
)
736 search_start
= end_mark
;
741 if (search_start
< start_mark
|| search_start
>= end_mark
)
743 search_start
= start_mark
;
749 if (edit_search_options
.backwards
)
750 end_mark
= max (1, edit
->curs1
) - 1;
752 if (edit_search_options
.backwards
)
754 search_end
= end_mark
;
755 while ((int) search_start
>= start_mark
)
757 if (search_end
> (off_t
) (search_start
+ edit
->search
->original_len
) &&
758 mc_search_is_fixed_search_str (edit
->search
))
760 search_end
= search_start
+ edit
->search
->original_len
;
762 if (mc_search_run (edit
->search
, (void *) edit
, search_start
, search_end
, len
)
763 && edit
->search
->normal_offset
== search_start
)
769 edit
->search
->error_str
= g_strdup (_("Search string not found"));
773 return mc_search_run (edit
->search
, (void *) edit
, search_start
, end_mark
, len
);
778 /* --------------------------------------------------------------------------------------------- */
781 edit_replace_cmd__conv_to_display (char *str
)
786 tmp
= str_convert_to_display (str
);
790 return g_string_free (tmp
, FALSE
);
791 g_string_free (tmp
, TRUE
);
794 return g_strdup (str
);
797 /* --------------------------------------------------------------------------------------------- */
800 edit_replace_cmd__conv_to_input (char *str
)
805 tmp
= str_convert_to_input (str
);
809 return g_string_free (tmp
, FALSE
);
810 g_string_free (tmp
, TRUE
);
813 return g_strdup (str
);
816 /* --------------------------------------------------------------------------------------------- */
819 edit_do_search (WEdit
* edit
)
823 if (edit
->search
== NULL
)
824 edit
->search_start
= edit
->curs1
;
826 edit_push_undo_action (edit
, KEY_PRESS
+ edit
->start_display
);
828 if (search_create_bookmark
)
830 int found
= 0, books
= 0;
831 long l
= 0, l_last
= -1;
834 search_create_bookmark
= FALSE
;
835 book_mark_flush (edit
, -1);
839 if (!mc_search_run (edit
->search
, (void *) edit
, q
, edit
->last_byte
, &len
))
842 edit
->search_start
= edit
->search
->normal_offset
;
844 l
+= edit_count_lines (edit
, q
, edit
->search
->normal_offset
);
847 book_mark_insert (edit
, l
, BOOK_MARK_FOUND_COLOR
);
851 q
= edit
->search
->normal_offset
+ 1;
855 edit_error_dialog (_("Search"), _("Search string not found"));
857 edit_cursor_move (edit
, edit
->search_start
- edit
->curs1
);
861 if (edit
->found_len
!= 0 && edit
->search_start
== edit
->found_start
+ 1
862 && edit_search_options
.backwards
)
863 edit
->search_start
--;
865 if (edit
->found_len
!= 0 && edit
->search_start
== edit
->found_start
- 1
866 && !edit_search_options
.backwards
)
867 edit
->search_start
++;
869 if (editcmd_find (edit
, &len
))
871 edit
->found_start
= edit
->search_start
= edit
->search
->normal_offset
;
872 edit
->found_len
= len
;
874 edit_cursor_move (edit
, edit
->search_start
- edit
->curs1
);
875 edit_scroll_screen_over_cursor (edit
);
876 if (edit_search_options
.backwards
)
877 edit
->search_start
--;
879 edit
->search_start
++;
883 edit
->search_start
= edit
->curs1
;
884 if (edit
->search
->error_str
!= NULL
)
885 edit_error_dialog (_("Search"), edit
->search
->error_str
);
889 edit
->force
|= REDRAW_COMPLETELY
;
890 edit_scroll_screen_over_cursor (edit
);
893 /* --------------------------------------------------------------------------------------------- */
896 edit_search (WEdit
* edit
)
898 if (editcmd_dialog_search_show (edit
))
899 edit_do_search (edit
);
902 /* --------------------------------------------------------------------------------------------- */
903 /** Return a null terminated length of text. Result must be g_free'd */
905 static unsigned char *
906 edit_get_block (WEdit
* edit
, long start
, long finish
, int *l
)
908 unsigned char *s
, *r
;
909 r
= s
= g_malloc0 (finish
- start
+ 1);
910 if (edit
->column_highlight
)
913 /* copy from buffer, excluding chars that are out of the column 'margins' */
914 while (start
< finish
)
918 x
= edit_move_forward3 (edit
, edit_bol (edit
, start
), 0, start
);
919 c
= edit_get_byte (edit
, start
);
920 if ((x
>= edit
->column1
&& x
< edit
->column2
)
921 || (x
>= edit
->column2
&& x
< edit
->column1
) || c
== '\n')
932 while (start
< finish
)
933 *s
++ = edit_get_byte (edit
, start
++);
939 /* --------------------------------------------------------------------------------------------- */
940 /** copies a block to clipboard file */
943 edit_save_block_to_clip_file (WEdit
* edit
, long start
, long finish
)
947 tmp
= mc_config_get_full_path (EDIT_CLIP_FILE
);
948 ret
= edit_save_block (edit
, tmp
, start
, finish
);
953 /* --------------------------------------------------------------------------------------------- */
956 pipe_mail (WEdit
* edit
, char *to
, char *subject
, char *cc
)
961 to
= name_quote (to
, 0);
962 subject
= name_quote (subject
, 0);
963 cc
= name_quote (cc
, 0);
964 s
= g_strconcat ("mail -s ", subject
, *cc
? " -c " : "", cc
, " ", to
, (char *) NULL
);
978 for (i
= 0; i
< edit
->last_byte
; i
++)
979 fputc (edit_get_byte (edit
, i
), p
);
984 /* --------------------------------------------------------------------------------------------- */
987 is_break_char (char c
)
989 return (isspace (c
) || strchr ("{}[]()<>=|/\\!?~-+`'\",.;:#$%^&*", c
));
992 /* --------------------------------------------------------------------------------------------- */
993 /** find first character of current word */
996 edit_find_word_start (WEdit
* edit
, long *word_start
, gsize
* word_len
)
1001 /* return if at begin of file */
1002 if (edit
->curs1
<= 0)
1005 c
= (unsigned char) edit_get_byte (edit
, edit
->curs1
- 1);
1006 /* return if not at end or in word */
1007 if (is_break_char (c
))
1010 /* search start of word to be completed */
1013 /* return if at begin of file */
1014 if ((gsize
) edit
->curs1
< i
)
1018 c
= (unsigned char) edit_get_byte (edit
, edit
->curs1
- i
);
1020 if (is_break_char (c
))
1022 /* return if word starts with digit */
1026 *word_start
= edit
->curs1
- (i
- 1); /* start found */
1035 /* --------------------------------------------------------------------------------------------- */
1037 * Get current word under cursor
1039 * @param edit editor object
1040 * @param srch mc_search object
1041 * @param word_start start word position
1043 * @return newly allocated string or NULL if no any words under cursor
1047 edit_collect_completions_get_current_word (WEdit
* edit
, mc_search_t
* srch
, long word_start
)
1052 if (!mc_search_run (srch
, (void *) edit
, word_start
, edit
->last_byte
, &len
))
1055 temp
= g_string_sized_new (len
);
1057 for (i
= 0; i
< len
; i
++)
1061 chr
= edit_get_byte (edit
, word_start
+ i
);
1063 g_string_append_c (temp
, chr
);
1066 return g_string_free (temp
, temp
->len
== 0);
1069 /* --------------------------------------------------------------------------------------------- */
1070 /** collect the possible completions */
1073 edit_collect_completions (WEdit
* edit
, long word_start
, gsize word_len
,
1074 char *match_expr
, struct selection
*compl, gsize
* num
)
1082 long last_byte
, start
= -1;
1085 srch
= mc_search_new (match_expr
, -1);
1089 if (mc_config_get_bool
1090 (mc_main_config
, CONFIG_APP_SECTION
, "editor_wordcompletion_collect_entire_file", 0))
1092 last_byte
= edit
->last_byte
;
1096 last_byte
= word_start
;
1099 srch
->search_type
= MC_SEARCH_T_REGEX
;
1100 srch
->is_case_sensitive
= TRUE
;
1101 srch
->search_fn
= edit_search_cmd_callback
;
1103 current_word
= edit_collect_completions_get_current_word (edit
, srch
, word_start
);
1105 temp
= g_string_new ("");
1107 /* collect max MAX_WORD_COMPLETIONS completions */
1108 while (mc_search_run (srch
, (void *) edit
, start
+ 1, last_byte
, &len
))
1110 g_string_set_size (temp
, 0);
1111 start
= srch
->normal_offset
;
1113 /* add matched completion if not yet added */
1114 for (i
= 0; i
< len
; i
++)
1116 skip
= edit_get_byte (edit
, start
+ i
);
1120 /* skip current word */
1121 if (start
+ (long) i
== word_start
)
1124 g_string_append_c (temp
, skip
);
1130 if (current_word
!= NULL
&& strcmp (current_word
, temp
->str
) == 0)
1135 for (i
= 0; i
< *num
; i
++)
1138 ((char *) &compl[i
].text
[word_len
],
1139 (char *) &temp
->str
[word_len
], max (len
, compl[i
].len
) - word_len
) == 0)
1141 struct selection
this = compl[i
];
1142 for (++i
; i
< *num
; i
++)
1144 compl[i
- 1] = compl[i
];
1146 compl[*num
- 1] = this;
1148 break; /* skip it, already added */
1154 if (*num
== MAX_WORD_COMPLETIONS
)
1156 g_free (compl[0].text
);
1157 for (i
= 1; i
< *num
; i
++)
1159 compl[i
- 1] = compl[i
];
1166 recoded
= str_convert_to_display (temp
->str
);
1168 if (recoded
&& recoded
->len
)
1169 g_string_assign (temp
, recoded
->str
);
1171 g_string_free (recoded
, TRUE
);
1174 compl[*num
].text
= g_strdup (temp
->str
);
1175 compl[*num
].len
= temp
->len
;
1179 /* note the maximal length needed for the completion dialog */
1184 mc_search_free (srch
);
1185 g_string_free (temp
, TRUE
);
1186 g_free (current_word
);
1191 /* --------------------------------------------------------------------------------------------- */
1194 edit_insert_column_of_text (WEdit
* edit
, unsigned char *data
, int size
, int width
,
1195 long *start_pos
, long *end_pos
, int *col1
, int *col2
)
1200 cursor
= edit
->curs1
;
1201 col
= edit_get_col (edit
);
1203 for (i
= 0; i
< size
; i
++)
1205 if (data
[i
] != '\n')
1206 edit_insert (edit
, data
[i
]);
1208 { /* fill in and move to next line */
1212 if (edit_get_byte (edit
, edit
->curs1
) != '\n')
1214 l
= width
- (edit_get_col (edit
) - col
);
1217 edit_insert (edit
, ' ');
1221 for (p
= edit
->curs1
;; p
++)
1223 if (p
== edit
->last_byte
)
1225 edit_cursor_move (edit
, edit
->last_byte
- edit
->curs1
);
1226 edit_insert_ahead (edit
, '\n');
1230 if (edit_get_byte (edit
, p
) == '\n')
1236 edit_cursor_move (edit
, edit_move_forward3 (edit
, p
, col
, 0) - edit
->curs1
);
1237 l
= col
- edit_get_col (edit
);
1238 while (l
>= space_width
)
1240 edit_insert (edit
, ' ');
1247 *col2
= col
+ width
;
1248 *start_pos
= cursor
;
1249 *end_pos
= edit
->curs1
;
1250 edit_cursor_move (edit
, cursor
- edit
->curs1
);
1253 /* --------------------------------------------------------------------------------------------- */
1256 edit_macro_comparator (gconstpointer
* macro1
, gconstpointer
* macro2
)
1258 const macros_t
*m1
= (const macros_t
*) macro1
;
1259 const macros_t
*m2
= (const macros_t
*) macro2
;
1261 return m1
->hotkey
- m2
->hotkey
;
1264 /* --------------------------------------------------------------------------------------------- */
1267 edit_macro_sort_by_hotkey (void)
1269 if (macros_list
!= NULL
&& macros_list
->len
!= 0)
1270 g_array_sort (macros_list
, (GCompareFunc
) edit_macro_comparator
);
1273 /* --------------------------------------------------------------------------------------------- */
1276 edit_get_macro (WEdit
* edit
, int hotkey
, const macros_t
** macros
, guint
* indx
)
1278 const macros_t
*array_start
= &g_array_index (macros_list
, struct macros_t
, 0);
1280 macros_t search_macro
;
1284 search_macro
.hotkey
= hotkey
;
1285 result
= bsearch (&search_macro
, macros_list
->data
, macros_list
->len
,
1286 sizeof (macros_t
), (GCompareFunc
) edit_macro_comparator
);
1288 if (result
!= NULL
&& result
->macro
!= NULL
)
1290 *indx
= (result
- array_start
);
1298 /* --------------------------------------------------------------------------------------------- */
1299 /** returns FALSE on error */
1302 edit_delete_macro (WEdit
* edit
, int hotkey
)
1304 mc_config_t
*macros_config
= NULL
;
1305 const char *section_name
= "editor";
1306 gchar
*macros_fname
;
1309 const macros_t
*macros
= NULL
;
1311 /* clear array of actions for current hotkey */
1312 while (edit_get_macro (edit
, hotkey
, ¯os
, &indx
))
1314 if (macros
->macro
!= NULL
)
1315 g_array_free (macros
->macro
, TRUE
);
1317 g_array_remove_index (macros_list
, indx
);
1318 edit_macro_sort_by_hotkey ();
1321 macros_fname
= mc_config_get_full_path (MC_MACRO_FILE
);
1322 macros_config
= mc_config_init (macros_fname
);
1323 g_free (macros_fname
);
1325 if (macros_config
== NULL
)
1328 skeyname
= lookup_key_by_code (hotkey
);
1329 while (mc_config_del_key (macros_config
, section_name
, skeyname
))
1332 mc_config_save_file (macros_config
, NULL
);
1333 mc_config_deinit (macros_config
);
1338 /* --------------------------------------------------------------------------------------------- */
1339 /*** public functions ****************************************************************************/
1340 /* --------------------------------------------------------------------------------------------- */
1343 edit_help_cmd (WEdit
* edit
)
1345 ev_help_t event_data
= { NULL
, "[Internal File Editor]" };
1346 mc_event_raise (MCEVENT_GROUP_CORE
, "help", &event_data
);
1348 edit
->force
|= REDRAW_COMPLETELY
;
1351 /* --------------------------------------------------------------------------------------------- */
1354 edit_refresh_cmd (WEdit
* edit
)
1359 edit_get_syntax_color (edit
, -1, &color
);
1360 tty_touch_screen ();
1367 #endif /* !HAVE_SLANG */
1371 /* --------------------------------------------------------------------------------------------- */
1374 menu_save_mode_cmd (void)
1377 const int DLG_X
= 38;
1378 const int DLG_Y
= 13;
1382 const char *str
[] = {
1385 N_("&Do backups with following extension:")
1388 QuickWidget widgets
[] = {
1390 QUICK_BUTTON (18, DLG_X
, DLG_Y
- 3, DLG_Y
, N_("&Cancel"), B_CANCEL
, NULL
),
1392 QUICK_BUTTON (6, DLG_X
, DLG_Y
- 3, DLG_Y
, N_("&OK"), B_ENTER
, NULL
),
1394 QUICK_CHECKBOX (4, DLG_X
, 8, DLG_Y
, N_("Check &POSIX new line"), &option_check_nl_at_eof
),
1396 QUICK_INPUT (8, DLG_X
, 6, DLG_Y
, option_backup_ext
, 9, 0, "edit-backup-ext", &str_result
),
1398 QUICK_RADIO (4, DLG_X
, 3, DLG_Y
, 3, str
, &option_save_mode
),
1402 QuickDialog dialog
= {
1403 DLG_X
, DLG_Y
, -1, -1, N_("Edit Save Mode"),
1404 "[Edit Save Mode]", widgets
, NULL
, FALSE
1409 size_t w0
, w1
, b_len
, w3
;
1411 assert (option_backup_ext
!= NULL
);
1413 /* OK/Cancel buttons */
1414 w0
= str_term_width1 (_(widgets
[0].u
.button
.text
)) + 3;
1415 w1
= str_term_width1 (_(widgets
[1].u
.button
.text
)) + 5; /* default button */
1416 b_len
= w0
+ w1
+ 3;
1418 maxlen
= max (b_len
, (size_t) str_term_width1 (_(dialog
.title
)) + 2);
1421 for (i
= 0; i
< 3; i
++)
1426 w3
= max (w3
, (size_t) str_term_width1 (str
[i
]));
1429 maxlen
= max (maxlen
, w3
+ 4);
1431 dialog
.xlen
= min ((size_t) COLS
, maxlen
+ 8);
1433 widgets
[3].u
.input
.len
= w3
;
1434 widgets
[1].relative_x
= (dialog
.xlen
- b_len
) / 2;
1435 widgets
[0].relative_x
= widgets
[1].relative_x
+ w0
+ 2;
1437 for (i
= 0; i
< sizeof (widgets
) / sizeof (widgets
[0]); i
++)
1438 widgets
[i
].x_divisions
= dialog
.xlen
;
1440 if (quick_dialog (&dialog
) != B_CANCEL
)
1442 g_free (option_backup_ext
);
1443 option_backup_ext
= str_result
;
1447 /* --------------------------------------------------------------------------------------------- */
1450 edit_set_filename (WEdit
* edit
, const vfs_path_t
* name_vpath
)
1452 vfs_path_free (edit
->filename_vpath
);
1453 edit
->filename_vpath
= vfs_path_clone (name_vpath
);
1455 if (edit
->dir_vpath
== NULL
)
1456 edit
->dir_vpath
= vfs_path_clone (vfs_get_raw_current_dir ());
1459 /* --------------------------------------------------------------------------------------------- */
1460 /* Here we want to warn the users of overwriting an existing file,
1461 but only if they have made a change to the filename */
1462 /* returns 1 on success */
1464 edit_save_as_cmd (WEdit
* edit
)
1466 /* This heads the 'Save As' dialog box */
1467 vfs_path_t
*exp_vpath
;
1469 int different_filename
= 0;
1471 if (!edit_check_newline (edit
))
1474 exp_vpath
= edit_get_save_file_as (edit
);
1475 edit_push_undo_action (edit
, KEY_PRESS
+ edit
->start_display
);
1477 if (exp_vpath
!= NULL
)
1479 if (vfs_path_len (exp_vpath
) == 0)
1485 if (vfs_path_cmp (edit
->filename_vpath
, exp_vpath
) != 0)
1489 different_filename
= 1;
1490 file
= mc_open (exp_vpath
, O_RDONLY
| O_BINARY
);
1494 /* the file exists */
1496 /* Overwrite the current file or cancel the operation */
1497 if (edit_query_dialog2
1499 _("A file already exists with this name"), _("&Overwrite"), _("&Cancel")))
1504 edit
->stat1
.st_mode
|= S_IWUSR
;
1506 save_lock
= lock_file (exp_vpath
);
1510 /* filenames equal, check if already locked */
1511 if (!edit
->locked
&& !edit
->delete_file
)
1512 save_lock
= lock_file (exp_vpath
);
1515 if (different_filename
)
1518 * Allow user to write into saved (under another name) file
1519 * even if original file had r/o user permissions.
1521 edit
->stat1
.st_mode
|= S_IWRITE
;
1524 rv
= edit_save_file (edit
, exp_vpath
);
1528 /* Succesful, so unlock both files */
1529 if (different_filename
)
1532 unlock_file (exp_vpath
);
1534 edit
->locked
= edit_unlock_file (edit
);
1538 if (edit
->locked
|| save_lock
)
1539 edit
->locked
= edit_unlock_file (edit
);
1542 edit_set_filename (edit
, exp_vpath
);
1543 if (edit
->lb
!= LB_ASIS
)
1544 edit_reload (edit
, exp_vpath
);
1546 edit
->delete_file
= 0;
1547 if (different_filename
)
1548 edit_load_syntax (edit
, NULL
, edit
->syntax_type
);
1549 vfs_path_free (exp_vpath
);
1550 edit
->force
|= REDRAW_COMPLETELY
;
1553 edit_error_dialog (_("Save as"), get_sys_error (_("Cannot save file")));
1556 /* Failed, so maintain modify (not save) lock */
1558 unlock_file (exp_vpath
);
1565 vfs_path_free (exp_vpath
);
1566 edit
->force
|= REDRAW_COMPLETELY
;
1570 /* {{{ Macro stuff starts here */
1571 /* --------------------------------------------------------------------------------------------- */
1574 edit_delete_macro_cmd (WEdit
* edit
)
1578 hotkey
= editcmd_dialog_raw_key_query (_("Delete macro"), _("Press macro hotkey:"), 1);
1580 if (hotkey
!= 0 && !edit_delete_macro (edit
, hotkey
))
1581 message (D_ERROR
, _("Delete macro"), _("Macro not deleted"));
1584 /* --------------------------------------------------------------------------------------------- */
1586 /** returns FALSE on error */
1588 edit_execute_macro (WEdit
* edit
, int hotkey
)
1590 gboolean res
= FALSE
;
1594 const macros_t
*macros
;
1597 if (edit_get_macro (edit
, hotkey
, ¯os
, &indx
) &&
1598 macros
->macro
!= NULL
&& macros
->macro
->len
!= 0)
1602 edit
->force
|= REDRAW_PAGE
;
1604 for (i
= 0; i
< macros
->macro
->len
; i
++)
1606 const macro_action_t
*m_act
;
1608 m_act
= &g_array_index (macros
->macro
, struct macro_action_t
, i
);
1609 edit_execute_cmd (edit
, m_act
->action
, m_act
->ch
);
1614 edit_update_screen (edit
);
1618 /* --------------------------------------------------------------------------------------------- */
1620 /** returns FALSE on error */
1622 edit_store_macro_cmd (WEdit
* edit
)
1626 GString
*marcros_string
;
1627 mc_config_t
*macros_config
= NULL
;
1628 const char *section_name
= "editor";
1629 gchar
*macros_fname
;
1630 GArray
*macros
; /* current macro */
1632 gboolean have_macro
= FALSE
;
1633 char *skeyname
= NULL
;
1635 hotkey
= editcmd_dialog_raw_key_query (_("Save macro"), _("Press the macro's new hotkey:"), 1);
1636 if (hotkey
== ESC_CHAR
)
1639 tmp_act
= keybind_lookup_keymap_command (editor_map
, hotkey
);
1641 /* return FALSE if try assign macro into restricted hotkeys */
1642 if (tmp_act
== CK_MacroStartRecord
1643 || tmp_act
== CK_MacroStopRecord
|| tmp_act
== CK_MacroStartStopRecord
)
1646 edit_delete_macro (edit
, hotkey
);
1648 macros_fname
= mc_config_get_full_path (MC_MACRO_FILE
);
1649 macros_config
= mc_config_init (macros_fname
);
1650 g_free (macros_fname
);
1652 if (macros_config
== NULL
)
1655 edit_push_undo_action (edit
, KEY_PRESS
+ edit
->start_display
);
1657 marcros_string
= g_string_sized_new (250);
1658 macros
= g_array_new (TRUE
, FALSE
, sizeof (macro_action_t
));
1660 skeyname
= lookup_key_by_code (hotkey
);
1662 for (i
= 0; i
< macro_index
; i
++)
1664 macro_action_t m_act
;
1665 const char *action_name
;
1667 action_name
= keybind_lookup_actionname (record_macro_buf
[i
].action
);
1669 if (action_name
== NULL
)
1672 m_act
.action
= record_macro_buf
[i
].action
;
1673 m_act
.ch
= record_macro_buf
[i
].ch
;
1674 g_array_append_val (macros
, m_act
);
1676 g_string_append_printf (marcros_string
, "%s:%i;", action_name
,
1677 (int) record_macro_buf
[i
].ch
);
1682 macro
.hotkey
= hotkey
;
1683 macro
.macro
= macros
;
1684 g_array_append_val (macros_list
, macro
);
1685 mc_config_set_string (macros_config
, section_name
, skeyname
, marcros_string
->str
);
1688 mc_config_del_key (macros_config
, section_name
, skeyname
);
1691 edit_macro_sort_by_hotkey ();
1693 g_string_free (marcros_string
, TRUE
);
1694 mc_config_save_file (macros_config
, NULL
);
1695 mc_config_deinit (macros_config
);
1699 /* --------------------------------------------------------------------------------------------- */
1702 edit_repeat_macro_cmd (WEdit
* edit
)
1709 f
= input_dialog (_("Repeat last commands"), _("Repeat times:"), MC_HISTORY_EDIT_REPEAT
, NULL
);
1710 if (f
== NULL
|| *f
== '\0')
1716 count_repeat
= strtol (f
, &error
, 0);
1726 edit_push_undo_action (edit
, KEY_PRESS
+ edit
->start_display
);
1727 edit
->force
|= REDRAW_PAGE
;
1729 for (j
= 0; j
< count_repeat
; j
++)
1730 for (i
= 0; i
< macro_index
; i
++)
1731 edit_execute_cmd (edit
, record_macro_buf
[i
].action
, record_macro_buf
[i
].ch
);
1732 edit_update_screen (edit
);
1736 /* --------------------------------------------------------------------------------------------- */
1737 /** return FALSE on error */
1740 edit_load_macro_cmd (WEdit
* edit
)
1742 mc_config_t
*macros_config
= NULL
;
1743 gchar
**profile_keys
, **keys
;
1744 gchar
**values
, **curr_values
;
1745 gsize len
, values_len
;
1746 const char *section_name
= "editor";
1747 gchar
*macros_fname
;
1752 macros_fname
= mc_config_get_full_path (MC_MACRO_FILE
);
1753 macros_config
= mc_config_init (macros_fname
);
1754 g_free (macros_fname
);
1756 if (macros_config
== NULL
)
1759 profile_keys
= keys
= mc_config_get_keys (macros_config
, section_name
, &len
);
1760 while (*profile_keys
!= NULL
)
1762 gboolean have_macro
;
1766 macros
= g_array_new (TRUE
, FALSE
, sizeof (macro_action_t
));
1768 curr_values
= values
= mc_config_get_string_list (macros_config
, section_name
,
1769 *profile_keys
, &values_len
);
1770 hotkey
= lookup_key (*profile_keys
, NULL
);
1773 while (*curr_values
!= NULL
&& *curr_values
[0] != '\0')
1775 char **macro_pair
= NULL
;
1777 macro_pair
= g_strsplit (*curr_values
, ":", 2);
1779 if (macro_pair
!= NULL
)
1781 macro_action_t m_act
;
1782 if (macro_pair
[0] == NULL
|| macro_pair
[0][0] == '\0')
1786 m_act
.action
= keybind_lookup_action (macro_pair
[0]);
1787 g_free (macro_pair
[0]);
1788 macro_pair
[0] = NULL
;
1790 if (macro_pair
[1] == NULL
|| macro_pair
[1][0] == '\0')
1794 m_act
.ch
= strtol (macro_pair
[1], NULL
, 0);
1795 g_free (macro_pair
[1]);
1796 macro_pair
[1] = NULL
;
1798 if (m_act
.action
!= 0)
1800 /* a shell command */
1801 if ((m_act
.action
/ CK_PipeBlock (0)) == 1)
1803 m_act
.action
= CK_PipeBlock (0) + (m_act
.ch
> 0 ? m_act
.ch
: 0);
1806 g_array_append_val (macros
, m_act
);
1809 g_strfreev (macro_pair
);
1816 macro
.hotkey
= hotkey
;
1817 macro
.macro
= macros
;
1818 g_array_append_val (macros_list
, macro
);
1821 g_strfreev (values
);
1824 mc_config_deinit (macros_config
);
1825 edit_macro_sort_by_hotkey ();
1829 /* }}} Macro stuff end here */
1831 /* --------------------------------------------------------------------------------------------- */
1832 /** returns 1 on success */
1835 edit_save_confirm_cmd (WEdit
* edit
)
1839 if (edit
->filename_vpath
== NULL
)
1840 return edit_save_as_cmd (edit
);
1842 if (!edit_check_newline (edit
))
1845 if (edit_confirm_save
)
1850 filename
= vfs_path_to_str (edit
->filename_vpath
);
1851 f
= g_strdup_printf (_("Confirm save file: \"%s\""), filename
);
1853 ok
= (edit_query_dialog2 (_("Save file"), f
, _("&Save"), _("&Cancel")) == 0);
1858 return edit_save_cmd (edit
);
1861 /* --------------------------------------------------------------------------------------------- */
1862 /** returns 1 on success */
1865 edit_new_cmd (WEdit
* edit
)
1869 if (edit_query_dialog2
1872 ("Current text was modified without a file save.\nContinue discards these changes"),
1873 _("C&ontinue"), _("&Cancel")))
1875 edit
->force
|= REDRAW_COMPLETELY
;
1879 edit
->force
|= REDRAW_COMPLETELY
;
1881 return edit_renew (edit
); /* if this gives an error, something has really screwed up */
1884 /* --------------------------------------------------------------------------------------------- */
1887 edit_load_cmd (WEdit
* edit
, edit_current_file_t what
)
1890 && (edit_query_dialog2
1892 _("Current text was modified without a file save.\n"
1893 "Continue discards these changes"), _("C&ontinue"), _("&Cancel")) == 1))
1895 edit
->force
|= REDRAW_COMPLETELY
;
1901 case EDIT_FILE_COMMON
:
1903 char *filename
, *exp
;
1905 filename
= vfs_path_to_str (edit
->filename_vpath
);
1906 exp
= input_expand_dialog (_("Load"), _("Enter file name:"),
1907 MC_HISTORY_EDIT_LOAD
, filename
);
1914 vfs_path_t
*exp_vpath
;
1916 exp_vpath
= vfs_path_from_str (exp
);
1917 edit_load_file_from_filename (edit
, exp_vpath
);
1918 vfs_path_free (exp_vpath
);
1925 case EDIT_FILE_SYNTAX
:
1926 edit_load_syntax_file (edit
);
1929 case EDIT_FILE_MENU
:
1930 edit_load_menu_file (edit
);
1937 edit
->force
|= REDRAW_COMPLETELY
;
1941 /* --------------------------------------------------------------------------------------------- */
1943 if mark2 is -1 then marking is from mark1 to the cursor.
1944 Otherwise its between the markers. This handles this.
1945 Returns 1 if no text is marked.
1949 eval_marks (WEdit
* edit
, long *start_mark
, long *end_mark
)
1951 if (edit
->mark1
!= edit
->mark2
)
1953 long start_bol
, start_eol
;
1954 long end_bol
, end_eol
;
1959 if (edit
->end_mark_curs
< 0)
1960 end_mark_curs
= edit
->curs1
;
1962 end_mark_curs
= edit
->end_mark_curs
;
1964 if (edit
->mark2
>= 0)
1966 *start_mark
= min (edit
->mark1
, edit
->mark2
);
1967 *end_mark
= max (edit
->mark1
, edit
->mark2
);
1971 *start_mark
= min (edit
->mark1
, end_mark_curs
);
1972 *end_mark
= max (edit
->mark1
, end_mark_curs
);
1973 edit
->column2
= edit
->curs_col
+ edit
->over_col
;
1976 if (edit
->column_highlight
1977 && (((edit
->mark1
> end_mark_curs
) && (edit
->column1
< edit
->column2
))
1978 || ((edit
->mark1
< end_mark_curs
) && (edit
->column1
> edit
->column2
))))
1980 start_bol
= edit_bol (edit
, *start_mark
);
1981 start_eol
= edit_eol (edit
, start_bol
- 1) + 1;
1982 end_bol
= edit_bol (edit
, *end_mark
);
1983 end_eol
= edit_eol (edit
, *end_mark
);
1984 col1
= min (edit
->column1
, edit
->column2
);
1985 col2
= max (edit
->column1
, edit
->column2
);
1988 edit_move_forward3 (edit
, start_bol
, col2
, 0) - edit_move_forward3 (edit
, start_bol
,
1991 edit_move_forward3 (edit
, end_bol
, col2
, 0) - edit_move_forward3 (edit
, end_bol
,
1994 *start_mark
-= diff1
;
1996 *start_mark
= max (*start_mark
, start_eol
);
1997 *end_mark
= min (*end_mark
, end_eol
);
2003 *start_mark
= *end_mark
= 0;
2004 edit
->column2
= edit
->column1
= 0;
2009 /* --------------------------------------------------------------------------------------------- */
2012 edit_insert_over (WEdit
* edit
)
2016 for (i
= 0; i
< edit
->over_col
; i
++)
2018 edit_insert (edit
, ' ');
2023 /* --------------------------------------------------------------------------------------------- */
2026 edit_insert_column_of_text_from_file (WEdit
* edit
, int file
,
2027 long *start_pos
, long *end_pos
, int *col1
, int *col2
)
2031 int blocklen
= -1, width
= 0;
2032 unsigned char *data
;
2034 cursor
= edit
->curs1
;
2035 col
= edit_get_col (edit
);
2036 data
= g_malloc0 (TEMP_BUF_LEN
);
2038 while ((blocklen
= mc_read (file
, (char *) data
, TEMP_BUF_LEN
)) > 0)
2041 for (width
= 0; width
< blocklen
; width
++)
2043 if (data
[width
] == '\n')
2046 for (i
= 0; i
< blocklen
; i
++)
2048 if (data
[i
] == '\n')
2049 { /* fill in and move to next line */
2052 if (edit_get_byte (edit
, edit
->curs1
) != '\n')
2054 l
= width
- (edit_get_col (edit
) - col
);
2057 edit_insert (edit
, ' ');
2061 for (p
= edit
->curs1
;; p
++)
2063 if (p
== edit
->last_byte
)
2065 edit_cursor_move (edit
, edit
->last_byte
- edit
->curs1
);
2066 edit_insert_ahead (edit
, '\n');
2070 if (edit_get_byte (edit
, p
) == '\n')
2076 edit_cursor_move (edit
, edit_move_forward3 (edit
, p
, col
, 0) - edit
->curs1
);
2077 l
= col
- edit_get_col (edit
);
2078 while (l
>= space_width
)
2080 edit_insert (edit
, ' ');
2085 edit_insert (edit
, data
[i
]);
2089 *col2
= col
+ width
;
2090 *start_pos
= cursor
;
2091 *end_pos
= edit
->curs1
;
2092 edit_cursor_move (edit
, cursor
- edit
->curs1
);
2098 /* --------------------------------------------------------------------------------------------- */
2101 edit_block_copy_cmd (WEdit
* edit
)
2103 long start_mark
, end_mark
, current
= edit
->curs1
;
2108 unsigned char *copy_buf
;
2110 edit_update_curs_col (edit
);
2111 if (eval_marks (edit
, &start_mark
, &end_mark
))
2114 copy_buf
= edit_get_block (edit
, start_mark
, end_mark
, &size
);
2116 /* all that gets pushed are deletes hence little space is used on the stack */
2118 edit_push_markers (edit
);
2120 if (edit
->column_highlight
)
2122 col_delta
= abs (edit
->column2
- edit
->column1
);
2123 edit_insert_column_of_text (edit
, copy_buf
, size
, col_delta
, &mark1
, &mark2
, &c1
, &c2
);
2128 edit_insert_ahead (edit
, copy_buf
[size
]);
2132 edit_scroll_screen_over_cursor (edit
);
2134 if (edit
->column_highlight
)
2135 edit_set_markers (edit
, edit
->curs1
, mark2
, c1
, c2
);
2136 else if (start_mark
< current
&& end_mark
> current
)
2137 edit_set_markers (edit
, start_mark
, end_mark
+ end_mark
- start_mark
, 0, 0);
2139 edit
->force
|= REDRAW_PAGE
;
2143 /* --------------------------------------------------------------------------------------------- */
2146 edit_block_move_cmd (WEdit
* edit
)
2149 unsigned char *copy_buf
= NULL
;
2150 long start_mark
, end_mark
;
2153 if (eval_marks (edit
, &start_mark
, &end_mark
))
2156 line
= edit
->curs_line
;
2157 if (edit
->mark2
< 0)
2158 edit_mark_cmd (edit
, 0);
2159 edit_push_markers (edit
);
2161 if (edit
->column_highlight
)
2169 c1
= min (edit
->column1
, edit
->column2
);
2170 c2
= max (edit
->column1
, edit
->column2
);
2171 b_width
= (c2
- c1
);
2173 edit_update_curs_col (edit
);
2176 x2
= x
+ edit
->over_col
;
2178 /* do nothing when cursor inside first line of selected area */
2179 if ((edit_eol (edit
, edit
->curs1
) == edit_eol (edit
, start_mark
)) && (x2
> c1
&& x2
<= c2
))
2182 if (edit
->curs1
> start_mark
&& edit
->curs1
< edit_eol (edit
, end_mark
))
2186 else if (x
> c1
&& x
<= c2
)
2189 /* save current selection into buffer */
2190 copy_buf
= edit_get_block (edit
, start_mark
, end_mark
, &size
);
2192 /* remove current selection */
2193 edit_block_delete_cmd (edit
);
2195 edit
->over_col
= max (0, edit
->over_col
- b_width
);
2196 /* calculate the cursor pos after delete block */
2197 current
= edit_move_forward3 (edit
, edit_bol (edit
, edit
->curs1
), x
, 0);
2198 edit_cursor_move (edit
, current
- edit
->curs1
);
2199 edit_scroll_screen_over_cursor (edit
);
2201 /* add TWS if need before block insertion */
2202 if (option_cursor_beyond_eol
&& edit
->over_col
> 0)
2203 edit_insert_over (edit
);
2205 edit_insert_column_of_text (edit
, copy_buf
, size
, b_width
, &mark1
, &mark2
, &c1
, &c2
);
2206 edit_set_markers (edit
, mark1
, mark2
, c1
, c2
);
2212 current
= edit
->curs1
;
2213 copy_buf
= g_malloc0 (end_mark
- start_mark
);
2214 edit_cursor_move (edit
, start_mark
- edit
->curs1
);
2215 edit_scroll_screen_over_cursor (edit
);
2217 while (count
< end_mark
)
2219 copy_buf
[end_mark
- count
- 1] = edit_delete (edit
, 1);
2222 edit_scroll_screen_over_cursor (edit
);
2223 edit_cursor_move (edit
,
2224 current
- edit
->curs1
-
2225 (((current
- edit
->curs1
) > 0) ? end_mark
- start_mark
: 0));
2226 edit_scroll_screen_over_cursor (edit
);
2227 while (count
-- > start_mark
)
2228 edit_insert_ahead (edit
, copy_buf
[end_mark
- count
- 1]);
2229 edit_set_markers (edit
, edit
->curs1
, edit
->curs1
+ end_mark
- start_mark
, 0, 0);
2232 edit_scroll_screen_over_cursor (edit
);
2234 edit
->force
|= REDRAW_PAGE
;
2237 /* --------------------------------------------------------------------------------------------- */
2238 /** returns 1 if canceelled by user */
2241 edit_block_delete_cmd (WEdit
* edit
)
2243 long start_mark
, end_mark
;
2244 if (eval_marks (edit
, &start_mark
, &end_mark
))
2246 edit_delete_line (edit
);
2249 return edit_block_delete (edit
);
2252 /* --------------------------------------------------------------------------------------------- */
2253 /** call with edit = 0 before shutdown to close memory leaks */
2256 edit_replace_cmd (WEdit
* edit
, int again
)
2258 /* 1 = search string, 2 = replace with */
2259 static char *saved1
= NULL
; /* saved default[123] */
2260 static char *saved2
= NULL
;
2261 char *input1
= NULL
; /* user input from the dialog */
2262 char *input2
= NULL
;
2265 long times_replaced
= 0;
2266 gboolean once_found
= FALSE
;
2270 g_free (saved1
), saved1
= NULL
;
2271 g_free (saved2
), saved2
= NULL
;
2275 edit
->force
|= REDRAW_COMPLETELY
;
2277 if (again
&& !saved1
&& !saved2
)
2282 input1
= g_strdup (saved1
? saved1
: "");
2283 input2
= g_strdup (saved2
? saved2
: "");
2287 char *tmp_inp1
, *tmp_inp2
;
2288 disp1
= edit_replace_cmd__conv_to_display (saved1
? saved1
: (char *) "");
2289 disp2
= edit_replace_cmd__conv_to_display (saved2
? saved2
: (char *) "");
2291 edit_push_undo_action (edit
, KEY_PRESS
+ edit
->start_display
);
2293 editcmd_dialog_replace_show (edit
, disp1
, disp2
, &input1
, &input2
);
2298 if (input1
== NULL
|| *input1
== '\0')
2300 edit
->force
= REDRAW_COMPLETELY
;
2306 input1
= edit_replace_cmd__conv_to_input (input1
);
2307 input2
= edit_replace_cmd__conv_to_input (input2
);
2311 g_free (saved1
), saved1
= g_strdup (input1
);
2312 g_free (saved2
), saved2
= g_strdup (input2
);
2314 mc_search_free (edit
->search
);
2315 edit
->search
= NULL
;
2320 edit
->search
= mc_search_new (input1
, -1);
2321 if (edit
->search
== NULL
)
2323 edit
->search_start
= edit
->curs1
;
2326 edit
->search
->search_type
= edit_search_options
.type
;
2327 edit
->search
->is_all_charsets
= edit_search_options
.all_codepages
;
2328 edit
->search
->is_case_sensitive
= edit_search_options
.case_sens
;
2329 edit
->search
->whole_words
= edit_search_options
.whole_words
;
2330 edit
->search
->search_fn
= edit_search_cmd_callback
;
2333 if (edit
->found_len
&& edit
->search_start
== edit
->found_start
+ 1
2334 && edit_search_options
.backwards
)
2335 edit
->search_start
--;
2337 if (edit
->found_len
&& edit
->search_start
== edit
->found_start
- 1
2338 && !edit_search_options
.backwards
)
2339 edit
->search_start
++;
2345 if (!editcmd_find (edit
, &len
))
2347 if (!(edit
->search
->error
== MC_SEARCH_E_OK
||
2348 (once_found
&& edit
->search
->error
== MC_SEARCH_E_NOTFOUND
)))
2350 edit_error_dialog (_("Search"), edit
->search
->error_str
);
2356 edit
->search_start
= edit
->search
->normal_offset
;
2357 /*returns negative on not found or error in pattern */
2359 if ((edit
->search_start
>= 0) && (edit
->search_start
< edit
->last_byte
))
2362 GString
*tmp_str
, *repl_str
;
2364 edit
->found_start
= edit
->search_start
;
2365 i
= edit
->found_len
= len
;
2367 edit_cursor_move (edit
, edit
->search_start
- edit
->curs1
);
2368 edit_scroll_screen_over_cursor (edit
);
2370 if (edit
->replace_mode
== 0)
2375 l
= edit
->curs_row
- edit
->widget
.lines
/ 3;
2377 edit_scroll_downward (edit
, l
);
2379 edit_scroll_upward (edit
, -l
);
2381 edit_scroll_screen_over_cursor (edit
);
2382 edit
->force
|= REDRAW_PAGE
;
2383 edit_render_keypress (edit
);
2385 /*so that undo stops at each query */
2386 edit_push_key_press (edit
);
2387 /* and prompt 2/3 down */
2388 disp1
= edit_replace_cmd__conv_to_display (saved1
);
2389 disp2
= edit_replace_cmd__conv_to_display (saved2
);
2390 prompt
= editcmd_dialog_replace_prompt_show (edit
, disp1
, disp2
, -1, -1);
2394 if (prompt
== B_REPLACE_ALL
)
2395 edit
->replace_mode
= 1;
2396 else if (prompt
== B_SKIP_REPLACE
)
2398 if (edit_search_options
.backwards
)
2399 edit
->search_start
--;
2401 edit
->search_start
++;
2402 continue; /* loop */
2404 else if (prompt
== B_CANCEL
)
2406 edit
->replace_mode
= -1;
2411 /* don't process string each time */
2412 tmp_str
= g_string_new (input2
);
2413 repl_str
= mc_search_prepare_replace_str (edit
->search
, tmp_str
);
2414 g_string_free (tmp_str
, TRUE
);
2416 if (edit
->search
->error
!= MC_SEARCH_E_OK
)
2418 edit_error_dialog (_("Replace"), edit
->search
->error_str
);
2419 g_string_free (repl_str
, TRUE
);
2423 /* delete then insert new */
2424 for (i
= 0; i
< len
; i
++)
2425 edit_delete (edit
, 1);
2427 for (i
= 0; i
< repl_str
->len
; i
++)
2428 edit_insert (edit
, repl_str
->str
[i
]);
2430 edit
->found_len
= repl_str
->len
;
2431 g_string_free (repl_str
, TRUE
);
2434 /* so that we don't find the same string again */
2435 if (edit_search_options
.backwards
)
2436 edit
->search_start
--;
2439 edit
->search_start
+= edit
->found_len
;
2441 if (edit
->search_start
>= edit
->last_byte
)
2445 edit_scroll_screen_over_cursor (edit
);
2449 /* try and find from right here for next search */
2450 edit
->search_start
= edit
->curs1
;
2451 edit_update_curs_col (edit
);
2453 edit
->force
|= REDRAW_PAGE
;
2454 edit_render_keypress (edit
);
2456 if (times_replaced
== 0)
2457 query_dialog (_("Replace"), _("Search string not found"), D_NORMAL
, 1, _("&OK"));
2461 while (edit
->replace_mode
>= 0);
2463 edit_scroll_screen_over_cursor (edit
);
2464 edit
->force
|= REDRAW_COMPLETELY
;
2465 edit_render_keypress (edit
);
2467 if ((edit
->replace_mode
== 1) && (times_replaced
!= 0))
2468 message (D_NORMAL
, _("Replace"), _("%ld replacements made"), times_replaced
);
2475 /* --------------------------------------------------------------------------------------------- */
2478 edit_search_cmd_callback (const void *user_data
, gsize char_offset
)
2480 return edit_get_byte ((WEdit
*) user_data
, (long) char_offset
);
2483 /* --------------------------------------------------------------------------------------------- */
2486 edit_search_cmd (WEdit
* edit
, gboolean again
)
2493 else if (edit
->last_search_string
!= NULL
)
2494 edit_do_search (edit
);
2497 /* find last search string in history */
2500 history
= history_get (MC_HISTORY_SHARED_SEARCH
);
2501 if (history
!= NULL
&& history
->data
!= NULL
)
2503 edit
->last_search_string
= (char *) history
->data
;
2504 history
->data
= NULL
;
2505 history
= g_list_first (history
);
2506 g_list_foreach (history
, (GFunc
) g_free
, NULL
);
2507 g_list_free (history
);
2509 edit
->search
= mc_search_new (edit
->last_search_string
, -1);
2510 if (edit
->search
== NULL
)
2512 /* if not... then ask for an expression */
2513 g_free (edit
->last_search_string
);
2514 edit
->last_search_string
= NULL
;
2519 edit
->search
->search_type
= edit_search_options
.type
;
2520 edit
->search
->is_all_charsets
= edit_search_options
.all_codepages
;
2521 edit
->search
->is_case_sensitive
= edit_search_options
.case_sens
;
2522 edit
->search
->whole_words
= edit_search_options
.whole_words
;
2523 edit
->search
->search_fn
= edit_search_cmd_callback
;
2524 edit_do_search (edit
);
2529 /* if not... then ask for an expression */
2530 g_free (edit
->last_search_string
);
2531 edit
->last_search_string
= NULL
;
2538 /* --------------------------------------------------------------------------------------------- */
2540 * Check if it's OK to close the editor. If there are unsaved changes,
2541 * ask user. Return 1 if it's OK to exit, 0 to continue editing.
2545 edit_ok_to_exit (WEdit
* edit
)
2549 if (!edit
->modified
)
2552 if (!mc_global
.widget
.midnight_shutdown
)
2554 if (!edit_check_newline (edit
))
2558 act
= edit_query_dialog3 (_("Quit"), _("File was modified. Save with exit?"),
2559 _("&Yes"), _("&No"), _("&Cancel quit"));
2564 edit_query_dialog2 (_("Quit"),
2565 _("Midnight Commander is being shut down.\nSave modified file?"),
2566 _("&Yes"), _("&No"));
2576 edit_push_markers (edit
);
2577 edit_set_markers (edit
, 0, 0, 0, 0);
2578 if (!edit_save_cmd (edit
) || mc_global
.widget
.midnight_shutdown
)
2579 return mc_global
.widget
.midnight_shutdown
;
2583 case 2: /* Cancel quit */
2591 /* --------------------------------------------------------------------------------------------- */
2592 /** save block, returns 1 on success */
2595 edit_save_block (WEdit
* edit
, const char *filename
, long start
, long finish
)
2600 vpath
= vfs_path_from_str (filename
);
2601 file
= mc_open (vpath
, O_CREAT
| O_WRONLY
| O_TRUNC
,
2602 S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IROTH
| O_BINARY
);
2603 vfs_path_free (vpath
);
2607 if (edit
->column_highlight
)
2611 r
= mc_write (file
, VERTICAL_MAGIC
, sizeof (VERTICAL_MAGIC
));
2614 unsigned char *block
, *p
;
2616 p
= block
= edit_get_block (edit
, start
, finish
, &len
);
2619 r
= mc_write (file
, p
, len
);
2633 len
= finish
- start
;
2634 buf
= g_malloc0 (TEMP_BUF_LEN
);
2635 while (start
!= finish
)
2637 end
= min (finish
, start
+ TEMP_BUF_LEN
);
2638 for (; i
< end
; i
++)
2639 buf
[i
- start
] = edit_get_byte (edit
, i
);
2640 len
-= mc_write (file
, (char *) buf
, end
- start
);
2651 /* --------------------------------------------------------------------------------------------- */
2654 edit_paste_from_history (WEdit
* edit
)
2657 edit_error_dialog (_("Error"), _("This function is not implemented"));
2660 /* --------------------------------------------------------------------------------------------- */
2663 edit_copy_to_X_buf_cmd (WEdit
* edit
)
2665 long start_mark
, end_mark
;
2666 if (eval_marks (edit
, &start_mark
, &end_mark
))
2668 if (!edit_save_block_to_clip_file (edit
, start_mark
, end_mark
))
2670 edit_error_dialog (_("Copy to clipboard"), get_sys_error (_("Unable to save to file")));
2673 /* try use external clipboard utility */
2674 mc_event_raise (MCEVENT_GROUP_CORE
, "clipboard_file_to_ext_clip", NULL
);
2679 /* --------------------------------------------------------------------------------------------- */
2682 edit_cut_to_X_buf_cmd (WEdit
* edit
)
2684 long start_mark
, end_mark
;
2685 if (eval_marks (edit
, &start_mark
, &end_mark
))
2687 if (!edit_save_block_to_clip_file (edit
, start_mark
, end_mark
))
2689 edit_error_dialog (_("Cut to clipboard"), _("Unable to save to file"));
2692 /* try use external clipboard utility */
2693 mc_event_raise (MCEVENT_GROUP_CORE
, "clipboard_file_to_ext_clip", NULL
);
2695 edit_block_delete_cmd (edit
);
2696 edit_mark_cmd (edit
, 1);
2700 /* --------------------------------------------------------------------------------------------- */
2703 edit_paste_from_X_buf_cmd (WEdit
* edit
)
2707 /* try use external clipboard utility */
2708 mc_event_raise (MCEVENT_GROUP_CORE
, "clipboard_file_from_ext_clip", NULL
);
2709 tmp
= mc_config_get_full_vpath (EDIT_CLIP_FILE
);
2710 edit_insert_file (edit
, tmp
);
2711 vfs_path_free (tmp
);
2715 /* --------------------------------------------------------------------------------------------- */
2717 * Ask user for the line and go to that line.
2718 * Negative numbers mean line from the end (i.e. -1 is the last line).
2722 edit_goto_cmd (WEdit
* edit
)
2725 static long line
= 0; /* line as typed, saved as default */
2730 g_snprintf (s
, sizeof (s
), "%ld", line
);
2731 f
= input_dialog (_("Goto line"), _("Enter line:"), MC_HISTORY_EDIT_GOTO_LINE
, line
? s
: "");
2741 l
= strtol (f
, &error
, 0);
2750 l
= edit
->total_lines
+ l
+ 2;
2751 edit_move_display (edit
, l
- edit
->widget
.lines
/ 2 - 1);
2752 edit_move_to_line (edit
, l
- 1);
2753 edit
->force
|= REDRAW_COMPLETELY
;
2758 /* --------------------------------------------------------------------------------------------- */
2759 /** Return 1 on success */
2762 edit_save_block_cmd (WEdit
* edit
)
2764 long start_mark
, end_mark
;
2767 if (eval_marks (edit
, &start_mark
, &end_mark
))
2770 tmp
= mc_config_get_full_path (EDIT_CLIP_FILE
);
2772 input_expand_dialog (_("Save block"), _("Enter file name:"),
2773 MC_HISTORY_EDIT_SAVE_BLOCK
, tmp
);
2775 edit_push_undo_action (edit
, KEY_PRESS
+ edit
->start_display
);
2785 if (edit_save_block (edit
, exp
, start_mark
, end_mark
))
2788 edit
->force
|= REDRAW_COMPLETELY
;
2794 edit_error_dialog (_("Save block"), get_sys_error (_("Cannot save file")));
2798 edit
->force
|= REDRAW_COMPLETELY
;
2803 /* --------------------------------------------------------------------------------------------- */
2804 /** returns TRUE on success */
2807 edit_insert_file_cmd (WEdit
* edit
)
2811 gboolean ret
= FALSE
;
2813 tmp
= mc_config_get_full_path (EDIT_CLIP_FILE
);
2814 exp
= input_expand_dialog (_("Insert file"), _("Enter file name:"),
2815 MC_HISTORY_EDIT_INSERT_FILE
, tmp
);
2818 edit_push_undo_action (edit
, KEY_PRESS
+ edit
->start_display
);
2820 if (exp
!= NULL
&& *exp
!= '\0')
2822 vfs_path_t
*exp_vpath
;
2824 exp_vpath
= vfs_path_from_str (exp
);
2825 ret
= (edit_insert_file (edit
, exp_vpath
) >= 0);
2826 vfs_path_free (exp_vpath
);
2829 edit_error_dialog (_("Insert file"), get_sys_error (_("Cannot insert file")));
2834 edit
->force
|= REDRAW_COMPLETELY
;
2838 /* --------------------------------------------------------------------------------------------- */
2839 /** sorts a block, returns -1 on system fail, 1 on cancel and 0 on success */
2842 edit_sort_cmd (WEdit
* edit
)
2844 static char *old
= 0;
2845 char *exp
, *tmp
, *tmp_edit_block_name
, *tmp_edit_temp_name
;
2846 long start_mark
, end_mark
;
2849 if (eval_marks (edit
, &start_mark
, &end_mark
))
2851 edit_error_dialog (_("Sort block"), _("You must first highlight a block of text"));
2855 tmp
= mc_config_get_full_path (EDIT_BLOCK_FILE
);
2856 edit_save_block (edit
, tmp
, start_mark
, end_mark
);
2859 exp
= input_dialog (_("Run sort"),
2860 _("Enter sort options (see manpage) separated by whitespace:"),
2861 MC_HISTORY_EDIT_SORT
, (old
!= NULL
) ? old
: "");
2867 tmp_edit_block_name
= mc_config_get_full_path (EDIT_BLOCK_FILE
);
2868 tmp_edit_temp_name
= mc_config_get_full_path (EDIT_TEMP_FILE
);
2870 g_strconcat (" sort ", exp
, " ", tmp_edit_block_name
,
2871 " > ", tmp_edit_temp_name
, (char *) NULL
);
2872 g_free (tmp_edit_temp_name
);
2873 g_free (tmp_edit_block_name
);
2879 if (e
== -1 || e
== 127)
2881 edit_error_dialog (_("Sort"), get_sys_error (_("Cannot execute sort command")));
2886 sprintf (q
, "%d ", e
);
2887 tmp
= g_strdup_printf (_("Sort returned non-zero: %s"), q
);
2888 edit_error_dialog (_("Sort"), tmp
);
2894 edit
->force
|= REDRAW_COMPLETELY
;
2896 if (edit_block_delete_cmd (edit
))
2900 vfs_path_t
*tmp_vpath
;
2902 tmp_vpath
= mc_config_get_full_vpath (EDIT_TEMP_FILE
);
2903 edit_insert_file (edit
, tmp_vpath
);
2904 vfs_path_free (tmp_vpath
);
2909 /* --------------------------------------------------------------------------------------------- */
2911 * Ask user for a command, execute it and paste its output back to the
2916 edit_ext_cmd (WEdit
* edit
)
2918 char *exp
, *tmp
, *tmp_edit_temp_file
;
2922 input_dialog (_("Paste output of external command"),
2923 _("Enter shell command(s):"), MC_HISTORY_EDIT_PASTE_EXTCMD
, NULL
);
2928 tmp_edit_temp_file
= mc_config_get_full_path (EDIT_TEMP_FILE
);
2929 tmp
= g_strconcat (exp
, " > ", tmp_edit_temp_file
, (char *) NULL
);
2930 g_free (tmp_edit_temp_file
);
2937 edit_error_dialog (_("External command"), get_sys_error (_("Cannot execute command")));
2941 edit
->force
|= REDRAW_COMPLETELY
;
2944 vfs_path_t
*tmp_vpath
;
2946 tmp_vpath
= mc_config_get_full_vpath (EDIT_TEMP_FILE
);
2947 edit_insert_file (edit
, tmp_vpath
);
2948 vfs_path_free (tmp_vpath
);
2953 /* --------------------------------------------------------------------------------------------- */
2954 /** if block is 1, a block must be highlighted and the shell command
2955 processes it. If block is 0 the shell command is a straight system
2956 command, that just produces some output which is to be inserted */
2959 edit_block_process_cmd (WEdit
* edit
, int macro_number
)
2962 char *macros_fname
= NULL
;
2964 fname
= g_strdup_printf ("%s.%i.sh", MC_EXTMACRO_FILE
, macro_number
);
2965 macros_fname
= g_build_filename (mc_config_get_data_path (), fname
, (char *) NULL
);
2966 user_menu (edit
, macros_fname
, 0);
2968 g_free (macros_fname
);
2969 edit
->force
|= REDRAW_COMPLETELY
;
2972 /* --------------------------------------------------------------------------------------------- */
2975 edit_mail_dialog (WEdit
* edit
)
2978 char *tmail_subject
;
2981 static char *mail_cc_last
= 0;
2982 static char *mail_subject_last
= 0;
2983 static char *mail_to_last
= 0;
2985 QuickWidget quick_widgets
[] = {
2986 /* 0 */ QUICK_BUTTON (6, 10, 9, MAIL_DLG_HEIGHT
, N_("&Cancel"), B_CANCEL
, NULL
),
2987 /* 1 */ QUICK_BUTTON (2, 10, 9, MAIL_DLG_HEIGHT
, N_("&OK"), B_ENTER
, NULL
),
2988 /* 2 */ QUICK_INPUT (3, 50, 8, MAIL_DLG_HEIGHT
, "", 44, 0, "mail-dlg-input", &tmail_cc
),
2989 /* 3 */ QUICK_LABEL (3, 50, 7, MAIL_DLG_HEIGHT
, N_("Copies to")),
2990 /* 4 */ QUICK_INPUT (3, 50, 6, MAIL_DLG_HEIGHT
, "", 44, 0, "mail-dlg-input-2",
2992 /* 5 */ QUICK_LABEL (3, 50, 5, MAIL_DLG_HEIGHT
, N_("Subject")),
2993 /* 6 */ QUICK_INPUT (3, 50, 4, MAIL_DLG_HEIGHT
, "", 44, 0, "mail-dlg-input-3", &tmail_to
),
2994 /* 7 */ QUICK_LABEL (3, 50, 3, MAIL_DLG_HEIGHT
, N_("To")),
2995 /* 8 */ QUICK_LABEL (3, 50, 2, MAIL_DLG_HEIGHT
, N_("mail -s <subject> -c <cc> <to>")),
2999 QuickDialog Quick_input
= {
3000 50, MAIL_DLG_HEIGHT
, -1, -1, N_("Mail"),
3001 "[Input Line Keys]", quick_widgets
, NULL
, FALSE
3004 quick_widgets
[2].u
.input
.text
= mail_cc_last
? mail_cc_last
: "";
3005 quick_widgets
[4].u
.input
.text
= mail_subject_last
? mail_subject_last
: "";
3006 quick_widgets
[6].u
.input
.text
= mail_to_last
? mail_to_last
: "";
3008 if (quick_dialog (&Quick_input
) != B_CANCEL
)
3010 g_free (mail_cc_last
);
3011 g_free (mail_subject_last
);
3012 g_free (mail_to_last
);
3013 mail_cc_last
= tmail_cc
;
3014 mail_subject_last
= tmail_subject
;
3015 mail_to_last
= tmail_to
;
3016 pipe_mail (edit
, mail_to_last
, mail_subject_last
, mail_cc_last
);
3021 /*******************/
3022 /* Word Completion */
3023 /*******************/
3025 /* --------------------------------------------------------------------------------------------- */
3027 * Complete current word using regular expression search
3028 * backwards beginning at the current cursor position.
3032 edit_complete_word_cmd (WEdit
* edit
)
3034 gsize i
, max_len
, word_len
= 0, num_compl
= 0;
3035 long word_start
= 0;
3036 unsigned char *bufpos
;
3038 struct selection
compl[MAX_WORD_COMPLETIONS
]; /* completions */
3040 /* search start of word to be completed */
3041 if (!edit_find_word_start (edit
, &word_start
, &word_len
))
3044 /* prepare match expression */
3045 bufpos
= &edit
->buffers1
[word_start
>> S_EDIT_BUF_SIZE
][word_start
& M_EDIT_BUF_SIZE
];
3047 /* match_expr = g_strdup_printf ("\\b%.*s[a-zA-Z_0-9]+", word_len, bufpos); */
3050 ("(^|\\s+|\\b)%.*s[^\\s\\.=\\+\\[\\]\\(\\)\\,\\;\\:\\\"\\'\\-\\?\\/\\|\\\\\\{\\}\\*\\&\\^\\%%\\$#@\\!]+",
3051 (int) word_len
, bufpos
);
3053 /* collect the possible completions */
3054 /* start search from begin to end of file */
3056 edit_collect_completions (edit
, word_start
, word_len
, match_expr
,
3057 (struct selection
*) &compl, &num_compl
);
3061 /* insert completed word if there is only one match */
3064 for (i
= word_len
; i
< compl[0].len
; i
++)
3065 edit_insert (edit
, *(compl[0].text
+ i
));
3067 /* more than one possible completion => ask the user */
3070 /* !!! usually only a beep is expected and when <ALT-TAB> is !!! */
3071 /* !!! pressed again the selection dialog pops up, but that !!! */
3072 /* !!! seems to require a further internal state !!! */
3075 /* let the user select the preferred completion */
3076 editcmd_dialog_completion_show (edit
, max_len
, word_len
,
3077 (struct selection
*) &compl, num_compl
);
3081 g_free (match_expr
);
3082 /* release memory before return */
3083 for (i
= 0; i
< num_compl
; i
++)
3084 g_free (compl[i
].text
);
3087 /* --------------------------------------------------------------------------------------------- */
3090 edit_select_codepage_cmd (WEdit
* edit
)
3093 if (do_select_codepage ())
3094 edit_set_codeset (edit
);
3096 edit
->force
= REDRAW_COMPLETELY
;
3097 edit_refresh_cmd (edit
);
3103 /* --------------------------------------------------------------------------------------------- */
3106 edit_insert_literal_cmd (WEdit
* edit
)
3108 int char_for_insertion
= editcmd_dialog_raw_key_query (_("Insert literal"),
3109 _("Press any key:"), 0);
3110 edit_execute_key_command (edit
, -1, ascii_alpha_to_cntrl (char_for_insertion
));
3113 /* --------------------------------------------------------------------------------------------- */
3116 edit_begin_end_macro_cmd (WEdit
* edit
)
3118 /* edit is a pointer to the widget */
3121 unsigned long command
= macro_index
< 0 ? CK_MacroStartRecord
: CK_MacroStopRecord
;
3122 edit_execute_key_command (edit
, command
, -1);
3126 /* --------------------------------------------------------------------------------------------- */
3129 edit_begin_end_repeat_cmd (WEdit
* edit
)
3131 /* edit is a pointer to the widget */
3134 unsigned long command
= macro_index
< 0 ? CK_RepeatStartRecord
: CK_RepeatStopRecord
;
3135 edit_execute_key_command (edit
, command
, -1);
3139 /* --------------------------------------------------------------------------------------------- */
3142 edit_load_forward_cmd (WEdit
* edit
)
3146 if (edit_query_dialog2
3148 _("Current text was modified without a file save\n"
3149 "Continue discards these changes"), _("C&ontinue"), _("&Cancel")))
3151 edit
->force
|= REDRAW_COMPLETELY
;
3155 if (edit_stack_iterator
+ 1 < MAX_HISTORY_MOVETO
)
3157 if (edit_history_moveto
[edit_stack_iterator
+ 1].line
< 1)
3161 edit_stack_iterator
++;
3162 if (edit_history_moveto
[edit_stack_iterator
].filename_vpath
)
3164 edit_reload_line (edit
, edit_history_moveto
[edit_stack_iterator
].filename_vpath
,
3165 edit_history_moveto
[edit_stack_iterator
].line
);
3179 /* --------------------------------------------------------------------------------------------- */
3182 edit_load_back_cmd (WEdit
* edit
)
3186 if (edit_query_dialog2
3188 _("Current text was modified without a file save\n"
3189 "Continue discards these changes"), _("C&ontinue"), _("&Cancel")))
3191 edit
->force
|= REDRAW_COMPLETELY
;
3195 if (edit_stack_iterator
> 0)
3197 edit_stack_iterator
--;
3198 if (edit_history_moveto
[edit_stack_iterator
].filename_vpath
)
3200 edit_reload_line (edit
, edit_history_moveto
[edit_stack_iterator
].filename_vpath
,
3201 edit_history_moveto
[edit_stack_iterator
].line
);
3215 /* --------------------------------------------------------------------------------------------- */
3218 edit_get_match_keyword_cmd (WEdit
* edit
)
3220 gsize word_len
= 0, max_len
= 0;
3223 long word_start
= 0;
3224 unsigned char *bufpos
;
3228 char *tagfile
= NULL
;
3230 etags_hash_t def_hash
[MAX_DEFINITIONS
];
3232 for (i
= 0; i
< MAX_DEFINITIONS
; i
++)
3234 def_hash
[i
].filename
= NULL
;
3237 /* search start of word to be completed */
3238 if (!edit_find_word_start (edit
, &word_start
, &word_len
))
3241 /* prepare match expression */
3242 bufpos
= &edit
->buffers1
[word_start
>> S_EDIT_BUF_SIZE
][word_start
& M_EDIT_BUF_SIZE
];
3243 match_expr
= g_strdup_printf ("%.*s", (int) word_len
, bufpos
);
3245 ptr
= g_get_current_dir ();
3246 path
= g_strconcat (ptr
, G_DIR_SEPARATOR_S
, (char *) NULL
);
3249 /* Recursive search file 'TAGS' in parent dirs */
3252 ptr
= g_path_get_dirname (path
);
3256 tagfile
= mc_build_filename (path
, TAGS_NAME
, (char *) NULL
);
3257 if (exist_file (tagfile
))
3260 while (strcmp (path
, G_DIR_SEPARATOR_S
) != 0);
3265 etags_set_definition_hash (tagfile
, path
, match_expr
, (etags_hash_t
*) & def_hash
);
3270 max_len
= MAX_WIDTH_DEF_DIALOG
;
3274 editcmd_dialog_select_definition_show (edit
, match_expr
, max_len
, word_len
,
3275 (etags_hash_t
*) & def_hash
, num_def
);
3277 g_free (match_expr
);
3280 /* --------------------------------------------------------------------------------------------- */