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
;
128 const vfs_path_element_t
*vpath_element
;
130 vpath_element
= vfs_path_get_by_index (filename_vpath
, 0);
131 if (vpath_element
== NULL
)
134 start_filename
= vpath_element
->path
;
135 if (*start_filename
== '\0')
138 if (*start_filename
!= PATH_SEP
&& edit
->dir_vpath
!= NULL
)
140 real_filename_vpath
= vfs_path_append_vpath_new (edit
->dir_vpath
, filename_vpath
, NULL
);
144 real_filename_vpath
= vfs_path_clone (filename_vpath
);
147 this_save_mode
= option_save_mode
;
148 if (this_save_mode
!= EDIT_QUICK_SAVE
)
150 if (!vfs_file_is_local (real_filename_vpath
)
151 || (fd
= mc_open (real_filename_vpath
, O_RDONLY
| O_BINARY
)) == -1)
154 * The file does not exists yet, so no safe save or
155 * backup are necessary.
157 this_save_mode
= EDIT_QUICK_SAVE
;
163 if (this_save_mode
== EDIT_QUICK_SAVE
&& !edit
->skip_detach_prompt
)
168 rv
= mc_stat (real_filename_vpath
, &sb
);
169 if (rv
== 0 && sb
.st_nlink
> 1)
171 rv
= edit_query_dialog3 (_("Warning"),
172 _("File has hard-links. Detach before saving?"),
173 _("&Yes"), _("&No"), _("&Cancel"));
177 this_save_mode
= EDIT_SAFE_SAVE
;
180 edit
->skip_detach_prompt
= 1;
183 vfs_path_free (real_filename_vpath
);
188 /* Prevent overwriting changes from other editor sessions. */
189 if (rv
== 0 && edit
->stat1
.st_mtime
!= 0 && edit
->stat1
.st_mtime
!= sb
.st_mtime
)
191 /* The default action is "Cancel". */
194 rv
= edit_query_dialog2 (_("Warning"),
195 _("The file has been modified in the meantime. Save anyway?"),
196 _("&Yes"), _("&Cancel"));
199 vfs_path_free (real_filename_vpath
);
205 if (this_save_mode
!= EDIT_QUICK_SAVE
)
207 char *savedir
, *saveprefix
;
209 savedir
= vfs_path_tokens_get (real_filename_vpath
, 0, -1);
211 savedir
= g_strdup (".");
213 saveprefix
= mc_build_filename (savedir
, "cooledit", NULL
);
215 fd
= mc_mkstemps (&savename_vpath
, saveprefix
, NULL
);
217 if (savename_vpath
== NULL
)
219 vfs_path_free (real_filename_vpath
);
223 * Close for now because mc_mkstemps use pure open system call
224 * to create temporary file and it needs to be reopened by
225 * VFS-aware mc_open().
230 savename_vpath
= vfs_path_clone (real_filename_vpath
);
232 (void) mc_chown (savename_vpath
, edit
->stat1
.st_uid
, edit
->stat1
.st_gid
);
233 (void) mc_chmod (savename_vpath
, edit
->stat1
.st_mode
);
235 fd
= mc_open (savename_vpath
, O_CREAT
| O_WRONLY
| O_TRUNC
| O_BINARY
, edit
->stat1
.st_mode
);
240 p
= edit_get_write_filter (savename_vpath
, real_filename_vpath
);
246 file
= (FILE *) popen (p
, "w");
250 filelen
= edit_write_stream (edit
, file
);
254 if (pclose (file
) != 0)
256 tmp
= g_strdup_printf (_("Error writing to pipe: %s"), p
);
257 edit_error_dialog (_("Error"), tmp
);
266 tmp
= g_strdup_printf (_("Cannot open pipe for writing: %s"), p
);
267 edit_error_dialog (_("Error"), get_sys_error (tmp
));
274 else if (edit
->lb
== LB_ASIS
)
275 { /* do not change line breaks */
278 filelen
= edit
->last_byte
;
279 while (buf
<= (edit
->curs1
>> S_EDIT_BUF_SIZE
) - 1)
281 if (mc_write (fd
, (char *) edit
->buffers1
[buf
], EDIT_BUF_SIZE
) != EDIT_BUF_SIZE
)
289 (fd
, (char *) edit
->buffers1
[buf
],
290 edit
->curs1
& M_EDIT_BUF_SIZE
) != (edit
->curs1
& M_EDIT_BUF_SIZE
))
294 else if (edit
->curs2
)
297 buf
= (edit
->curs2
>> S_EDIT_BUF_SIZE
);
300 (char *) edit
->buffers2
[buf
] + EDIT_BUF_SIZE
-
301 (edit
->curs2
& M_EDIT_BUF_SIZE
) - 1,
302 1 + (edit
->curs2
& M_EDIT_BUF_SIZE
)) != 1 + (edit
->curs2
& M_EDIT_BUF_SIZE
))
310 if (mc_write (fd
, (char *) edit
->buffers2
[buf
], EDIT_BUF_SIZE
) != EDIT_BUF_SIZE
)
322 /* Update the file information, especially the mtime. */
323 if (mc_stat (savename_vpath
, &edit
->stat1
) == -1)
327 { /* change line breaks */
329 const vfs_path_element_t
*path_element
;
333 path_element
= vfs_path_get_by_index (savename_vpath
, -1);
334 file
= (FILE *) fopen (path_element
->path
, "w");
337 filelen
= edit_write_stream (edit
, file
);
344 msg
= g_strdup_printf (_("Cannot open file for writing: %s"), path_element
->path
);
345 edit_error_dialog (_("Error"), msg
);
351 if (filelen
!= edit
->last_byte
)
354 if (this_save_mode
== EDIT_DO_BACKUP
)
356 vfs_path_t
*tmp_vpath
;
359 assert (option_backup_ext
!= NULL
);
360 tmp_vpath
= vfs_path_append_new (real_filename_vpath
, option_backup_ext
, (char *) NULL
);
361 ok
= (mc_rename (real_filename_vpath
, tmp_vpath
) != -1);
362 vfs_path_free (tmp_vpath
);
367 if (this_save_mode
!= EDIT_QUICK_SAVE
)
368 if (mc_rename (savename_vpath
, real_filename_vpath
) == -1)
371 vfs_path_free (real_filename_vpath
);
372 vfs_path_free (savename_vpath
);
375 /* FIXME: Is this safe ?
376 * if (this_save_mode != EDIT_QUICK_SAVE)
377 * mc_unlink (savename);
379 vfs_path_free (real_filename_vpath
);
380 vfs_path_free (savename_vpath
);
384 /* --------------------------------------------------------------------------------------------- */
387 edit_check_newline (WEdit
* edit
)
389 return !(option_check_nl_at_eof
&& edit
->last_byte
> 0
390 && edit_get_byte (edit
, edit
->last_byte
- 1) != '\n'
391 && edit_query_dialog2 (_("Warning"),
392 _("The file you are saving is not finished with a newline"),
393 _("C&ontinue"), _("&Cancel")));
396 /* --------------------------------------------------------------------------------------------- */
399 edit_get_save_file_as (WEdit
* edit
)
402 #define DLG_HEIGHT 14
404 static LineBreaks cur_lb
= LB_ASIS
;
406 char *filename
= vfs_path_to_str (edit
->filename_vpath
);
409 const char *lb_names
[LB_NAMES
] = {
410 N_("&Do not change"),
411 N_("&Unix format (LF)"),
412 N_("&Windows/DOS format (CR LF)"),
413 N_("&Macintosh format (CR)")
416 QuickWidget quick_widgets
[] = {
417 QUICK_BUTTON (6, 10, DLG_HEIGHT
- 3, DLG_HEIGHT
, N_("&Cancel"), B_CANCEL
, NULL
),
418 QUICK_BUTTON (2, 10, DLG_HEIGHT
- 3, DLG_HEIGHT
, N_("&OK"), B_ENTER
, NULL
),
419 QUICK_RADIO (5, DLG_WIDTH
, DLG_HEIGHT
- 8, DLG_HEIGHT
, LB_NAMES
, lb_names
, (int *) &cur_lb
),
420 QUICK_LABEL (3, DLG_WIDTH
, DLG_HEIGHT
- 9, DLG_HEIGHT
, N_("Change line breaks to:")),
421 QUICK_INPUT (3, DLG_WIDTH
, DLG_HEIGHT
- 11, DLG_HEIGHT
, filename
, DLG_WIDTH
- 6, 0,
422 "save-as", &filename_res
),
423 QUICK_LABEL (3, DLG_WIDTH
, DLG_HEIGHT
- 12, DLG_HEIGHT
, N_("Enter file name:")),
427 QuickDialog Quick_options
= {
428 DLG_WIDTH
, DLG_HEIGHT
, -1, -1,
429 N_("Save As"), "[Save File As]",
430 quick_widgets
, NULL
, FALSE
433 if (quick_dialog (&Quick_options
) != B_CANCEL
)
436 vfs_path_t
*ret_vpath
;
439 fname
= tilde_expand (filename_res
);
440 g_free (filename_res
);
441 ret_vpath
= vfs_path_from_str (fname
);
453 /* --------------------------------------------------------------------------------------------- */
455 /** returns 1 on success */
458 edit_save_cmd (WEdit
* edit
)
460 int res
, save_lock
= 0;
462 if (!edit
->locked
&& !edit
->delete_file
)
463 save_lock
= lock_file (edit
->filename_vpath
);
464 res
= edit_save_file (edit
, edit
->filename_vpath
);
466 /* Maintain modify (not save) lock on failure */
467 if ((res
> 0 && edit
->locked
) || save_lock
)
468 edit
->locked
= unlock_file (edit
->filename_vpath
);
470 /* On failure try 'save as', it does locking on its own */
472 return edit_save_as_cmd (edit
);
473 edit
->force
|= REDRAW_COMPLETELY
;
476 edit
->delete_file
= 0;
483 /* --------------------------------------------------------------------------------------------- */
484 /** returns 1 on error */
487 edit_load_file_from_filename (WEdit
* edit
, const vfs_path_t
* exp_vpath
)
489 int prev_locked
= edit
->locked
;
490 vfs_path_t
*prev_filename
;
492 prev_filename
= vfs_path_clone (edit
->filename_vpath
);
493 if (!edit_reload (edit
, exp_vpath
))
495 vfs_path_free (prev_filename
);
501 vfs_path_t
*fullpath
;
503 fullpath
= vfs_path_append_vpath_new (edit
->dir_vpath
, prev_filename
, (char *) NULL
);
504 unlock_file (fullpath
);
505 vfs_path_free (fullpath
);
507 vfs_path_free (prev_filename
);
511 /* --------------------------------------------------------------------------------------------- */
514 edit_load_syntax_file (WEdit
* edit
)
516 vfs_path_t
*extdir_vpath
;
521 dir
= query_dialog (_("Syntax file edit"),
522 _("Which syntax file you want to edit?"), D_NORMAL
, 2,
523 _("&User"), _("&System Wide"));
527 vfs_path_build_filename (mc_global
.sysconfig_dir
, "syntax", "Syntax", (char *) NULL
);
528 if (!exist_file (vfs_path_get_last_path_str (extdir_vpath
)))
530 vfs_path_free (extdir_vpath
);
532 vfs_path_build_filename (mc_global
.share_data_dir
, "syntax", "Syntax", (char *) NULL
);
537 vfs_path_t
*user_syntax_file_vpath
;
539 user_syntax_file_vpath
= mc_config_get_full_vpath (EDIT_SYNTAX_FILE
);
540 check_for_default (extdir_vpath
, user_syntax_file_vpath
);
541 edit_load_file_from_filename (edit
, user_syntax_file_vpath
);
542 vfs_path_free (user_syntax_file_vpath
);
545 edit_load_file_from_filename (edit
, extdir_vpath
);
547 vfs_path_free (extdir_vpath
);
550 /* --------------------------------------------------------------------------------------------- */
553 edit_load_menu_file (WEdit
* edit
)
555 vfs_path_t
*buffer_vpath
;
556 vfs_path_t
*menufile_vpath
;
559 dir
= query_dialog (_("Menu edit"),
560 _("Which menu file do you want to edit?"), D_NORMAL
,
561 geteuid () != 0 ? 2 : 3, _("&Local"), _("&User"), _("&System Wide"));
563 menufile_vpath
= vfs_path_build_filename (mc_global
.sysconfig_dir
, EDIT_GLOBAL_MENU
, NULL
);
565 if (!exist_file (vfs_path_get_last_path_str (menufile_vpath
)))
567 vfs_path_free (menufile_vpath
);
568 menufile_vpath
= vfs_path_build_filename (mc_global
.share_data_dir
, EDIT_GLOBAL_MENU
, NULL
);
574 buffer_vpath
= vfs_path_from_str (EDIT_LOCAL_MENU
);
575 check_for_default (menufile_vpath
, buffer_vpath
);
576 chmod (vfs_path_get_last_path_str (buffer_vpath
), 0600);
580 buffer_vpath
= mc_config_get_full_vpath (EDIT_HOME_MENU
);
581 check_for_default (menufile_vpath
, buffer_vpath
);
585 buffer_vpath
= vfs_path_build_filename (mc_global
.sysconfig_dir
, EDIT_GLOBAL_MENU
, NULL
);
586 if (!exist_file (vfs_path_get_last_path_str (buffer_vpath
)))
588 vfs_path_free (buffer_vpath
);
590 vfs_path_build_filename (mc_global
.share_data_dir
, EDIT_GLOBAL_MENU
, NULL
);
595 vfs_path_free (menufile_vpath
);
599 edit_load_file_from_filename (edit
, buffer_vpath
);
601 vfs_path_free (buffer_vpath
);
602 vfs_path_free (menufile_vpath
);
605 /* --------------------------------------------------------------------------------------------- */
608 edit_delete_column_of_text (WEdit
* edit
)
610 long p
, q
, r
, m1
, m2
;
613 eval_marks (edit
, &m1
, &m2
);
614 n
= edit_move_forward (edit
, m1
, 0, m2
) + 1;
615 c
= edit_move_forward3 (edit
, edit_bol (edit
, m1
), 0, m1
);
616 d
= edit_move_forward3 (edit
, edit_bol (edit
, m2
), 0, m2
);
617 b
= max (min (c
, d
), min (edit
->column1
, edit
->column2
));
618 c
= max (c
, max (edit
->column1
, edit
->column2
));
622 r
= edit_bol (edit
, edit
->curs1
);
623 p
= edit_move_forward3 (edit
, r
, b
, 0);
624 q
= edit_move_forward3 (edit
, r
, c
, 0);
629 edit_cursor_move (edit
, p
- edit
->curs1
);
632 /* delete line between margins */
633 if (edit_get_byte (edit
, edit
->curs1
) != '\n')
634 edit_delete (edit
, 1);
638 /* move to next line except on the last delete */
639 edit_cursor_move (edit
, edit_move_forward (edit
, edit
->curs1
, 1, 0) - edit
->curs1
);
643 /* --------------------------------------------------------------------------------------------- */
644 /** if success return 0 */
647 edit_block_delete (WEdit
* edit
)
650 long start_mark
, end_mark
;
651 int curs_pos
, line_width
;
652 long curs_line
, c1
, c2
;
654 if (eval_marks (edit
, &start_mark
, &end_mark
))
656 if (edit
->column_highlight
&& edit
->mark2
< 0)
657 edit_mark_cmd (edit
, 0);
658 if ((end_mark
- start_mark
) > option_max_undo
/ 2)
660 /* Warning message with a query to continue or cancel the operation */
661 if (edit_query_dialog2
664 ("Block is large, you may not be able to undo this action"),
665 _("C&ontinue"), _("&Cancel")))
670 c1
= min (edit
->column1
, edit
->column2
);
671 c2
= max (edit
->column1
, edit
->column2
);
675 edit_push_markers (edit
);
677 curs_line
= edit
->curs_line
;
679 /* calculate line width and cursor position before cut */
680 line_width
= edit_move_forward3 (edit
, edit_bol (edit
, edit
->curs1
), 0,
681 edit_eol (edit
, edit
->curs1
));
682 curs_pos
= edit
->curs_col
+ edit
->over_col
;
684 /* move cursor to start of selection */
685 edit_cursor_move (edit
, start_mark
- edit
->curs1
);
686 edit_scroll_screen_over_cursor (edit
);
688 if (start_mark
< end_mark
)
690 if (edit
->column_highlight
)
693 edit_mark_cmd (edit
, 0);
694 edit_delete_column_of_text (edit
);
695 /* move cursor to the saved position */
696 edit_move_to_line (edit
, curs_line
);
697 /* calculate line width after cut */
698 line_width
= edit_move_forward3 (edit
, edit_bol (edit
, edit
->curs1
), 0,
699 edit_eol (edit
, edit
->curs1
));
700 if (option_cursor_beyond_eol
&& curs_pos
> line_width
)
701 edit
->over_col
= curs_pos
- line_width
;
705 while (count
< end_mark
)
707 edit_delete (edit
, 1);
712 edit_set_markers (edit
, 0, 0, 0, 0);
713 edit
->force
|= REDRAW_PAGE
;
717 /* --------------------------------------------------------------------------------------------- */
720 editcmd_find (WEdit
* edit
, gsize
* len
)
722 off_t search_start
= edit
->search_start
;
725 long end_mark
= edit
->last_byte
;
728 if (edit_search_options
.only_in_selection
)
730 mark_res
= eval_marks (edit
, &start_mark
, &end_mark
);
733 edit
->search
->error
= MC_SEARCH_E_NOTFOUND
;
734 edit
->search
->error_str
= g_strdup (_("Search string not found"));
737 if (edit_search_options
.backwards
)
739 if (search_start
> end_mark
|| search_start
<= start_mark
)
741 search_start
= end_mark
;
746 if (search_start
< start_mark
|| search_start
>= end_mark
)
748 search_start
= start_mark
;
754 if (edit_search_options
.backwards
)
755 end_mark
= max (1, edit
->curs1
) - 1;
757 if (edit_search_options
.backwards
)
759 search_end
= end_mark
;
760 while ((int) search_start
>= start_mark
)
762 if (search_end
> (off_t
) (search_start
+ edit
->search
->original_len
) &&
763 mc_search_is_fixed_search_str (edit
->search
))
765 search_end
= search_start
+ edit
->search
->original_len
;
767 if (mc_search_run (edit
->search
, (void *) edit
, search_start
, search_end
, len
)
768 && edit
->search
->normal_offset
== search_start
)
774 edit
->search
->error_str
= g_strdup (_("Search string not found"));
778 return mc_search_run (edit
->search
, (void *) edit
, search_start
, end_mark
, len
);
783 /* --------------------------------------------------------------------------------------------- */
786 edit_replace_cmd__conv_to_display (char *str
)
791 tmp
= str_convert_to_display (str
);
795 return g_string_free (tmp
, FALSE
);
796 g_string_free (tmp
, TRUE
);
799 return g_strdup (str
);
802 /* --------------------------------------------------------------------------------------------- */
805 edit_replace_cmd__conv_to_input (char *str
)
810 tmp
= str_convert_to_input (str
);
814 return g_string_free (tmp
, FALSE
);
815 g_string_free (tmp
, TRUE
);
818 return g_strdup (str
);
821 /* --------------------------------------------------------------------------------------------- */
824 edit_do_search (WEdit
* edit
)
828 if (edit
->search
== NULL
)
829 edit
->search_start
= edit
->curs1
;
831 edit_push_undo_action (edit
, KEY_PRESS
+ edit
->start_display
);
833 if (search_create_bookmark
)
835 int found
= 0, books
= 0;
836 long l
= 0, l_last
= -1;
839 search_create_bookmark
= FALSE
;
840 book_mark_flush (edit
, -1);
844 if (!mc_search_run (edit
->search
, (void *) edit
, q
, edit
->last_byte
, &len
))
847 edit
->search_start
= edit
->search
->normal_offset
;
849 l
+= edit_count_lines (edit
, q
, edit
->search
->normal_offset
);
852 book_mark_insert (edit
, l
, BOOK_MARK_FOUND_COLOR
);
856 q
= edit
->search
->normal_offset
+ 1;
860 edit_error_dialog (_("Search"), _("Search string not found"));
862 edit_cursor_move (edit
, edit
->search_start
- edit
->curs1
);
866 if (edit
->found_len
!= 0 && edit
->search_start
== edit
->found_start
+ 1
867 && edit_search_options
.backwards
)
868 edit
->search_start
--;
870 if (edit
->found_len
!= 0 && edit
->search_start
== edit
->found_start
- 1
871 && !edit_search_options
.backwards
)
872 edit
->search_start
++;
874 if (editcmd_find (edit
, &len
))
876 edit
->found_start
= edit
->search_start
= edit
->search
->normal_offset
;
877 edit
->found_len
= len
;
879 edit_cursor_move (edit
, edit
->search_start
- edit
->curs1
);
880 edit_scroll_screen_over_cursor (edit
);
881 if (edit_search_options
.backwards
)
882 edit
->search_start
--;
884 edit
->search_start
++;
888 edit
->search_start
= edit
->curs1
;
889 if (edit
->search
->error_str
!= NULL
)
890 edit_error_dialog (_("Search"), edit
->search
->error_str
);
894 edit
->force
|= REDRAW_COMPLETELY
;
895 edit_scroll_screen_over_cursor (edit
);
898 /* --------------------------------------------------------------------------------------------- */
901 edit_search (WEdit
* edit
)
903 if (editcmd_dialog_search_show (edit
))
904 edit_do_search (edit
);
907 /* --------------------------------------------------------------------------------------------- */
908 /** Return a null terminated length of text. Result must be g_free'd */
910 static unsigned char *
911 edit_get_block (WEdit
* edit
, long start
, long finish
, int *l
)
913 unsigned char *s
, *r
;
914 r
= s
= g_malloc0 (finish
- start
+ 1);
915 if (edit
->column_highlight
)
918 /* copy from buffer, excluding chars that are out of the column 'margins' */
919 while (start
< finish
)
923 x
= edit_move_forward3 (edit
, edit_bol (edit
, start
), 0, start
);
924 c
= edit_get_byte (edit
, start
);
925 if ((x
>= edit
->column1
&& x
< edit
->column2
)
926 || (x
>= edit
->column2
&& x
< edit
->column1
) || c
== '\n')
937 while (start
< finish
)
938 *s
++ = edit_get_byte (edit
, start
++);
944 /* --------------------------------------------------------------------------------------------- */
945 /** copies a block to clipboard file */
948 edit_save_block_to_clip_file (WEdit
* edit
, long start
, long finish
)
952 tmp
= mc_config_get_full_path (EDIT_CLIP_FILE
);
953 ret
= edit_save_block (edit
, tmp
, start
, finish
);
958 /* --------------------------------------------------------------------------------------------- */
961 pipe_mail (WEdit
* edit
, char *to
, char *subject
, char *cc
)
966 to
= name_quote (to
, 0);
967 subject
= name_quote (subject
, 0);
968 cc
= name_quote (cc
, 0);
969 s
= g_strconcat ("mail -s ", subject
, *cc
? " -c " : "", cc
, " ", to
, (char *) NULL
);
983 for (i
= 0; i
< edit
->last_byte
; i
++)
984 fputc (edit_get_byte (edit
, i
), p
);
989 /* --------------------------------------------------------------------------------------------- */
992 is_break_char (char c
)
994 return (isspace (c
) || strchr ("{}[]()<>=|/\\!?~-+`'\",.;:#$%^&*", c
));
997 /* --------------------------------------------------------------------------------------------- */
998 /** find first character of current word */
1001 edit_find_word_start (WEdit
* edit
, long *word_start
, gsize
* word_len
)
1006 /* return if at begin of file */
1007 if (edit
->curs1
<= 0)
1010 c
= (unsigned char) edit_get_byte (edit
, edit
->curs1
- 1);
1011 /* return if not at end or in word */
1012 if (is_break_char (c
))
1015 /* search start of word to be completed */
1018 /* return if at begin of file */
1019 if ((gsize
) edit
->curs1
< i
)
1023 c
= (unsigned char) edit_get_byte (edit
, edit
->curs1
- i
);
1025 if (is_break_char (c
))
1027 /* return if word starts with digit */
1031 *word_start
= edit
->curs1
- (i
- 1); /* start found */
1040 /* --------------------------------------------------------------------------------------------- */
1042 * Get current word under cursor
1044 * @param edit editor object
1045 * @param srch mc_search object
1046 * @param word_start start word position
1048 * @return newly allocated string or NULL if no any words under cursor
1052 edit_collect_completions_get_current_word (WEdit
* edit
, mc_search_t
* srch
, long word_start
)
1057 if (!mc_search_run (srch
, (void *) edit
, word_start
, edit
->last_byte
, &len
))
1060 temp
= g_string_sized_new (len
);
1062 for (i
= 0; i
< len
; i
++)
1066 chr
= edit_get_byte (edit
, word_start
+ i
);
1068 g_string_append_c (temp
, chr
);
1071 return g_string_free (temp
, temp
->len
== 0);
1074 /* --------------------------------------------------------------------------------------------- */
1075 /** collect the possible completions */
1078 edit_collect_completions (WEdit
* edit
, long word_start
, gsize word_len
,
1079 char *match_expr
, struct selection
*compl, gsize
* num
)
1087 long last_byte
, start
= -1;
1090 srch
= mc_search_new (match_expr
, -1);
1094 if (mc_config_get_bool
1095 (mc_main_config
, CONFIG_APP_SECTION
, "editor_wordcompletion_collect_entire_file", 0))
1097 last_byte
= edit
->last_byte
;
1101 last_byte
= word_start
;
1104 srch
->search_type
= MC_SEARCH_T_REGEX
;
1105 srch
->is_case_sensitive
= TRUE
;
1106 srch
->search_fn
= edit_search_cmd_callback
;
1108 current_word
= edit_collect_completions_get_current_word (edit
, srch
, word_start
);
1110 temp
= g_string_new ("");
1112 /* collect max MAX_WORD_COMPLETIONS completions */
1113 while (mc_search_run (srch
, (void *) edit
, start
+ 1, last_byte
, &len
))
1115 g_string_set_size (temp
, 0);
1116 start
= srch
->normal_offset
;
1118 /* add matched completion if not yet added */
1119 for (i
= 0; i
< len
; i
++)
1121 skip
= edit_get_byte (edit
, start
+ i
);
1125 /* skip current word */
1126 if (start
+ (long) i
== word_start
)
1129 g_string_append_c (temp
, skip
);
1135 if (current_word
!= NULL
&& strcmp (current_word
, temp
->str
) == 0)
1140 for (i
= 0; i
< *num
; i
++)
1143 ((char *) &compl[i
].text
[word_len
],
1144 (char *) &temp
->str
[word_len
], max (len
, compl[i
].len
) - word_len
) == 0)
1146 struct selection
this = compl[i
];
1147 for (++i
; i
< *num
; i
++)
1149 compl[i
- 1] = compl[i
];
1151 compl[*num
- 1] = this;
1153 break; /* skip it, already added */
1159 if (*num
== MAX_WORD_COMPLETIONS
)
1161 g_free (compl[0].text
);
1162 for (i
= 1; i
< *num
; i
++)
1164 compl[i
- 1] = compl[i
];
1171 recoded
= str_convert_to_display (temp
->str
);
1173 if (recoded
&& recoded
->len
)
1174 g_string_assign (temp
, recoded
->str
);
1176 g_string_free (recoded
, TRUE
);
1179 compl[*num
].text
= g_strdup (temp
->str
);
1180 compl[*num
].len
= temp
->len
;
1184 /* note the maximal length needed for the completion dialog */
1189 mc_search_free (srch
);
1190 g_string_free (temp
, TRUE
);
1191 g_free (current_word
);
1196 /* --------------------------------------------------------------------------------------------- */
1199 edit_insert_column_of_text (WEdit
* edit
, unsigned char *data
, int size
, int width
,
1200 long *start_pos
, long *end_pos
, int *col1
, int *col2
)
1205 cursor
= edit
->curs1
;
1206 col
= edit_get_col (edit
);
1208 for (i
= 0; i
< size
; i
++)
1210 if (data
[i
] != '\n')
1211 edit_insert (edit
, data
[i
]);
1213 { /* fill in and move to next line */
1217 if (edit_get_byte (edit
, edit
->curs1
) != '\n')
1219 l
= width
- (edit_get_col (edit
) - col
);
1222 edit_insert (edit
, ' ');
1226 for (p
= edit
->curs1
;; p
++)
1228 if (p
== edit
->last_byte
)
1230 edit_cursor_move (edit
, edit
->last_byte
- edit
->curs1
);
1231 edit_insert_ahead (edit
, '\n');
1235 if (edit_get_byte (edit
, p
) == '\n')
1241 edit_cursor_move (edit
, edit_move_forward3 (edit
, p
, col
, 0) - edit
->curs1
);
1242 l
= col
- edit_get_col (edit
);
1243 while (l
>= space_width
)
1245 edit_insert (edit
, ' ');
1252 *col2
= col
+ width
;
1253 *start_pos
= cursor
;
1254 *end_pos
= edit
->curs1
;
1255 edit_cursor_move (edit
, cursor
- edit
->curs1
);
1258 /* --------------------------------------------------------------------------------------------- */
1261 edit_macro_comparator (gconstpointer
* macro1
, gconstpointer
* macro2
)
1263 const macros_t
*m1
= (const macros_t
*) macro1
;
1264 const macros_t
*m2
= (const macros_t
*) macro2
;
1266 return m1
->hotkey
- m2
->hotkey
;
1269 /* --------------------------------------------------------------------------------------------- */
1272 edit_macro_sort_by_hotkey (void)
1274 if (macros_list
!= NULL
&& macros_list
->len
!= 0)
1275 g_array_sort (macros_list
, (GCompareFunc
) edit_macro_comparator
);
1278 /* --------------------------------------------------------------------------------------------- */
1281 edit_get_macro (WEdit
* edit
, int hotkey
, const macros_t
** macros
, guint
* indx
)
1283 const macros_t
*array_start
= &g_array_index (macros_list
, struct macros_t
, 0);
1285 macros_t search_macro
;
1289 search_macro
.hotkey
= hotkey
;
1290 result
= bsearch (&search_macro
, macros_list
->data
, macros_list
->len
,
1291 sizeof (macros_t
), (GCompareFunc
) edit_macro_comparator
);
1293 if (result
!= NULL
&& result
->macro
!= NULL
)
1295 *indx
= (result
- array_start
);
1303 /* --------------------------------------------------------------------------------------------- */
1304 /** returns FALSE on error */
1307 edit_delete_macro (WEdit
* edit
, int hotkey
)
1309 mc_config_t
*macros_config
= NULL
;
1310 const char *section_name
= "editor";
1311 gchar
*macros_fname
;
1314 const macros_t
*macros
= NULL
;
1316 /* clear array of actions for current hotkey */
1317 while (edit_get_macro (edit
, hotkey
, ¯os
, &indx
))
1319 if (macros
->macro
!= NULL
)
1320 g_array_free (macros
->macro
, TRUE
);
1322 g_array_remove_index (macros_list
, indx
);
1323 edit_macro_sort_by_hotkey ();
1326 macros_fname
= mc_config_get_full_path (MC_MACRO_FILE
);
1327 macros_config
= mc_config_init (macros_fname
);
1328 g_free (macros_fname
);
1330 if (macros_config
== NULL
)
1333 skeyname
= lookup_key_by_code (hotkey
);
1334 while (mc_config_del_key (macros_config
, section_name
, skeyname
))
1337 mc_config_save_file (macros_config
, NULL
);
1338 mc_config_deinit (macros_config
);
1343 /* --------------------------------------------------------------------------------------------- */
1344 /*** public functions ****************************************************************************/
1345 /* --------------------------------------------------------------------------------------------- */
1348 edit_help_cmd (WEdit
* edit
)
1350 ev_help_t event_data
= { NULL
, "[Internal File Editor]" };
1351 mc_event_raise (MCEVENT_GROUP_CORE
, "help", &event_data
);
1353 edit
->force
|= REDRAW_COMPLETELY
;
1356 /* --------------------------------------------------------------------------------------------- */
1359 edit_refresh_cmd (WEdit
* edit
)
1364 edit_get_syntax_color (edit
, -1, &color
);
1365 tty_touch_screen ();
1372 #endif /* !HAVE_SLANG */
1376 /* --------------------------------------------------------------------------------------------- */
1379 menu_save_mode_cmd (void)
1382 const int DLG_X
= 38;
1383 const int DLG_Y
= 13;
1387 const char *str
[] = {
1390 N_("&Do backups with following extension:")
1393 QuickWidget widgets
[] = {
1395 QUICK_BUTTON (18, DLG_X
, DLG_Y
- 3, DLG_Y
, N_("&Cancel"), B_CANCEL
, NULL
),
1397 QUICK_BUTTON (6, DLG_X
, DLG_Y
- 3, DLG_Y
, N_("&OK"), B_ENTER
, NULL
),
1399 QUICK_CHECKBOX (4, DLG_X
, 8, DLG_Y
, N_("Check &POSIX new line"), &option_check_nl_at_eof
),
1401 QUICK_INPUT (8, DLG_X
, 6, DLG_Y
, option_backup_ext
, 9, 0, "edit-backup-ext", &str_result
),
1403 QUICK_RADIO (4, DLG_X
, 3, DLG_Y
, 3, str
, &option_save_mode
),
1407 QuickDialog dialog
= {
1408 DLG_X
, DLG_Y
, -1, -1, N_("Edit Save Mode"),
1409 "[Edit Save Mode]", widgets
, NULL
, FALSE
1414 size_t w0
, w1
, b_len
, w3
;
1416 assert (option_backup_ext
!= NULL
);
1418 /* OK/Cancel buttons */
1419 w0
= str_term_width1 (_(widgets
[0].u
.button
.text
)) + 3;
1420 w1
= str_term_width1 (_(widgets
[1].u
.button
.text
)) + 5; /* default button */
1421 b_len
= w0
+ w1
+ 3;
1423 maxlen
= max (b_len
, (size_t) str_term_width1 (_(dialog
.title
)) + 2);
1426 for (i
= 0; i
< 3; i
++)
1431 w3
= max (w3
, (size_t) str_term_width1 (str
[i
]));
1434 maxlen
= max (maxlen
, w3
+ 4);
1436 dialog
.xlen
= min ((size_t) COLS
, maxlen
+ 8);
1438 widgets
[3].u
.input
.len
= w3
;
1439 widgets
[1].relative_x
= (dialog
.xlen
- b_len
) / 2;
1440 widgets
[0].relative_x
= widgets
[1].relative_x
+ w0
+ 2;
1442 for (i
= 0; i
< sizeof (widgets
) / sizeof (widgets
[0]); i
++)
1443 widgets
[i
].x_divisions
= dialog
.xlen
;
1445 if (quick_dialog (&dialog
) != B_CANCEL
)
1447 g_free (option_backup_ext
);
1448 option_backup_ext
= str_result
;
1452 /* --------------------------------------------------------------------------------------------- */
1455 edit_set_filename (WEdit
* edit
, const vfs_path_t
* name_vpath
)
1457 vfs_path_free (edit
->filename_vpath
);
1458 edit
->filename_vpath
= vfs_path_clone (name_vpath
);
1460 if (edit
->dir_vpath
== NULL
)
1461 edit
->dir_vpath
= vfs_path_clone (vfs_get_raw_current_dir ());
1464 /* --------------------------------------------------------------------------------------------- */
1465 /* Here we want to warn the users of overwriting an existing file,
1466 but only if they have made a change to the filename */
1467 /* returns 1 on success */
1469 edit_save_as_cmd (WEdit
* edit
)
1471 /* This heads the 'Save As' dialog box */
1472 vfs_path_t
*exp_vpath
;
1474 int different_filename
= 0;
1476 if (!edit_check_newline (edit
))
1479 exp_vpath
= edit_get_save_file_as (edit
);
1480 edit_push_undo_action (edit
, KEY_PRESS
+ edit
->start_display
);
1482 if (exp_vpath
!= NULL
)
1484 if (vfs_path_len (exp_vpath
) == 0)
1490 if (vfs_path_cmp (edit
->filename_vpath
, exp_vpath
) != 0)
1494 different_filename
= 1;
1495 file
= mc_open (exp_vpath
, O_RDONLY
| O_BINARY
);
1499 /* the file exists */
1501 /* Overwrite the current file or cancel the operation */
1502 if (edit_query_dialog2
1504 _("A file already exists with this name"), _("&Overwrite"), _("&Cancel")))
1509 edit
->stat1
.st_mode
|= S_IWUSR
;
1511 save_lock
= lock_file (exp_vpath
);
1515 /* filenames equal, check if already locked */
1516 if (!edit
->locked
&& !edit
->delete_file
)
1517 save_lock
= lock_file (exp_vpath
);
1520 if (different_filename
)
1523 * Allow user to write into saved (under another name) file
1524 * even if original file had r/o user permissions.
1526 edit
->stat1
.st_mode
|= S_IWRITE
;
1529 rv
= edit_save_file (edit
, exp_vpath
);
1533 /* Succesful, so unlock both files */
1534 if (different_filename
)
1537 unlock_file (exp_vpath
);
1539 edit
->locked
= unlock_file (edit
->filename_vpath
);
1543 if (edit
->locked
|| save_lock
)
1544 edit
->locked
= unlock_file (edit
->filename_vpath
);
1547 edit_set_filename (edit
, exp_vpath
);
1548 if (edit
->lb
!= LB_ASIS
)
1549 edit_reload (edit
, exp_vpath
);
1551 edit
->delete_file
= 0;
1552 if (different_filename
)
1553 edit_load_syntax (edit
, NULL
, edit
->syntax_type
);
1554 vfs_path_free (exp_vpath
);
1555 edit
->force
|= REDRAW_COMPLETELY
;
1558 edit_error_dialog (_("Save as"), get_sys_error (_("Cannot save file")));
1561 /* Failed, so maintain modify (not save) lock */
1563 unlock_file (exp_vpath
);
1570 vfs_path_free (exp_vpath
);
1571 edit
->force
|= REDRAW_COMPLETELY
;
1575 /* {{{ Macro stuff starts here */
1576 /* --------------------------------------------------------------------------------------------- */
1579 edit_delete_macro_cmd (WEdit
* edit
)
1583 hotkey
= editcmd_dialog_raw_key_query (_("Delete macro"), _("Press macro hotkey:"), 1);
1585 if (hotkey
!= 0 && !edit_delete_macro (edit
, hotkey
))
1586 message (D_ERROR
, _("Delete macro"), _("Macro not deleted"));
1589 /* --------------------------------------------------------------------------------------------- */
1591 /** returns FALSE on error */
1593 edit_execute_macro (WEdit
* edit
, int hotkey
)
1595 gboolean res
= FALSE
;
1599 const macros_t
*macros
;
1602 if (edit_get_macro (edit
, hotkey
, ¯os
, &indx
) &&
1603 macros
->macro
!= NULL
&& macros
->macro
->len
!= 0)
1607 edit
->force
|= REDRAW_PAGE
;
1609 for (i
= 0; i
< macros
->macro
->len
; i
++)
1611 const macro_action_t
*m_act
;
1613 m_act
= &g_array_index (macros
->macro
, struct macro_action_t
, i
);
1614 edit_execute_cmd (edit
, m_act
->action
, m_act
->ch
);
1619 edit_update_screen (edit
);
1623 /* --------------------------------------------------------------------------------------------- */
1625 /** returns FALSE on error */
1627 edit_store_macro_cmd (WEdit
* edit
)
1631 GString
*marcros_string
;
1632 mc_config_t
*macros_config
= NULL
;
1633 const char *section_name
= "editor";
1634 gchar
*macros_fname
;
1635 GArray
*macros
; /* current macro */
1637 gboolean have_macro
= FALSE
;
1638 char *skeyname
= NULL
;
1640 hotkey
= editcmd_dialog_raw_key_query (_("Save macro"), _("Press the macro's new hotkey:"), 1);
1641 if (hotkey
== ESC_CHAR
)
1644 tmp_act
= keybind_lookup_keymap_command (editor_map
, hotkey
);
1646 /* return FALSE if try assign macro into restricted hotkeys */
1647 if (tmp_act
== CK_MacroStartRecord
1648 || tmp_act
== CK_MacroStopRecord
|| tmp_act
== CK_MacroStartStopRecord
)
1651 edit_delete_macro (edit
, hotkey
);
1653 macros_fname
= mc_config_get_full_path (MC_MACRO_FILE
);
1654 macros_config
= mc_config_init (macros_fname
);
1655 g_free (macros_fname
);
1657 if (macros_config
== NULL
)
1660 edit_push_undo_action (edit
, KEY_PRESS
+ edit
->start_display
);
1662 marcros_string
= g_string_sized_new (250);
1663 macros
= g_array_new (TRUE
, FALSE
, sizeof (macro_action_t
));
1665 skeyname
= lookup_key_by_code (hotkey
);
1667 for (i
= 0; i
< macro_index
; i
++)
1669 macro_action_t m_act
;
1670 const char *action_name
;
1672 action_name
= keybind_lookup_actionname (record_macro_buf
[i
].action
);
1674 if (action_name
== NULL
)
1677 m_act
.action
= record_macro_buf
[i
].action
;
1678 m_act
.ch
= record_macro_buf
[i
].ch
;
1679 g_array_append_val (macros
, m_act
);
1681 g_string_append_printf (marcros_string
, "%s:%i;", action_name
,
1682 (int) record_macro_buf
[i
].ch
);
1687 macro
.hotkey
= hotkey
;
1688 macro
.macro
= macros
;
1689 g_array_append_val (macros_list
, macro
);
1690 mc_config_set_string (macros_config
, section_name
, skeyname
, marcros_string
->str
);
1693 mc_config_del_key (macros_config
, section_name
, skeyname
);
1696 edit_macro_sort_by_hotkey ();
1698 g_string_free (marcros_string
, TRUE
);
1699 mc_config_save_file (macros_config
, NULL
);
1700 mc_config_deinit (macros_config
);
1704 /* --------------------------------------------------------------------------------------------- */
1707 edit_repeat_macro_cmd (WEdit
* edit
)
1714 f
= input_dialog (_("Repeat last commands"), _("Repeat times:"), MC_HISTORY_EDIT_REPEAT
, NULL
);
1715 if (f
== NULL
|| *f
== '\0')
1721 count_repeat
= strtol (f
, &error
, 0);
1731 edit_push_undo_action (edit
, KEY_PRESS
+ edit
->start_display
);
1732 edit
->force
|= REDRAW_PAGE
;
1734 for (j
= 0; j
< count_repeat
; j
++)
1735 for (i
= 0; i
< macro_index
; i
++)
1736 edit_execute_cmd (edit
, record_macro_buf
[i
].action
, record_macro_buf
[i
].ch
);
1737 edit_update_screen (edit
);
1741 /* --------------------------------------------------------------------------------------------- */
1742 /** return FALSE on error */
1745 edit_load_macro_cmd (WEdit
* edit
)
1747 mc_config_t
*macros_config
= NULL
;
1748 gchar
**profile_keys
, **keys
;
1749 gchar
**values
, **curr_values
;
1750 gsize len
, values_len
;
1751 const char *section_name
= "editor";
1752 gchar
*macros_fname
;
1757 macros_fname
= mc_config_get_full_path (MC_MACRO_FILE
);
1758 macros_config
= mc_config_init (macros_fname
);
1759 g_free (macros_fname
);
1761 if (macros_config
== NULL
)
1764 profile_keys
= keys
= mc_config_get_keys (macros_config
, section_name
, &len
);
1765 while (*profile_keys
!= NULL
)
1767 gboolean have_macro
;
1771 macros
= g_array_new (TRUE
, FALSE
, sizeof (macro_action_t
));
1773 curr_values
= values
= mc_config_get_string_list (macros_config
, section_name
,
1774 *profile_keys
, &values_len
);
1775 hotkey
= lookup_key (*profile_keys
, NULL
);
1778 while (*curr_values
!= NULL
&& *curr_values
[0] != '\0')
1780 char **macro_pair
= NULL
;
1782 macro_pair
= g_strsplit (*curr_values
, ":", 2);
1784 if (macro_pair
!= NULL
)
1786 macro_action_t m_act
;
1787 if (macro_pair
[0] == NULL
|| macro_pair
[0][0] == '\0')
1791 m_act
.action
= keybind_lookup_action (macro_pair
[0]);
1792 g_free (macro_pair
[0]);
1793 macro_pair
[0] = NULL
;
1795 if (macro_pair
[1] == NULL
|| macro_pair
[1][0] == '\0')
1799 m_act
.ch
= strtol (macro_pair
[1], NULL
, 0);
1800 g_free (macro_pair
[1]);
1801 macro_pair
[1] = NULL
;
1803 if (m_act
.action
!= 0)
1805 /* a shell command */
1806 if ((m_act
.action
/ CK_PipeBlock (0)) == 1)
1808 m_act
.action
= CK_PipeBlock (0) + (m_act
.ch
> 0 ? m_act
.ch
: 0);
1811 g_array_append_val (macros
, m_act
);
1814 g_strfreev (macro_pair
);
1821 macro
.hotkey
= hotkey
;
1822 macro
.macro
= macros
;
1823 g_array_append_val (macros_list
, macro
);
1826 g_strfreev (values
);
1829 mc_config_deinit (macros_config
);
1830 edit_macro_sort_by_hotkey ();
1834 /* }}} Macro stuff end here */
1836 /* --------------------------------------------------------------------------------------------- */
1837 /** returns 1 on success */
1840 edit_save_confirm_cmd (WEdit
* edit
)
1844 if (edit
->filename_vpath
== NULL
)
1845 return edit_save_as_cmd (edit
);
1847 if (!edit_check_newline (edit
))
1850 if (edit_confirm_save
)
1855 filename
= vfs_path_to_str (edit
->filename_vpath
);
1856 f
= g_strdup_printf (_("Confirm save file: \"%s\""), filename
);
1858 ok
= (edit_query_dialog2 (_("Save file"), f
, _("&Save"), _("&Cancel")) == 0);
1863 return edit_save_cmd (edit
);
1866 /* --------------------------------------------------------------------------------------------- */
1867 /** returns 1 on success */
1870 edit_new_cmd (WEdit
* edit
)
1874 if (edit_query_dialog2
1877 ("Current text was modified without a file save.\nContinue discards these changes"),
1878 _("C&ontinue"), _("&Cancel")))
1880 edit
->force
|= REDRAW_COMPLETELY
;
1884 edit
->force
|= REDRAW_COMPLETELY
;
1886 return edit_renew (edit
); /* if this gives an error, something has really screwed up */
1889 /* --------------------------------------------------------------------------------------------- */
1892 edit_load_cmd (WEdit
* edit
, edit_current_file_t what
)
1895 && (edit_query_dialog2
1897 _("Current text was modified without a file save.\n"
1898 "Continue discards these changes"), _("C&ontinue"), _("&Cancel")) == 1))
1900 edit
->force
|= REDRAW_COMPLETELY
;
1906 case EDIT_FILE_COMMON
:
1908 char *filename
, *exp
;
1910 filename
= vfs_path_to_str (edit
->filename_vpath
);
1911 exp
= input_expand_dialog (_("Load"), _("Enter file name:"),
1912 MC_HISTORY_EDIT_LOAD
, filename
);
1919 vfs_path_t
*exp_vpath
;
1921 exp_vpath
= vfs_path_from_str (exp
);
1922 edit_load_file_from_filename (edit
, exp_vpath
);
1923 vfs_path_free (exp_vpath
);
1930 case EDIT_FILE_SYNTAX
:
1931 edit_load_syntax_file (edit
);
1934 case EDIT_FILE_MENU
:
1935 edit_load_menu_file (edit
);
1942 edit
->force
|= REDRAW_COMPLETELY
;
1946 /* --------------------------------------------------------------------------------------------- */
1948 if mark2 is -1 then marking is from mark1 to the cursor.
1949 Otherwise its between the markers. This handles this.
1950 Returns 1 if no text is marked.
1954 eval_marks (WEdit
* edit
, long *start_mark
, long *end_mark
)
1956 if (edit
->mark1
!= edit
->mark2
)
1958 long start_bol
, start_eol
;
1959 long end_bol
, end_eol
;
1964 if (edit
->end_mark_curs
< 0)
1965 end_mark_curs
= edit
->curs1
;
1967 end_mark_curs
= edit
->end_mark_curs
;
1969 if (edit
->mark2
>= 0)
1971 *start_mark
= min (edit
->mark1
, edit
->mark2
);
1972 *end_mark
= max (edit
->mark1
, edit
->mark2
);
1976 *start_mark
= min (edit
->mark1
, end_mark_curs
);
1977 *end_mark
= max (edit
->mark1
, end_mark_curs
);
1978 edit
->column2
= edit
->curs_col
+ edit
->over_col
;
1981 if (edit
->column_highlight
1982 && (((edit
->mark1
> end_mark_curs
) && (edit
->column1
< edit
->column2
))
1983 || ((edit
->mark1
< end_mark_curs
) && (edit
->column1
> edit
->column2
))))
1985 start_bol
= edit_bol (edit
, *start_mark
);
1986 start_eol
= edit_eol (edit
, start_bol
- 1) + 1;
1987 end_bol
= edit_bol (edit
, *end_mark
);
1988 end_eol
= edit_eol (edit
, *end_mark
);
1989 col1
= min (edit
->column1
, edit
->column2
);
1990 col2
= max (edit
->column1
, edit
->column2
);
1993 edit_move_forward3 (edit
, start_bol
, col2
, 0) - edit_move_forward3 (edit
, start_bol
,
1996 edit_move_forward3 (edit
, end_bol
, col2
, 0) - edit_move_forward3 (edit
, end_bol
,
1999 *start_mark
-= diff1
;
2001 *start_mark
= max (*start_mark
, start_eol
);
2002 *end_mark
= min (*end_mark
, end_eol
);
2008 *start_mark
= *end_mark
= 0;
2009 edit
->column2
= edit
->column1
= 0;
2014 /* --------------------------------------------------------------------------------------------- */
2017 edit_insert_over (WEdit
* edit
)
2021 for (i
= 0; i
< edit
->over_col
; i
++)
2023 edit_insert (edit
, ' ');
2028 /* --------------------------------------------------------------------------------------------- */
2031 edit_insert_column_of_text_from_file (WEdit
* edit
, int file
,
2032 long *start_pos
, long *end_pos
, int *col1
, int *col2
)
2036 int blocklen
= -1, width
= 0;
2037 unsigned char *data
;
2039 cursor
= edit
->curs1
;
2040 col
= edit_get_col (edit
);
2041 data
= g_malloc0 (TEMP_BUF_LEN
);
2043 while ((blocklen
= mc_read (file
, (char *) data
, TEMP_BUF_LEN
)) > 0)
2046 for (width
= 0; width
< blocklen
; width
++)
2048 if (data
[width
] == '\n')
2051 for (i
= 0; i
< blocklen
; i
++)
2053 if (data
[i
] == '\n')
2054 { /* fill in and move to next line */
2057 if (edit_get_byte (edit
, edit
->curs1
) != '\n')
2059 l
= width
- (edit_get_col (edit
) - col
);
2062 edit_insert (edit
, ' ');
2066 for (p
= edit
->curs1
;; p
++)
2068 if (p
== edit
->last_byte
)
2070 edit_cursor_move (edit
, edit
->last_byte
- edit
->curs1
);
2071 edit_insert_ahead (edit
, '\n');
2075 if (edit_get_byte (edit
, p
) == '\n')
2081 edit_cursor_move (edit
, edit_move_forward3 (edit
, p
, col
, 0) - edit
->curs1
);
2082 l
= col
- edit_get_col (edit
);
2083 while (l
>= space_width
)
2085 edit_insert (edit
, ' ');
2090 edit_insert (edit
, data
[i
]);
2094 *col2
= col
+ width
;
2095 *start_pos
= cursor
;
2096 *end_pos
= edit
->curs1
;
2097 edit_cursor_move (edit
, cursor
- edit
->curs1
);
2103 /* --------------------------------------------------------------------------------------------- */
2106 edit_block_copy_cmd (WEdit
* edit
)
2108 long start_mark
, end_mark
, current
= edit
->curs1
;
2113 unsigned char *copy_buf
;
2115 edit_update_curs_col (edit
);
2116 if (eval_marks (edit
, &start_mark
, &end_mark
))
2119 copy_buf
= edit_get_block (edit
, start_mark
, end_mark
, &size
);
2121 /* all that gets pushed are deletes hence little space is used on the stack */
2123 edit_push_markers (edit
);
2125 if (edit
->column_highlight
)
2127 col_delta
= abs (edit
->column2
- edit
->column1
);
2128 edit_insert_column_of_text (edit
, copy_buf
, size
, col_delta
, &mark1
, &mark2
, &c1
, &c2
);
2133 edit_insert_ahead (edit
, copy_buf
[size
]);
2137 edit_scroll_screen_over_cursor (edit
);
2139 if (edit
->column_highlight
)
2140 edit_set_markers (edit
, edit
->curs1
, mark2
, c1
, c2
);
2141 else if (start_mark
< current
&& end_mark
> current
)
2142 edit_set_markers (edit
, start_mark
, end_mark
+ end_mark
- start_mark
, 0, 0);
2144 edit
->force
|= REDRAW_PAGE
;
2148 /* --------------------------------------------------------------------------------------------- */
2151 edit_block_move_cmd (WEdit
* edit
)
2154 unsigned char *copy_buf
= NULL
;
2155 long start_mark
, end_mark
;
2158 if (eval_marks (edit
, &start_mark
, &end_mark
))
2161 line
= edit
->curs_line
;
2162 if (edit
->mark2
< 0)
2163 edit_mark_cmd (edit
, 0);
2164 edit_push_markers (edit
);
2166 if (edit
->column_highlight
)
2174 c1
= min (edit
->column1
, edit
->column2
);
2175 c2
= max (edit
->column1
, edit
->column2
);
2176 b_width
= (c2
- c1
);
2178 edit_update_curs_col (edit
);
2181 x2
= x
+ edit
->over_col
;
2183 /* do nothing when cursor inside first line of selected area */
2184 if ((edit_eol (edit
, edit
->curs1
) == edit_eol (edit
, start_mark
)) && (x2
> c1
&& x2
<= c2
))
2187 if (edit
->curs1
> start_mark
&& edit
->curs1
< edit_eol (edit
, end_mark
))
2191 else if (x
> c1
&& x
<= c2
)
2194 /* save current selection into buffer */
2195 copy_buf
= edit_get_block (edit
, start_mark
, end_mark
, &size
);
2197 /* remove current selection */
2198 edit_block_delete_cmd (edit
);
2200 edit
->over_col
= max (0, edit
->over_col
- b_width
);
2201 /* calculate the cursor pos after delete block */
2202 current
= edit_move_forward3 (edit
, edit_bol (edit
, edit
->curs1
), x
, 0);
2203 edit_cursor_move (edit
, current
- edit
->curs1
);
2204 edit_scroll_screen_over_cursor (edit
);
2206 /* add TWS if need before block insertion */
2207 if (option_cursor_beyond_eol
&& edit
->over_col
> 0)
2208 edit_insert_over (edit
);
2210 edit_insert_column_of_text (edit
, copy_buf
, size
, b_width
, &mark1
, &mark2
, &c1
, &c2
);
2211 edit_set_markers (edit
, mark1
, mark2
, c1
, c2
);
2217 current
= edit
->curs1
;
2218 copy_buf
= g_malloc0 (end_mark
- start_mark
);
2219 edit_cursor_move (edit
, start_mark
- edit
->curs1
);
2220 edit_scroll_screen_over_cursor (edit
);
2222 while (count
< end_mark
)
2224 copy_buf
[end_mark
- count
- 1] = edit_delete (edit
, 1);
2227 edit_scroll_screen_over_cursor (edit
);
2228 edit_cursor_move (edit
,
2229 current
- edit
->curs1
-
2230 (((current
- edit
->curs1
) > 0) ? end_mark
- start_mark
: 0));
2231 edit_scroll_screen_over_cursor (edit
);
2232 while (count
-- > start_mark
)
2233 edit_insert_ahead (edit
, copy_buf
[end_mark
- count
- 1]);
2234 edit_set_markers (edit
, edit
->curs1
, edit
->curs1
+ end_mark
- start_mark
, 0, 0);
2237 edit_scroll_screen_over_cursor (edit
);
2239 edit
->force
|= REDRAW_PAGE
;
2242 /* --------------------------------------------------------------------------------------------- */
2243 /** returns 1 if canceelled by user */
2246 edit_block_delete_cmd (WEdit
* edit
)
2248 long start_mark
, end_mark
;
2249 if (eval_marks (edit
, &start_mark
, &end_mark
))
2251 edit_delete_line (edit
);
2254 return edit_block_delete (edit
);
2257 /* --------------------------------------------------------------------------------------------- */
2258 /** call with edit = 0 before shutdown to close memory leaks */
2261 edit_replace_cmd (WEdit
* edit
, int again
)
2263 /* 1 = search string, 2 = replace with */
2264 static char *saved1
= NULL
; /* saved default[123] */
2265 static char *saved2
= NULL
;
2266 char *input1
= NULL
; /* user input from the dialog */
2267 char *input2
= NULL
;
2270 long times_replaced
= 0;
2271 gboolean once_found
= FALSE
;
2275 g_free (saved1
), saved1
= NULL
;
2276 g_free (saved2
), saved2
= NULL
;
2280 edit
->force
|= REDRAW_COMPLETELY
;
2282 if (again
&& !saved1
&& !saved2
)
2287 input1
= g_strdup (saved1
? saved1
: "");
2288 input2
= g_strdup (saved2
? saved2
: "");
2292 char *tmp_inp1
, *tmp_inp2
;
2293 disp1
= edit_replace_cmd__conv_to_display (saved1
? saved1
: (char *) "");
2294 disp2
= edit_replace_cmd__conv_to_display (saved2
? saved2
: (char *) "");
2296 edit_push_undo_action (edit
, KEY_PRESS
+ edit
->start_display
);
2298 editcmd_dialog_replace_show (edit
, disp1
, disp2
, &input1
, &input2
);
2303 if (input1
== NULL
|| *input1
== '\0')
2305 edit
->force
= REDRAW_COMPLETELY
;
2311 input1
= edit_replace_cmd__conv_to_input (input1
);
2312 input2
= edit_replace_cmd__conv_to_input (input2
);
2316 g_free (saved1
), saved1
= g_strdup (input1
);
2317 g_free (saved2
), saved2
= g_strdup (input2
);
2319 mc_search_free (edit
->search
);
2320 edit
->search
= NULL
;
2325 edit
->search
= mc_search_new (input1
, -1);
2326 if (edit
->search
== NULL
)
2328 edit
->search_start
= edit
->curs1
;
2331 edit
->search
->search_type
= edit_search_options
.type
;
2332 edit
->search
->is_all_charsets
= edit_search_options
.all_codepages
;
2333 edit
->search
->is_case_sensitive
= edit_search_options
.case_sens
;
2334 edit
->search
->whole_words
= edit_search_options
.whole_words
;
2335 edit
->search
->search_fn
= edit_search_cmd_callback
;
2338 if (edit
->found_len
&& edit
->search_start
== edit
->found_start
+ 1
2339 && edit_search_options
.backwards
)
2340 edit
->search_start
--;
2342 if (edit
->found_len
&& edit
->search_start
== edit
->found_start
- 1
2343 && !edit_search_options
.backwards
)
2344 edit
->search_start
++;
2350 if (!editcmd_find (edit
, &len
))
2352 if (!(edit
->search
->error
== MC_SEARCH_E_OK
||
2353 (once_found
&& edit
->search
->error
== MC_SEARCH_E_NOTFOUND
)))
2355 edit_error_dialog (_("Search"), edit
->search
->error_str
);
2361 edit
->search_start
= edit
->search
->normal_offset
;
2362 /*returns negative on not found or error in pattern */
2364 if ((edit
->search_start
>= 0) && (edit
->search_start
< edit
->last_byte
))
2367 GString
*tmp_str
, *repl_str
;
2369 edit
->found_start
= edit
->search_start
;
2370 i
= edit
->found_len
= len
;
2372 edit_cursor_move (edit
, edit
->search_start
- edit
->curs1
);
2373 edit_scroll_screen_over_cursor (edit
);
2375 if (edit
->replace_mode
== 0)
2380 l
= edit
->curs_row
- edit
->widget
.lines
/ 3;
2382 edit_scroll_downward (edit
, l
);
2384 edit_scroll_upward (edit
, -l
);
2386 edit_scroll_screen_over_cursor (edit
);
2387 edit
->force
|= REDRAW_PAGE
;
2388 edit_render_keypress (edit
);
2390 /*so that undo stops at each query */
2391 edit_push_key_press (edit
);
2392 /* and prompt 2/3 down */
2393 disp1
= edit_replace_cmd__conv_to_display (saved1
);
2394 disp2
= edit_replace_cmd__conv_to_display (saved2
);
2395 prompt
= editcmd_dialog_replace_prompt_show (edit
, disp1
, disp2
, -1, -1);
2399 if (prompt
== B_REPLACE_ALL
)
2400 edit
->replace_mode
= 1;
2401 else if (prompt
== B_SKIP_REPLACE
)
2403 if (edit_search_options
.backwards
)
2404 edit
->search_start
--;
2406 edit
->search_start
++;
2407 continue; /* loop */
2409 else if (prompt
== B_CANCEL
)
2411 edit
->replace_mode
= -1;
2416 /* don't process string each time */
2417 tmp_str
= g_string_new (input2
);
2418 repl_str
= mc_search_prepare_replace_str (edit
->search
, tmp_str
);
2419 g_string_free (tmp_str
, TRUE
);
2421 if (edit
->search
->error
!= MC_SEARCH_E_OK
)
2423 edit_error_dialog (_("Replace"), edit
->search
->error_str
);
2424 g_string_free (repl_str
, TRUE
);
2428 /* delete then insert new */
2429 for (i
= 0; i
< len
; i
++)
2430 edit_delete (edit
, 1);
2432 for (i
= 0; i
< repl_str
->len
; i
++)
2433 edit_insert (edit
, repl_str
->str
[i
]);
2435 edit
->found_len
= repl_str
->len
;
2436 g_string_free (repl_str
, TRUE
);
2439 /* so that we don't find the same string again */
2440 if (edit_search_options
.backwards
)
2441 edit
->search_start
--;
2444 edit
->search_start
+= edit
->found_len
;
2446 if (edit
->search_start
>= edit
->last_byte
)
2450 edit_scroll_screen_over_cursor (edit
);
2454 /* try and find from right here for next search */
2455 edit
->search_start
= edit
->curs1
;
2456 edit_update_curs_col (edit
);
2458 edit
->force
|= REDRAW_PAGE
;
2459 edit_render_keypress (edit
);
2461 if (times_replaced
== 0)
2462 query_dialog (_("Replace"), _("Search string not found"), D_NORMAL
, 1, _("&OK"));
2466 while (edit
->replace_mode
>= 0);
2468 edit_scroll_screen_over_cursor (edit
);
2469 edit
->force
|= REDRAW_COMPLETELY
;
2470 edit_render_keypress (edit
);
2472 if ((edit
->replace_mode
== 1) && (times_replaced
!= 0))
2473 message (D_NORMAL
, _("Replace"), _("%ld replacements made"), times_replaced
);
2480 /* --------------------------------------------------------------------------------------------- */
2483 edit_search_cmd_callback (const void *user_data
, gsize char_offset
)
2485 return edit_get_byte ((WEdit
*) user_data
, (long) char_offset
);
2488 /* --------------------------------------------------------------------------------------------- */
2491 edit_search_cmd (WEdit
* edit
, gboolean again
)
2498 else if (edit
->last_search_string
!= NULL
)
2499 edit_do_search (edit
);
2502 /* find last search string in history */
2505 history
= history_get (MC_HISTORY_SHARED_SEARCH
);
2506 if (history
!= NULL
&& history
->data
!= NULL
)
2508 edit
->last_search_string
= (char *) history
->data
;
2509 history
->data
= NULL
;
2510 history
= g_list_first (history
);
2511 g_list_foreach (history
, (GFunc
) g_free
, NULL
);
2512 g_list_free (history
);
2514 edit
->search
= mc_search_new (edit
->last_search_string
, -1);
2515 if (edit
->search
== NULL
)
2517 /* if not... then ask for an expression */
2518 g_free (edit
->last_search_string
);
2519 edit
->last_search_string
= NULL
;
2524 edit
->search
->search_type
= edit_search_options
.type
;
2525 edit
->search
->is_all_charsets
= edit_search_options
.all_codepages
;
2526 edit
->search
->is_case_sensitive
= edit_search_options
.case_sens
;
2527 edit
->search
->whole_words
= edit_search_options
.whole_words
;
2528 edit
->search
->search_fn
= edit_search_cmd_callback
;
2529 edit_do_search (edit
);
2534 /* if not... then ask for an expression */
2535 g_free (edit
->last_search_string
);
2536 edit
->last_search_string
= NULL
;
2543 /* --------------------------------------------------------------------------------------------- */
2545 * Check if it's OK to close the editor. If there are unsaved changes,
2546 * ask user. Return 1 if it's OK to exit, 0 to continue editing.
2550 edit_ok_to_exit (WEdit
* edit
)
2554 if (!edit
->modified
)
2557 if (!mc_global
.widget
.midnight_shutdown
)
2559 if (!edit_check_newline (edit
))
2563 act
= edit_query_dialog3 (_("Quit"), _("File was modified. Save with exit?"),
2564 _("&Yes"), _("&No"), _("&Cancel quit"));
2569 edit_query_dialog2 (_("Quit"),
2570 _("Midnight Commander is being shut down.\nSave modified file?"),
2571 _("&Yes"), _("&No"));
2581 edit_push_markers (edit
);
2582 edit_set_markers (edit
, 0, 0, 0, 0);
2583 if (!edit_save_cmd (edit
) || mc_global
.widget
.midnight_shutdown
)
2584 return mc_global
.widget
.midnight_shutdown
;
2588 case 2: /* Cancel quit */
2596 /* --------------------------------------------------------------------------------------------- */
2597 /** save block, returns 1 on success */
2600 edit_save_block (WEdit
* edit
, const char *filename
, long start
, long finish
)
2605 vpath
= vfs_path_from_str (filename
);
2606 file
= mc_open (vpath
, O_CREAT
| O_WRONLY
| O_TRUNC
,
2607 S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IROTH
| O_BINARY
);
2608 vfs_path_free (vpath
);
2612 if (edit
->column_highlight
)
2616 r
= mc_write (file
, VERTICAL_MAGIC
, sizeof (VERTICAL_MAGIC
));
2619 unsigned char *block
, *p
;
2621 p
= block
= edit_get_block (edit
, start
, finish
, &len
);
2624 r
= mc_write (file
, p
, len
);
2638 len
= finish
- start
;
2639 buf
= g_malloc0 (TEMP_BUF_LEN
);
2640 while (start
!= finish
)
2642 end
= min (finish
, start
+ TEMP_BUF_LEN
);
2643 for (; i
< end
; i
++)
2644 buf
[i
- start
] = edit_get_byte (edit
, i
);
2645 len
-= mc_write (file
, (char *) buf
, end
- start
);
2656 /* --------------------------------------------------------------------------------------------- */
2659 edit_paste_from_history (WEdit
* edit
)
2662 edit_error_dialog (_("Error"), _("This function is not implemented"));
2665 /* --------------------------------------------------------------------------------------------- */
2668 edit_copy_to_X_buf_cmd (WEdit
* edit
)
2670 long start_mark
, end_mark
;
2671 if (eval_marks (edit
, &start_mark
, &end_mark
))
2673 if (!edit_save_block_to_clip_file (edit
, start_mark
, end_mark
))
2675 edit_error_dialog (_("Copy to clipboard"), get_sys_error (_("Unable to save to file")));
2678 /* try use external clipboard utility */
2679 mc_event_raise (MCEVENT_GROUP_CORE
, "clipboard_file_to_ext_clip", NULL
);
2684 /* --------------------------------------------------------------------------------------------- */
2687 edit_cut_to_X_buf_cmd (WEdit
* edit
)
2689 long start_mark
, end_mark
;
2690 if (eval_marks (edit
, &start_mark
, &end_mark
))
2692 if (!edit_save_block_to_clip_file (edit
, start_mark
, end_mark
))
2694 edit_error_dialog (_("Cut to clipboard"), _("Unable to save to file"));
2697 /* try use external clipboard utility */
2698 mc_event_raise (MCEVENT_GROUP_CORE
, "clipboard_file_to_ext_clip", NULL
);
2700 edit_block_delete_cmd (edit
);
2701 edit_mark_cmd (edit
, 1);
2705 /* --------------------------------------------------------------------------------------------- */
2708 edit_paste_from_X_buf_cmd (WEdit
* edit
)
2712 /* try use external clipboard utility */
2713 mc_event_raise (MCEVENT_GROUP_CORE
, "clipboard_file_from_ext_clip", NULL
);
2714 tmp
= mc_config_get_full_vpath (EDIT_CLIP_FILE
);
2715 edit_insert_file (edit
, tmp
);
2716 vfs_path_free (tmp
);
2720 /* --------------------------------------------------------------------------------------------- */
2722 * Ask user for the line and go to that line.
2723 * Negative numbers mean line from the end (i.e. -1 is the last line).
2727 edit_goto_cmd (WEdit
* edit
)
2730 static long line
= 0; /* line as typed, saved as default */
2735 g_snprintf (s
, sizeof (s
), "%ld", line
);
2736 f
= input_dialog (_("Goto line"), _("Enter line:"), MC_HISTORY_EDIT_GOTO_LINE
, line
? s
: "");
2746 l
= strtol (f
, &error
, 0);
2755 l
= edit
->total_lines
+ l
+ 2;
2756 edit_move_display (edit
, l
- edit
->widget
.lines
/ 2 - 1);
2757 edit_move_to_line (edit
, l
- 1);
2758 edit
->force
|= REDRAW_COMPLETELY
;
2763 /* --------------------------------------------------------------------------------------------- */
2764 /** Return 1 on success */
2767 edit_save_block_cmd (WEdit
* edit
)
2769 long start_mark
, end_mark
;
2772 if (eval_marks (edit
, &start_mark
, &end_mark
))
2775 tmp
= mc_config_get_full_path (EDIT_CLIP_FILE
);
2777 input_expand_dialog (_("Save block"), _("Enter file name:"),
2778 MC_HISTORY_EDIT_SAVE_BLOCK
, tmp
);
2780 edit_push_undo_action (edit
, KEY_PRESS
+ edit
->start_display
);
2790 if (edit_save_block (edit
, exp
, start_mark
, end_mark
))
2793 edit
->force
|= REDRAW_COMPLETELY
;
2799 edit_error_dialog (_("Save block"), get_sys_error (_("Cannot save file")));
2803 edit
->force
|= REDRAW_COMPLETELY
;
2808 /* --------------------------------------------------------------------------------------------- */
2809 /** returns TRUE on success */
2812 edit_insert_file_cmd (WEdit
* edit
)
2816 gboolean ret
= FALSE
;
2818 tmp
= mc_config_get_full_path (EDIT_CLIP_FILE
);
2819 exp
= input_expand_dialog (_("Insert file"), _("Enter file name:"),
2820 MC_HISTORY_EDIT_INSERT_FILE
, tmp
);
2823 edit_push_undo_action (edit
, KEY_PRESS
+ edit
->start_display
);
2825 if (exp
!= NULL
&& *exp
!= '\0')
2827 vfs_path_t
*exp_vpath
;
2829 exp_vpath
= vfs_path_from_str (exp
);
2830 ret
= (edit_insert_file (edit
, exp_vpath
) >= 0);
2831 vfs_path_free (exp_vpath
);
2834 edit_error_dialog (_("Insert file"), get_sys_error (_("Cannot insert file")));
2839 edit
->force
|= REDRAW_COMPLETELY
;
2843 /* --------------------------------------------------------------------------------------------- */
2844 /** sorts a block, returns -1 on system fail, 1 on cancel and 0 on success */
2847 edit_sort_cmd (WEdit
* edit
)
2849 static char *old
= 0;
2850 char *exp
, *tmp
, *tmp_edit_block_name
, *tmp_edit_temp_name
;
2851 long start_mark
, end_mark
;
2854 if (eval_marks (edit
, &start_mark
, &end_mark
))
2856 edit_error_dialog (_("Sort block"), _("You must first highlight a block of text"));
2860 tmp
= mc_config_get_full_path (EDIT_BLOCK_FILE
);
2861 edit_save_block (edit
, tmp
, start_mark
, end_mark
);
2864 exp
= input_dialog (_("Run sort"),
2865 _("Enter sort options (see manpage) separated by whitespace:"),
2866 MC_HISTORY_EDIT_SORT
, (old
!= NULL
) ? old
: "");
2872 tmp_edit_block_name
= mc_config_get_full_path (EDIT_BLOCK_FILE
);
2873 tmp_edit_temp_name
= mc_config_get_full_path (EDIT_TEMP_FILE
);
2875 g_strconcat (" sort ", exp
, " ", tmp_edit_block_name
,
2876 " > ", tmp_edit_temp_name
, (char *) NULL
);
2877 g_free (tmp_edit_temp_name
);
2878 g_free (tmp_edit_block_name
);
2884 if (e
== -1 || e
== 127)
2886 edit_error_dialog (_("Sort"), get_sys_error (_("Cannot execute sort command")));
2891 sprintf (q
, "%d ", e
);
2892 tmp
= g_strdup_printf (_("Sort returned non-zero: %s"), q
);
2893 edit_error_dialog (_("Sort"), tmp
);
2899 edit
->force
|= REDRAW_COMPLETELY
;
2901 if (edit_block_delete_cmd (edit
))
2905 vfs_path_t
*tmp_vpath
;
2907 tmp_vpath
= mc_config_get_full_vpath (EDIT_TEMP_FILE
);
2908 edit_insert_file (edit
, tmp_vpath
);
2909 vfs_path_free (tmp_vpath
);
2914 /* --------------------------------------------------------------------------------------------- */
2916 * Ask user for a command, execute it and paste its output back to the
2921 edit_ext_cmd (WEdit
* edit
)
2923 char *exp
, *tmp
, *tmp_edit_temp_file
;
2927 input_dialog (_("Paste output of external command"),
2928 _("Enter shell command(s):"), MC_HISTORY_EDIT_PASTE_EXTCMD
, NULL
);
2933 tmp_edit_temp_file
= mc_config_get_full_path (EDIT_TEMP_FILE
);
2934 tmp
= g_strconcat (exp
, " > ", tmp_edit_temp_file
, (char *) NULL
);
2935 g_free (tmp_edit_temp_file
);
2942 edit_error_dialog (_("External command"), get_sys_error (_("Cannot execute command")));
2946 edit
->force
|= REDRAW_COMPLETELY
;
2949 vfs_path_t
*tmp_vpath
;
2951 tmp_vpath
= mc_config_get_full_vpath (EDIT_TEMP_FILE
);
2952 edit_insert_file (edit
, tmp_vpath
);
2953 vfs_path_free (tmp_vpath
);
2958 /* --------------------------------------------------------------------------------------------- */
2959 /** if block is 1, a block must be highlighted and the shell command
2960 processes it. If block is 0 the shell command is a straight system
2961 command, that just produces some output which is to be inserted */
2964 edit_block_process_cmd (WEdit
* edit
, int macro_number
)
2967 char *macros_fname
= NULL
;
2969 fname
= g_strdup_printf ("%s.%i.sh", MC_EXTMACRO_FILE
, macro_number
);
2970 macros_fname
= g_build_filename (mc_config_get_data_path (), fname
, (char *) NULL
);
2971 user_menu (edit
, macros_fname
, 0);
2973 g_free (macros_fname
);
2974 edit
->force
|= REDRAW_COMPLETELY
;
2977 /* --------------------------------------------------------------------------------------------- */
2980 edit_mail_dialog (WEdit
* edit
)
2983 char *tmail_subject
;
2986 static char *mail_cc_last
= 0;
2987 static char *mail_subject_last
= 0;
2988 static char *mail_to_last
= 0;
2990 QuickWidget quick_widgets
[] = {
2991 /* 0 */ QUICK_BUTTON (6, 10, 9, MAIL_DLG_HEIGHT
, N_("&Cancel"), B_CANCEL
, NULL
),
2992 /* 1 */ QUICK_BUTTON (2, 10, 9, MAIL_DLG_HEIGHT
, N_("&OK"), B_ENTER
, NULL
),
2993 /* 2 */ QUICK_INPUT (3, 50, 8, MAIL_DLG_HEIGHT
, "", 44, 0, "mail-dlg-input", &tmail_cc
),
2994 /* 3 */ QUICK_LABEL (3, 50, 7, MAIL_DLG_HEIGHT
, N_("Copies to")),
2995 /* 4 */ QUICK_INPUT (3, 50, 6, MAIL_DLG_HEIGHT
, "", 44, 0, "mail-dlg-input-2",
2997 /* 5 */ QUICK_LABEL (3, 50, 5, MAIL_DLG_HEIGHT
, N_("Subject")),
2998 /* 6 */ QUICK_INPUT (3, 50, 4, MAIL_DLG_HEIGHT
, "", 44, 0, "mail-dlg-input-3", &tmail_to
),
2999 /* 7 */ QUICK_LABEL (3, 50, 3, MAIL_DLG_HEIGHT
, N_("To")),
3000 /* 8 */ QUICK_LABEL (3, 50, 2, MAIL_DLG_HEIGHT
, N_("mail -s <subject> -c <cc> <to>")),
3004 QuickDialog Quick_input
= {
3005 50, MAIL_DLG_HEIGHT
, -1, -1, N_("Mail"),
3006 "[Input Line Keys]", quick_widgets
, NULL
, FALSE
3009 quick_widgets
[2].u
.input
.text
= mail_cc_last
? mail_cc_last
: "";
3010 quick_widgets
[4].u
.input
.text
= mail_subject_last
? mail_subject_last
: "";
3011 quick_widgets
[6].u
.input
.text
= mail_to_last
? mail_to_last
: "";
3013 if (quick_dialog (&Quick_input
) != B_CANCEL
)
3015 g_free (mail_cc_last
);
3016 g_free (mail_subject_last
);
3017 g_free (mail_to_last
);
3018 mail_cc_last
= tmail_cc
;
3019 mail_subject_last
= tmail_subject
;
3020 mail_to_last
= tmail_to
;
3021 pipe_mail (edit
, mail_to_last
, mail_subject_last
, mail_cc_last
);
3026 /*******************/
3027 /* Word Completion */
3028 /*******************/
3030 /* --------------------------------------------------------------------------------------------- */
3032 * Complete current word using regular expression search
3033 * backwards beginning at the current cursor position.
3037 edit_complete_word_cmd (WEdit
* edit
)
3039 gsize i
, max_len
, word_len
= 0, num_compl
= 0;
3040 long word_start
= 0;
3041 unsigned char *bufpos
;
3043 struct selection
compl[MAX_WORD_COMPLETIONS
]; /* completions */
3045 /* search start of word to be completed */
3046 if (!edit_find_word_start (edit
, &word_start
, &word_len
))
3049 /* prepare match expression */
3050 bufpos
= &edit
->buffers1
[word_start
>> S_EDIT_BUF_SIZE
][word_start
& M_EDIT_BUF_SIZE
];
3052 /* match_expr = g_strdup_printf ("\\b%.*s[a-zA-Z_0-9]+", word_len, bufpos); */
3055 ("(^|\\s+|\\b)%.*s[^\\s\\.=\\+\\[\\]\\(\\)\\,\\;\\:\\\"\\'\\-\\?\\/\\|\\\\\\{\\}\\*\\&\\^\\%%\\$#@\\!]+",
3056 (int) word_len
, bufpos
);
3058 /* collect the possible completions */
3059 /* start search from begin to end of file */
3061 edit_collect_completions (edit
, word_start
, word_len
, match_expr
,
3062 (struct selection
*) &compl, &num_compl
);
3066 /* insert completed word if there is only one match */
3069 for (i
= word_len
; i
< compl[0].len
; i
++)
3070 edit_insert (edit
, *(compl[0].text
+ i
));
3072 /* more than one possible completion => ask the user */
3075 /* !!! usually only a beep is expected and when <ALT-TAB> is !!! */
3076 /* !!! pressed again the selection dialog pops up, but that !!! */
3077 /* !!! seems to require a further internal state !!! */
3080 /* let the user select the preferred completion */
3081 editcmd_dialog_completion_show (edit
, max_len
, word_len
,
3082 (struct selection
*) &compl, num_compl
);
3086 g_free (match_expr
);
3087 /* release memory before return */
3088 for (i
= 0; i
< num_compl
; i
++)
3089 g_free (compl[i
].text
);
3092 /* --------------------------------------------------------------------------------------------- */
3095 edit_select_codepage_cmd (WEdit
* edit
)
3098 if (do_select_codepage ())
3099 edit_set_codeset (edit
);
3101 edit
->force
= REDRAW_COMPLETELY
;
3102 edit_refresh_cmd (edit
);
3108 /* --------------------------------------------------------------------------------------------- */
3111 edit_insert_literal_cmd (WEdit
* edit
)
3113 int char_for_insertion
= editcmd_dialog_raw_key_query (_("Insert literal"),
3114 _("Press any key:"), 0);
3115 edit_execute_key_command (edit
, -1, ascii_alpha_to_cntrl (char_for_insertion
));
3118 /* --------------------------------------------------------------------------------------------- */
3121 edit_begin_end_macro_cmd (WEdit
* edit
)
3123 /* edit is a pointer to the widget */
3126 unsigned long command
= macro_index
< 0 ? CK_MacroStartRecord
: CK_MacroStopRecord
;
3127 edit_execute_key_command (edit
, command
, -1);
3131 /* --------------------------------------------------------------------------------------------- */
3134 edit_begin_end_repeat_cmd (WEdit
* edit
)
3136 /* edit is a pointer to the widget */
3139 unsigned long command
= macro_index
< 0 ? CK_RepeatStartRecord
: CK_RepeatStopRecord
;
3140 edit_execute_key_command (edit
, command
, -1);
3144 /* --------------------------------------------------------------------------------------------- */
3147 edit_load_forward_cmd (WEdit
* edit
)
3151 if (edit_query_dialog2
3153 _("Current text was modified without a file save\n"
3154 "Continue discards these changes"), _("C&ontinue"), _("&Cancel")))
3156 edit
->force
|= REDRAW_COMPLETELY
;
3160 if (edit_stack_iterator
+ 1 < MAX_HISTORY_MOVETO
)
3162 if (edit_history_moveto
[edit_stack_iterator
+ 1].line
< 1)
3166 edit_stack_iterator
++;
3167 if (edit_history_moveto
[edit_stack_iterator
].filename_vpath
)
3169 edit_reload_line (edit
, edit_history_moveto
[edit_stack_iterator
].filename_vpath
,
3170 edit_history_moveto
[edit_stack_iterator
].line
);
3184 /* --------------------------------------------------------------------------------------------- */
3187 edit_load_back_cmd (WEdit
* edit
)
3191 if (edit_query_dialog2
3193 _("Current text was modified without a file save\n"
3194 "Continue discards these changes"), _("C&ontinue"), _("&Cancel")))
3196 edit
->force
|= REDRAW_COMPLETELY
;
3200 if (edit_stack_iterator
> 0)
3202 edit_stack_iterator
--;
3203 if (edit_history_moveto
[edit_stack_iterator
].filename_vpath
)
3205 edit_reload_line (edit
, edit_history_moveto
[edit_stack_iterator
].filename_vpath
,
3206 edit_history_moveto
[edit_stack_iterator
].line
);
3220 /* --------------------------------------------------------------------------------------------- */
3223 edit_get_match_keyword_cmd (WEdit
* edit
)
3225 gsize word_len
= 0, max_len
= 0;
3228 long word_start
= 0;
3229 unsigned char *bufpos
;
3233 char *tagfile
= NULL
;
3235 etags_hash_t def_hash
[MAX_DEFINITIONS
];
3237 for (i
= 0; i
< MAX_DEFINITIONS
; i
++)
3239 def_hash
[i
].filename
= NULL
;
3242 /* search start of word to be completed */
3243 if (!edit_find_word_start (edit
, &word_start
, &word_len
))
3246 /* prepare match expression */
3247 bufpos
= &edit
->buffers1
[word_start
>> S_EDIT_BUF_SIZE
][word_start
& M_EDIT_BUF_SIZE
];
3248 match_expr
= g_strdup_printf ("%.*s", (int) word_len
, bufpos
);
3250 ptr
= g_get_current_dir ();
3251 path
= g_strconcat (ptr
, G_DIR_SEPARATOR_S
, (char *) NULL
);
3254 /* Recursive search file 'TAGS' in parent dirs */
3257 ptr
= g_path_get_dirname (path
);
3261 tagfile
= mc_build_filename (path
, TAGS_NAME
, (char *) NULL
);
3262 if (exist_file (tagfile
))
3265 while (strcmp (path
, G_DIR_SEPARATOR_S
) != 0);
3270 etags_set_definition_hash (tagfile
, path
, match_expr
, (etags_hash_t
*) & def_hash
);
3275 max_len
= MAX_WIDTH_DEF_DIALOG
;
3279 editcmd_dialog_select_definition_show (edit
, match_expr
, max_len
, word_len
,
3280 (etags_hash_t
*) & def_hash
, num_def
);
3282 g_free (match_expr
);
3285 /* --------------------------------------------------------------------------------------------- */