2 Editor high level editing commands
4 Copyright (C) 1996, 1997, 1998, 2001, 2002, 2003, 2004, 2005, 2006,
6 The Free Software Foundation, Inc.
11 This file is part of the Midnight Commander.
13 The Midnight Commander is free software: you can redistribute it
14 and/or modify it under the terms of the GNU General Public License as
15 published by the Free Software Foundation, either version 3 of the License,
16 or (at your option) any later version.
18 The Midnight Commander is distributed in the hope that it will be useful,
19 but WITHOUT ANY WARRANTY; without even the implied warranty of
20 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 GNU General Public License for more details.
23 You should have received a copy of the GNU General Public License
24 along with this program. If not, see <http://www.gnu.org/licenses/>.
28 * \brief Source: editor high level editing commands
33 /* #define PIPE_BLOCKS_SO_READ_BYTE_BY_BYTE */
44 #include <sys/types.h>
52 #include "lib/global.h"
53 #include "lib/tty/tty.h"
54 #include "lib/tty/key.h" /* XCTRL */
55 #include "lib/mcconfig.h"
57 #include "lib/strutil.h" /* utf string functions */
59 #include "lib/util.h" /* tilde_expand() */
60 #include "lib/vfs/vfs.h"
61 #include "lib/widget.h"
62 #include "lib/event.h" /* mc_event_raise() */
64 #include "lib/charsets.h"
67 #include "src/history.h"
68 #include "src/setup.h" /* option_tab_spacing */
69 #include "src/main.h" /* mactos_t */
71 #include "src/selcodepage.h"
73 #include "src/keybind-defaults.h"
74 #include "src/util.h" /* check_for_default() */
75 #include "src/filemanager/layout.h" /* mc_refresh() */
77 #include "edit-impl.h"
78 #include "editwidget.h"
79 #include "editcmd_dialogs.h"
82 /*** global variables ****************************************************************************/
84 /* search and replace: */
85 int search_create_bookmark
= FALSE
;
87 /* queries on a save */
88 int edit_confirm_save
= 1;
90 /*** file scope macro definitions ****************************************************************/
94 #define TEMP_BUF_LEN 1024
98 /* thanks to Liviu Daia <daia@stoilow.imar.ro> for getting this
99 (and the above) routines to work properly - paul */
101 #define is_digit(x) ((x) >= '0' && (x) <= '9')
103 #define MAIL_DLG_HEIGHT 12
105 #define MAX_WORD_COMPLETIONS 100 /* in listbox */
107 /*** file scope type declarations ****************************************************************/
109 /*** file scope variables ************************************************************************/
111 /*** file scope functions ************************************************************************/
112 /* --------------------------------------------------------------------------------------------- */
114 /* If 0 (quick save) then a) create/truncate <filename> file,
115 b) save to <filename>;
116 if 1 (safe save) then a) save to <tempnam>,
117 b) rename <tempnam> to <filename>;
118 if 2 (do backups) then a) save to <tempnam>,
119 b) rename <filename> to <filename.backup_ext>,
120 c) rename <tempnam> to <filename>. */
122 /* returns 0 on error, -1 on abort */
125 edit_save_file (WEdit
* edit
, const vfs_path_t
* filename_vpath
)
130 int this_save_mode
, fd
= -1;
131 vfs_path_t
*real_filename_vpath
;
132 vfs_path_t
*savename_vpath
= NULL
;
133 const char *start_filename
;
134 const vfs_path_element_t
*vpath_element
;
136 vpath_element
= vfs_path_get_by_index (filename_vpath
, 0);
137 if (vpath_element
== NULL
)
140 start_filename
= vpath_element
->path
;
141 if (*start_filename
== '\0')
144 if (*start_filename
!= PATH_SEP
&& edit
->dir_vpath
!= NULL
)
146 real_filename_vpath
= vfs_path_append_vpath_new (edit
->dir_vpath
, filename_vpath
, NULL
);
150 real_filename_vpath
= vfs_path_clone (filename_vpath
);
153 this_save_mode
= option_save_mode
;
154 if (this_save_mode
!= EDIT_QUICK_SAVE
)
156 if (!vfs_file_is_local (real_filename_vpath
)
157 || (fd
= mc_open (real_filename_vpath
, O_RDONLY
| O_BINARY
)) == -1)
160 * The file does not exists yet, so no safe save or
161 * backup are necessary.
163 this_save_mode
= EDIT_QUICK_SAVE
;
169 if (this_save_mode
== EDIT_QUICK_SAVE
&& !edit
->skip_detach_prompt
)
174 rv
= mc_stat (real_filename_vpath
, &sb
);
175 if (rv
== 0 && sb
.st_nlink
> 1)
177 rv
= edit_query_dialog3 (_("Warning"),
178 _("File has hard-links. Detach before saving?"),
179 _("&Yes"), _("&No"), _("&Cancel"));
183 this_save_mode
= EDIT_SAFE_SAVE
;
186 edit
->skip_detach_prompt
= 1;
189 vfs_path_free (real_filename_vpath
);
194 /* Prevent overwriting changes from other editor sessions. */
195 if (rv
== 0 && edit
->stat1
.st_mtime
!= 0 && edit
->stat1
.st_mtime
!= sb
.st_mtime
)
197 /* The default action is "Cancel". */
200 rv
= edit_query_dialog2 (_("Warning"),
201 _("The file has been modified in the meantime. Save anyway?"),
202 _("&Yes"), _("&Cancel"));
205 vfs_path_free (real_filename_vpath
);
211 if (this_save_mode
!= EDIT_QUICK_SAVE
)
213 char *savedir
, *saveprefix
;
215 savedir
= vfs_path_tokens_get (real_filename_vpath
, 0, -1);
217 savedir
= g_strdup (".");
219 saveprefix
= mc_build_filename (savedir
, "cooledit", NULL
);
221 fd
= mc_mkstemps (&savename_vpath
, saveprefix
, NULL
);
223 if (savename_vpath
== NULL
)
225 vfs_path_free (real_filename_vpath
);
229 * Close for now because mc_mkstemps use pure open system call
230 * to create temporary file and it needs to be reopened by
231 * VFS-aware mc_open().
236 savename_vpath
= vfs_path_clone (real_filename_vpath
);
238 (void) mc_chown (savename_vpath
, edit
->stat1
.st_uid
, edit
->stat1
.st_gid
);
239 (void) mc_chmod (savename_vpath
, edit
->stat1
.st_mode
);
241 fd
= mc_open (savename_vpath
, O_CREAT
| O_WRONLY
| O_TRUNC
| O_BINARY
, edit
->stat1
.st_mode
);
246 p
= edit_get_write_filter (savename_vpath
, real_filename_vpath
);
252 file
= (FILE *) popen (p
, "w");
256 filelen
= edit_write_stream (edit
, file
);
260 if (pclose (file
) != 0)
262 tmp
= g_strdup_printf (_("Error writing to pipe: %s"), p
);
263 edit_error_dialog (_("Error"), tmp
);
272 tmp
= g_strdup_printf (_("Cannot open pipe for writing: %s"), p
);
273 edit_error_dialog (_("Error"), get_sys_error (tmp
));
280 else if (edit
->lb
== LB_ASIS
)
281 { /* do not change line breaks */
284 filelen
= edit
->last_byte
;
285 while (buf
<= (edit
->curs1
>> S_EDIT_BUF_SIZE
) - 1)
287 if (mc_write (fd
, (char *) edit
->buffers1
[buf
], EDIT_BUF_SIZE
) != EDIT_BUF_SIZE
)
295 (fd
, (char *) edit
->buffers1
[buf
],
296 edit
->curs1
& M_EDIT_BUF_SIZE
) != (edit
->curs1
& M_EDIT_BUF_SIZE
))
300 else if (edit
->curs2
)
303 buf
= (edit
->curs2
>> S_EDIT_BUF_SIZE
);
306 (char *) edit
->buffers2
[buf
] + EDIT_BUF_SIZE
-
307 (edit
->curs2
& M_EDIT_BUF_SIZE
) - 1,
308 1 + (edit
->curs2
& M_EDIT_BUF_SIZE
)) != 1 + (edit
->curs2
& M_EDIT_BUF_SIZE
))
316 if (mc_write (fd
, (char *) edit
->buffers2
[buf
], EDIT_BUF_SIZE
) != EDIT_BUF_SIZE
)
328 /* Update the file information, especially the mtime. */
329 if (mc_stat (savename_vpath
, &edit
->stat1
) == -1)
333 { /* change line breaks */
335 const vfs_path_element_t
*path_element
;
339 path_element
= vfs_path_get_by_index (savename_vpath
, -1);
340 file
= (FILE *) fopen (path_element
->path
, "w");
343 filelen
= edit_write_stream (edit
, file
);
350 msg
= g_strdup_printf (_("Cannot open file for writing: %s"), path_element
->path
);
351 edit_error_dialog (_("Error"), msg
);
357 if (filelen
!= edit
->last_byte
)
360 if (this_save_mode
== EDIT_DO_BACKUP
)
362 vfs_path_t
*tmp_vpath
;
366 assert (option_backup_ext
!= NULL
);
368 tmp_vpath
= vfs_path_append_new (real_filename_vpath
, option_backup_ext
, (char *) NULL
);
369 ok
= (mc_rename (real_filename_vpath
, tmp_vpath
) != -1);
370 vfs_path_free (tmp_vpath
);
375 if (this_save_mode
!= EDIT_QUICK_SAVE
)
376 if (mc_rename (savename_vpath
, real_filename_vpath
) == -1)
379 vfs_path_free (real_filename_vpath
);
380 vfs_path_free (savename_vpath
);
383 /* FIXME: Is this safe ?
384 * if (this_save_mode != EDIT_QUICK_SAVE)
385 * mc_unlink (savename);
387 vfs_path_free (real_filename_vpath
);
388 vfs_path_free (savename_vpath
);
392 /* --------------------------------------------------------------------------------------------- */
395 edit_check_newline (WEdit
* edit
)
397 return !(option_check_nl_at_eof
&& edit
->last_byte
> 0
398 && edit_get_byte (edit
, edit
->last_byte
- 1) != '\n'
399 && edit_query_dialog2 (_("Warning"),
400 _("The file you are saving is not finished with a newline"),
401 _("C&ontinue"), _("&Cancel")));
404 /* --------------------------------------------------------------------------------------------- */
407 edit_get_save_file_as (WEdit
* edit
)
410 #define DLG_HEIGHT 14
412 static LineBreaks cur_lb
= LB_ASIS
;
414 char *filename
= vfs_path_to_str (edit
->filename_vpath
);
417 const char *lb_names
[LB_NAMES
] = {
418 N_("&Do not change"),
419 N_("&Unix format (LF)"),
420 N_("&Windows/DOS format (CR LF)"),
421 N_("&Macintosh format (CR)")
424 QuickWidget quick_widgets
[] = {
425 QUICK_BUTTON (6, 10, DLG_HEIGHT
- 3, DLG_HEIGHT
, N_("&Cancel"), B_CANCEL
, NULL
),
426 QUICK_BUTTON (2, 10, DLG_HEIGHT
- 3, DLG_HEIGHT
, N_("&OK"), B_ENTER
, NULL
),
427 QUICK_RADIO (5, DLG_WIDTH
, DLG_HEIGHT
- 8, DLG_HEIGHT
, LB_NAMES
, lb_names
, (int *) &cur_lb
),
428 QUICK_LABEL (3, DLG_WIDTH
, DLG_HEIGHT
- 9, DLG_HEIGHT
, N_("Change line breaks to:")),
429 QUICK_INPUT (3, DLG_WIDTH
, DLG_HEIGHT
- 11, DLG_HEIGHT
, filename
, DLG_WIDTH
- 6, 0,
430 "save-as", &filename_res
),
431 QUICK_LABEL (3, DLG_WIDTH
, DLG_HEIGHT
- 12, DLG_HEIGHT
, N_("Enter file name:")),
435 QuickDialog Quick_options
= {
436 DLG_WIDTH
, DLG_HEIGHT
, -1, -1,
437 N_("Save As"), "[Save File As]",
438 quick_widgets
, NULL
, FALSE
441 if (quick_dialog (&Quick_options
) != B_CANCEL
)
444 vfs_path_t
*ret_vpath
;
447 fname
= tilde_expand (filename_res
);
448 g_free (filename_res
);
449 ret_vpath
= vfs_path_from_str (fname
);
461 /* --------------------------------------------------------------------------------------------- */
463 /** returns 1 on success */
466 edit_save_cmd (WEdit
* edit
)
468 int res
, save_lock
= 0;
470 if (!edit
->locked
&& !edit
->delete_file
)
471 save_lock
= lock_file (edit
->filename_vpath
);
472 res
= edit_save_file (edit
, edit
->filename_vpath
);
474 /* Maintain modify (not save) lock on failure */
475 if ((res
> 0 && edit
->locked
) || save_lock
)
476 edit
->locked
= unlock_file (edit
->filename_vpath
);
478 /* On failure try 'save as', it does locking on its own */
480 return edit_save_as_cmd (edit
);
481 edit
->force
|= REDRAW_COMPLETELY
;
484 edit
->delete_file
= 0;
491 /* --------------------------------------------------------------------------------------------- */
492 /** returns 1 on error */
495 edit_load_file_from_filename (WEdit
* edit
, const vfs_path_t
* exp_vpath
)
497 int prev_locked
= edit
->locked
;
498 vfs_path_t
*prev_filename
;
501 prev_filename
= vfs_path_clone (edit
->filename_vpath
);
502 if (!edit_reload (edit
, exp_vpath
))
504 else if (prev_locked
)
505 unlock_file (prev_filename
);
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 /* --------------------------------------------------------------------------------------------- */
719 * Get EOL symbol for searching.
721 * @param edit editor object
726 edit_search_get_current_end_line_char (const WEdit
* edit
)
737 /* --------------------------------------------------------------------------------------------- */
739 * Checking if search condition have BOL(^) or EOL ($) regexp special characters.
741 * @param search search object
742 * @return result of checks.
745 static edit_search_line_t
746 edit_get_search_line_type (mc_search_t
* search
)
748 edit_search_line_t search_line_type
= 0;
750 if (search
->search_type
!= MC_SEARCH_T_REGEX
)
751 return search_line_type
;
753 if (*search
->original
== '^')
754 search_line_type
|= AT_START_LINE
;
756 if (search
->original
[search
->original_len
- 1] == '$')
757 search_line_type
|= AT_END_LINE
;
758 return search_line_type
;
761 /* --------------------------------------------------------------------------------------------- */
763 * Calculating the start position of next line.
765 * @param edit editor object
766 * @param current_pos current position
767 * @param max_pos max position
768 * @param end_string_symbol end of line symbol
769 * @return start position of next line
773 edit_calculate_start_of_next_line (WEdit
* edit
, off_t current_pos
, off_t max_pos
,
774 char end_string_symbol
)
778 for (i
= current_pos
; i
< max_pos
; i
++)
781 if (edit_get_byte (edit
, i
) == end_string_symbol
)
788 /* --------------------------------------------------------------------------------------------- */
790 * Calculating the end position of previous line.
792 * @param edit editor object
793 * @param current_pos current position
794 * @param end_string_symbol end of line symbol
795 * @return end position of previous line
799 edit_calculate_end_of_previous_line (WEdit
* edit
, off_t current_pos
, char end_string_symbol
)
803 for (i
= current_pos
- 1; i
>= 0; i
--)
804 if (edit_get_byte (edit
, i
) == end_string_symbol
)
810 /* --------------------------------------------------------------------------------------------- */
812 * Calculating the start position of previous line.
814 * @param edit editor object
815 * @param current_pos current position
816 * @param end_string_symbol end of line symbol
817 * @return start position of previous line
821 edit_calculate_start_of_previous_line (WEdit
* edit
, off_t current_pos
, char end_string_symbol
)
823 current_pos
= edit_calculate_end_of_previous_line (edit
, current_pos
, end_string_symbol
);
824 current_pos
= edit_calculate_end_of_previous_line (edit
, current_pos
, end_string_symbol
);
826 return (current_pos
+ 1);
829 /* --------------------------------------------------------------------------------------------- */
831 * Calculating the start position of current line.
833 * @param edit editor object
834 * @param current_pos current position
835 * @param end_string_symbol end of line symbol
836 * @return start position of current line
840 edit_calculate_start_of_current_line (WEdit
* edit
, off_t current_pos
, char end_string_symbol
)
842 current_pos
= edit_calculate_end_of_previous_line (edit
, current_pos
, end_string_symbol
);
844 return (current_pos
+ 1);
847 /* --------------------------------------------------------------------------------------------- */
849 * Fixing (if needed) search start position if 'only in selection' option present.
851 * @param edit editor object
855 edit_search_fix_search_start_if_selection (WEdit
* edit
)
860 if (!edit_search_options
.only_in_selection
)
863 if (eval_marks (edit
, &start_mark
, &end_mark
) != 0)
866 if (edit_search_options
.backwards
)
868 if (edit
->search_start
> end_mark
|| edit
->search_start
<= start_mark
)
869 edit
->search_start
= end_mark
;
873 if (edit
->search_start
< start_mark
|| edit
->search_start
>= end_mark
)
874 edit
->search_start
= start_mark
;
878 /* --------------------------------------------------------------------------------------------- */
881 editcmd_find (WEdit
* edit
, gsize
* len
)
883 off_t search_start
= edit
->search_start
;
886 long end_mark
= edit
->last_byte
;
888 char end_string_symbol
;
890 end_string_symbol
= edit_search_get_current_end_line_char (edit
);
892 /* prepare for search */
893 if (edit_search_options
.only_in_selection
)
895 mark_res
= eval_marks (edit
, &start_mark
, &end_mark
);
898 edit
->search
->error
= MC_SEARCH_E_NOTFOUND
;
899 edit
->search
->error_str
= g_strdup (_("Search string not found"));
903 /* fix the start and the end of search block positions */
904 if ((edit
->search_line_type
& AT_START_LINE
) != 0
905 && (start_mark
!= 0 || edit_get_byte (edit
, start_mark
- 1) != end_string_symbol
))
908 edit_calculate_start_of_next_line (edit
, start_mark
, edit
->last_byte
,
911 if ((edit
->search_line_type
& AT_END_LINE
) != 0
912 && (end_mark
- 1 != edit
->last_byte
913 || edit_get_byte (edit
, end_mark
) != end_string_symbol
))
915 end_mark
= edit_calculate_end_of_previous_line (edit
, end_mark
, end_string_symbol
);
917 if (start_mark
>= end_mark
)
919 edit
->search
->error
= MC_SEARCH_E_NOTFOUND
;
920 edit
->search
->error_str
= g_strdup (_("Search string not found"));
926 if (edit_search_options
.backwards
)
927 end_mark
= max (1, edit
->curs1
) - 1;
931 if (edit_search_options
.backwards
)
933 /* backward search */
934 search_end
= end_mark
;
936 if ((edit
->search_line_type
& AT_START_LINE
) != 0)
938 edit_calculate_start_of_current_line (edit
, search_start
, end_string_symbol
);
940 while ((int) search_start
>= start_mark
)
942 if (search_end
> (off_t
) (search_start
+ edit
->search
->original_len
)
943 && mc_search_is_fixed_search_str (edit
->search
))
945 search_end
= search_start
+ edit
->search
->original_len
;
947 if (mc_search_run (edit
->search
, (void *) edit
, search_start
, search_end
, len
)
948 && edit
->search
->normal_offset
== search_start
)
954 if ((edit
->search_line_type
& AT_START_LINE
) != 0)
956 edit_calculate_start_of_previous_line (edit
, search_start
, end_string_symbol
);
960 edit
->search
->error_str
= g_strdup (_("Search string not found"));
965 if ((edit
->search_line_type
& AT_START_LINE
) != 0 && search_start
!= start_mark
)
967 edit_calculate_start_of_next_line (edit
, search_start
, end_mark
, end_string_symbol
);
968 return mc_search_run (edit
->search
, (void *) edit
, search_start
, end_mark
, len
);
973 /* --------------------------------------------------------------------------------------------- */
976 edit_replace_cmd__conv_to_display (char *str
)
981 tmp
= str_convert_to_display (str
);
985 return g_string_free (tmp
, FALSE
);
986 g_string_free (tmp
, TRUE
);
989 return g_strdup (str
);
992 /* --------------------------------------------------------------------------------------------- */
995 edit_replace_cmd__conv_to_input (char *str
)
1000 tmp
= str_convert_to_input (str
);
1004 return g_string_free (tmp
, FALSE
);
1005 g_string_free (tmp
, TRUE
);
1008 return g_strdup (str
);
1011 /* --------------------------------------------------------------------------------------------- */
1014 edit_do_search (WEdit
* edit
)
1018 if (edit
->search
== NULL
)
1019 edit
->search_start
= edit
->curs1
;
1021 edit_push_undo_action (edit
, KEY_PRESS
+ edit
->start_display
);
1023 if (search_create_bookmark
)
1025 int found
= 0, books
= 0;
1026 long l
= 0, l_last
= -1;
1029 search_create_bookmark
= FALSE
;
1030 book_mark_flush (edit
, -1);
1034 if (!mc_search_run (edit
->search
, (void *) edit
, q
, edit
->last_byte
, &len
))
1037 edit
->search_start
= edit
->search
->normal_offset
;
1039 l
+= edit_count_lines (edit
, q
, edit
->search
->normal_offset
);
1042 book_mark_insert (edit
, l
, BOOK_MARK_FOUND_COLOR
);
1046 q
= edit
->search
->normal_offset
+ 1;
1050 edit_error_dialog (_("Search"), _("Search string not found"));
1052 edit_cursor_move (edit
, edit
->search_start
- edit
->curs1
);
1056 if (edit
->found_len
!= 0 && edit
->search_start
== edit
->found_start
+ 1
1057 && edit_search_options
.backwards
)
1058 edit
->search_start
--;
1060 if (edit
->found_len
!= 0 && edit
->search_start
== edit
->found_start
- 1
1061 && !edit_search_options
.backwards
)
1062 edit
->search_start
++;
1064 if (editcmd_find (edit
, &len
))
1066 edit
->found_start
= edit
->search_start
= edit
->search
->normal_offset
;
1067 edit
->found_len
= len
;
1069 edit_cursor_move (edit
, edit
->search_start
- edit
->curs1
);
1070 edit_scroll_screen_over_cursor (edit
);
1071 if (edit_search_options
.backwards
)
1072 edit
->search_start
--;
1074 edit
->search_start
++;
1078 edit
->search_start
= edit
->curs1
;
1079 if (edit
->search
->error_str
!= NULL
)
1080 edit_error_dialog (_("Search"), edit
->search
->error_str
);
1084 edit
->force
|= REDRAW_COMPLETELY
;
1085 edit_scroll_screen_over_cursor (edit
);
1088 /* --------------------------------------------------------------------------------------------- */
1091 edit_search (WEdit
* edit
)
1093 if (editcmd_dialog_search_show (edit
))
1095 edit
->search_line_type
= edit_get_search_line_type (edit
->search
);
1096 edit_search_fix_search_start_if_selection (edit
);
1097 edit_do_search (edit
);
1101 /* --------------------------------------------------------------------------------------------- */
1102 /** Return a null terminated length of text. Result must be g_free'd */
1104 static unsigned char *
1105 edit_get_block (WEdit
* edit
, long start
, long finish
, int *l
)
1107 unsigned char *s
, *r
;
1108 r
= s
= g_malloc0 (finish
- start
+ 1);
1109 if (edit
->column_highlight
)
1112 /* copy from buffer, excluding chars that are out of the column 'margins' */
1113 while (start
< finish
)
1117 x
= edit_move_forward3 (edit
, edit_bol (edit
, start
), 0, start
);
1118 c
= edit_get_byte (edit
, start
);
1119 if ((x
>= edit
->column1
&& x
< edit
->column2
)
1120 || (x
>= edit
->column2
&& x
< edit
->column1
) || c
== '\n')
1130 *l
= finish
- start
;
1131 while (start
< finish
)
1132 *s
++ = edit_get_byte (edit
, start
++);
1138 /* --------------------------------------------------------------------------------------------- */
1139 /** copies a block to clipboard file */
1142 edit_save_block_to_clip_file (WEdit
* edit
, long start
, long finish
)
1146 tmp
= mc_config_get_full_path (EDIT_CLIP_FILE
);
1147 ret
= edit_save_block (edit
, tmp
, start
, finish
);
1152 /* --------------------------------------------------------------------------------------------- */
1155 pipe_mail (WEdit
* edit
, char *to
, char *subject
, char *cc
)
1160 to
= name_quote (to
, 0);
1161 subject
= name_quote (subject
, 0);
1162 cc
= name_quote (cc
, 0);
1163 s
= g_strconcat ("mail -s ", subject
, *cc
? " -c " : "", cc
, " ", to
, (char *) NULL
);
1177 for (i
= 0; i
< edit
->last_byte
; i
++)
1178 fputc (edit_get_byte (edit
, i
), p
);
1183 /* --------------------------------------------------------------------------------------------- */
1186 is_break_char (char c
)
1188 return (isspace (c
) || strchr ("{}[]()<>=|/\\!?~-+`'\",.;:#$%^&*", c
));
1191 /* --------------------------------------------------------------------------------------------- */
1192 /** find first character of current word */
1195 edit_find_word_start (WEdit
* edit
, long *word_start
, gsize
* word_len
)
1200 /* return if at begin of file */
1201 if (edit
->curs1
<= 0)
1204 c
= (unsigned char) edit_get_byte (edit
, edit
->curs1
- 1);
1205 /* return if not at end or in word */
1206 if (is_break_char (c
))
1209 /* search start of word to be completed */
1212 /* return if at begin of file */
1213 if ((gsize
) edit
->curs1
< i
)
1217 c
= (unsigned char) edit_get_byte (edit
, edit
->curs1
- i
);
1219 if (is_break_char (c
))
1221 /* return if word starts with digit */
1225 *word_start
= edit
->curs1
- (i
- 1); /* start found */
1234 /* --------------------------------------------------------------------------------------------- */
1236 * Get current word under cursor
1238 * @param edit editor object
1239 * @param srch mc_search object
1240 * @param word_start start word position
1242 * @return newly allocated string or NULL if no any words under cursor
1246 edit_collect_completions_get_current_word (WEdit
* edit
, mc_search_t
* srch
, long word_start
)
1251 if (!mc_search_run (srch
, (void *) edit
, word_start
, edit
->last_byte
, &len
))
1254 temp
= g_string_sized_new (len
);
1256 for (i
= 0; i
< len
; i
++)
1260 chr
= edit_get_byte (edit
, word_start
+ i
);
1262 g_string_append_c (temp
, chr
);
1265 return g_string_free (temp
, temp
->len
== 0);
1268 /* --------------------------------------------------------------------------------------------- */
1269 /** collect the possible completions */
1272 edit_collect_completions (WEdit
* edit
, long word_start
, gsize word_len
,
1273 char *match_expr
, struct selection
*compl, gsize
* num
)
1281 long last_byte
, start
= -1;
1284 srch
= mc_search_new (match_expr
, -1);
1288 if (mc_config_get_bool
1289 (mc_main_config
, CONFIG_APP_SECTION
, "editor_wordcompletion_collect_entire_file", 0))
1291 last_byte
= edit
->last_byte
;
1295 last_byte
= word_start
;
1298 srch
->search_type
= MC_SEARCH_T_REGEX
;
1299 srch
->is_case_sensitive
= TRUE
;
1300 srch
->search_fn
= edit_search_cmd_callback
;
1302 current_word
= edit_collect_completions_get_current_word (edit
, srch
, word_start
);
1304 temp
= g_string_new ("");
1306 /* collect max MAX_WORD_COMPLETIONS completions */
1307 while (mc_search_run (srch
, (void *) edit
, start
+ 1, last_byte
, &len
))
1309 g_string_set_size (temp
, 0);
1310 start
= srch
->normal_offset
;
1312 /* add matched completion if not yet added */
1313 for (i
= 0; i
< len
; i
++)
1315 skip
= edit_get_byte (edit
, start
+ i
);
1319 /* skip current word */
1320 if (start
+ (long) i
== word_start
)
1323 g_string_append_c (temp
, skip
);
1329 if (current_word
!= NULL
&& strcmp (current_word
, temp
->str
) == 0)
1334 for (i
= 0; i
< *num
; i
++)
1337 ((char *) &compl[i
].text
[word_len
],
1338 (char *) &temp
->str
[word_len
], max (len
, compl[i
].len
) - word_len
) == 0)
1340 struct selection
this = compl[i
];
1341 for (++i
; i
< *num
; i
++)
1343 compl[i
- 1] = compl[i
];
1345 compl[*num
- 1] = this;
1347 break; /* skip it, already added */
1353 if (*num
== MAX_WORD_COMPLETIONS
)
1355 g_free (compl[0].text
);
1356 for (i
= 1; i
< *num
; i
++)
1358 compl[i
- 1] = compl[i
];
1365 recoded
= str_convert_to_display (temp
->str
);
1367 if (recoded
&& recoded
->len
)
1368 g_string_assign (temp
, recoded
->str
);
1370 g_string_free (recoded
, TRUE
);
1373 compl[*num
].text
= g_strdup (temp
->str
);
1374 compl[*num
].len
= temp
->len
;
1378 /* note the maximal length needed for the completion dialog */
1383 mc_search_free (srch
);
1384 g_string_free (temp
, TRUE
);
1385 g_free (current_word
);
1390 /* --------------------------------------------------------------------------------------------- */
1393 edit_insert_column_of_text (WEdit
* edit
, unsigned char *data
, int size
, int width
,
1394 long *start_pos
, long *end_pos
, int *col1
, int *col2
)
1399 cursor
= edit
->curs1
;
1400 col
= edit_get_col (edit
);
1402 for (i
= 0; i
< size
; i
++)
1404 if (data
[i
] != '\n')
1405 edit_insert (edit
, data
[i
]);
1407 { /* fill in and move to next line */
1411 if (edit_get_byte (edit
, edit
->curs1
) != '\n')
1413 l
= width
- (edit_get_col (edit
) - col
);
1416 edit_insert (edit
, ' ');
1420 for (p
= edit
->curs1
;; p
++)
1422 if (p
== edit
->last_byte
)
1424 edit_cursor_move (edit
, edit
->last_byte
- edit
->curs1
);
1425 edit_insert_ahead (edit
, '\n');
1429 if (edit_get_byte (edit
, p
) == '\n')
1435 edit_cursor_move (edit
, edit_move_forward3 (edit
, p
, col
, 0) - edit
->curs1
);
1436 l
= col
- edit_get_col (edit
);
1437 while (l
>= space_width
)
1439 edit_insert (edit
, ' ');
1446 *col2
= col
+ width
;
1447 *start_pos
= cursor
;
1448 *end_pos
= edit
->curs1
;
1449 edit_cursor_move (edit
, cursor
- edit
->curs1
);
1452 /* --------------------------------------------------------------------------------------------- */
1455 edit_macro_comparator (gconstpointer
* macro1
, gconstpointer
* macro2
)
1457 const macros_t
*m1
= (const macros_t
*) macro1
;
1458 const macros_t
*m2
= (const macros_t
*) macro2
;
1460 return m1
->hotkey
- m2
->hotkey
;
1463 /* --------------------------------------------------------------------------------------------- */
1466 edit_macro_sort_by_hotkey (void)
1468 if (macros_list
!= NULL
&& macros_list
->len
!= 0)
1469 g_array_sort (macros_list
, (GCompareFunc
) edit_macro_comparator
);
1472 /* --------------------------------------------------------------------------------------------- */
1475 edit_get_macro (WEdit
* edit
, int hotkey
, const macros_t
** macros
, guint
* indx
)
1477 const macros_t
*array_start
= &g_array_index (macros_list
, struct macros_t
, 0);
1479 macros_t search_macro
;
1483 search_macro
.hotkey
= hotkey
;
1484 result
= bsearch (&search_macro
, macros_list
->data
, macros_list
->len
,
1485 sizeof (macros_t
), (GCompareFunc
) edit_macro_comparator
);
1487 if (result
!= NULL
&& result
->macro
!= NULL
)
1489 *indx
= (result
- array_start
);
1497 /* --------------------------------------------------------------------------------------------- */
1498 /** returns FALSE on error */
1501 edit_delete_macro (WEdit
* edit
, int hotkey
)
1503 mc_config_t
*macros_config
= NULL
;
1504 const char *section_name
= "editor";
1505 gchar
*macros_fname
;
1508 const macros_t
*macros
= NULL
;
1510 /* clear array of actions for current hotkey */
1511 while (edit_get_macro (edit
, hotkey
, ¯os
, &indx
))
1513 if (macros
->macro
!= NULL
)
1514 g_array_free (macros
->macro
, TRUE
);
1516 g_array_remove_index (macros_list
, indx
);
1517 edit_macro_sort_by_hotkey ();
1520 macros_fname
= mc_config_get_full_path (MC_MACRO_FILE
);
1521 macros_config
= mc_config_init (macros_fname
);
1522 g_free (macros_fname
);
1524 if (macros_config
== NULL
)
1527 skeyname
= lookup_key_by_code (hotkey
);
1528 while (mc_config_del_key (macros_config
, section_name
, skeyname
))
1531 mc_config_save_file (macros_config
, NULL
);
1532 mc_config_deinit (macros_config
);
1537 /* --------------------------------------------------------------------------------------------- */
1538 /*** public functions ****************************************************************************/
1539 /* --------------------------------------------------------------------------------------------- */
1542 edit_help_cmd (WEdit
* edit
)
1544 ev_help_t event_data
= { NULL
, "[Internal File Editor]" };
1545 mc_event_raise (MCEVENT_GROUP_CORE
, "help", &event_data
);
1547 edit
->force
|= REDRAW_COMPLETELY
;
1550 /* --------------------------------------------------------------------------------------------- */
1553 edit_refresh_cmd (WEdit
* edit
)
1558 edit_get_syntax_color (edit
, -1, &color
);
1559 tty_touch_screen ();
1566 #endif /* !HAVE_SLANG */
1570 /* --------------------------------------------------------------------------------------------- */
1573 menu_save_mode_cmd (void)
1576 const int DLG_X
= 38;
1577 const int DLG_Y
= 13;
1581 const char *str
[] = {
1584 N_("&Do backups with following extension:")
1587 QuickWidget widgets
[] = {
1589 QUICK_BUTTON (18, DLG_X
, DLG_Y
- 3, DLG_Y
, N_("&Cancel"), B_CANCEL
, NULL
),
1591 QUICK_BUTTON (6, DLG_X
, DLG_Y
- 3, DLG_Y
, N_("&OK"), B_ENTER
, NULL
),
1593 QUICK_CHECKBOX (4, DLG_X
, 8, DLG_Y
, N_("Check &POSIX new line"), &option_check_nl_at_eof
),
1595 QUICK_INPUT (8, DLG_X
, 6, DLG_Y
, option_backup_ext
, 9, 0, "edit-backup-ext", &str_result
),
1597 QUICK_RADIO (4, DLG_X
, 3, DLG_Y
, 3, str
, &option_save_mode
),
1601 QuickDialog dialog
= {
1602 DLG_X
, DLG_Y
, -1, -1, N_("Edit Save Mode"),
1603 "[Edit Save Mode]", widgets
, NULL
, FALSE
1608 size_t w0
, w1
, b_len
, w3
;
1610 #ifdef HAVE_ASSERT_H
1611 assert (option_backup_ext
!= NULL
);
1614 /* OK/Cancel buttons */
1615 w0
= str_term_width1 (_(widgets
[0].u
.button
.text
)) + 3;
1616 w1
= str_term_width1 (_(widgets
[1].u
.button
.text
)) + 5; /* default button */
1617 b_len
= w0
+ w1
+ 3;
1619 maxlen
= max (b_len
, (size_t) str_term_width1 (_(dialog
.title
)) + 2);
1622 for (i
= 0; i
< 3; i
++)
1627 w3
= max (w3
, (size_t) str_term_width1 (str
[i
]));
1630 maxlen
= max (maxlen
, w3
+ 4);
1632 dialog
.xlen
= min ((size_t) COLS
, maxlen
+ 8);
1634 widgets
[3].u
.input
.len
= w3
;
1635 widgets
[1].relative_x
= (dialog
.xlen
- b_len
) / 2;
1636 widgets
[0].relative_x
= widgets
[1].relative_x
+ w0
+ 2;
1638 for (i
= 0; i
< sizeof (widgets
) / sizeof (widgets
[0]); i
++)
1639 widgets
[i
].x_divisions
= dialog
.xlen
;
1641 if (quick_dialog (&dialog
) != B_CANCEL
)
1643 g_free (option_backup_ext
);
1644 option_backup_ext
= str_result
;
1648 /* --------------------------------------------------------------------------------------------- */
1651 edit_set_filename (WEdit
* edit
, const vfs_path_t
* name_vpath
)
1653 vfs_path_free (edit
->filename_vpath
);
1654 edit
->filename_vpath
= vfs_path_clone (name_vpath
);
1656 if (edit
->dir_vpath
== NULL
)
1657 edit
->dir_vpath
= vfs_path_clone (vfs_get_raw_current_dir ());
1660 /* --------------------------------------------------------------------------------------------- */
1661 /* Here we want to warn the users of overwriting an existing file,
1662 but only if they have made a change to the filename */
1663 /* returns 1 on success */
1665 edit_save_as_cmd (WEdit
* edit
)
1667 /* This heads the 'Save As' dialog box */
1668 vfs_path_t
*exp_vpath
;
1670 int different_filename
= 0;
1672 if (!edit_check_newline (edit
))
1675 exp_vpath
= edit_get_save_file_as (edit
);
1676 edit_push_undo_action (edit
, KEY_PRESS
+ edit
->start_display
);
1678 if (exp_vpath
!= NULL
)
1680 if (vfs_path_len (exp_vpath
) == 0)
1686 if (vfs_path_cmp (edit
->filename_vpath
, exp_vpath
) != 0)
1691 if (mc_stat (exp_vpath
, &sb
) == 0 && !S_ISREG (sb
.st_mode
))
1693 edit_error_dialog (_("Save as"),
1695 ("Cannot save: destination is not a regular file")));
1699 different_filename
= 1;
1700 file
= mc_open (exp_vpath
, O_RDONLY
| O_BINARY
);
1704 /* the file exists */
1706 /* Overwrite the current file or cancel the operation */
1707 if (edit_query_dialog2
1709 _("A file already exists with this name"), _("&Overwrite"), _("&Cancel")))
1714 edit
->stat1
.st_mode
|= S_IWUSR
;
1716 save_lock
= lock_file (exp_vpath
);
1720 /* filenames equal, check if already locked */
1721 if (!edit
->locked
&& !edit
->delete_file
)
1722 save_lock
= lock_file (exp_vpath
);
1725 if (different_filename
)
1728 * Allow user to write into saved (under another name) file
1729 * even if original file had r/o user permissions.
1731 edit
->stat1
.st_mode
|= S_IWRITE
;
1734 rv
= edit_save_file (edit
, exp_vpath
);
1738 /* Succesful, so unlock both files */
1739 if (different_filename
)
1742 unlock_file (exp_vpath
);
1744 edit
->locked
= unlock_file (edit
->filename_vpath
);
1748 if (edit
->locked
|| save_lock
)
1749 edit
->locked
= unlock_file (edit
->filename_vpath
);
1752 edit_set_filename (edit
, exp_vpath
);
1753 if (edit
->lb
!= LB_ASIS
)
1754 edit_reload (edit
, exp_vpath
);
1756 edit
->delete_file
= 0;
1757 if (different_filename
)
1758 edit_load_syntax (edit
, NULL
, edit
->syntax_type
);
1759 vfs_path_free (exp_vpath
);
1760 edit
->force
|= REDRAW_COMPLETELY
;
1763 edit_error_dialog (_("Save as"), get_sys_error (_("Cannot save file")));
1766 /* Failed, so maintain modify (not save) lock */
1768 unlock_file (exp_vpath
);
1775 vfs_path_free (exp_vpath
);
1776 edit
->force
|= REDRAW_COMPLETELY
;
1780 /* {{{ Macro stuff starts here */
1781 /* --------------------------------------------------------------------------------------------- */
1784 edit_delete_macro_cmd (WEdit
* edit
)
1788 hotkey
= editcmd_dialog_raw_key_query (_("Delete macro"), _("Press macro hotkey:"), 1);
1790 if (hotkey
!= 0 && !edit_delete_macro (edit
, hotkey
))
1791 message (D_ERROR
, _("Delete macro"), _("Macro not deleted"));
1794 /* --------------------------------------------------------------------------------------------- */
1796 /** returns FALSE on error */
1798 edit_execute_macro (WEdit
* edit
, int hotkey
)
1800 gboolean res
= FALSE
;
1804 const macros_t
*macros
;
1807 if (edit_get_macro (edit
, hotkey
, ¯os
, &indx
) &&
1808 macros
->macro
!= NULL
&& macros
->macro
->len
!= 0)
1812 edit
->force
|= REDRAW_PAGE
;
1814 for (i
= 0; i
< macros
->macro
->len
; i
++)
1816 const macro_action_t
*m_act
;
1818 m_act
= &g_array_index (macros
->macro
, struct macro_action_t
, i
);
1819 edit_execute_cmd (edit
, m_act
->action
, m_act
->ch
);
1828 /* --------------------------------------------------------------------------------------------- */
1830 /** returns FALSE on error */
1832 edit_store_macro_cmd (WEdit
* edit
)
1836 GString
*marcros_string
;
1837 mc_config_t
*macros_config
= NULL
;
1838 const char *section_name
= "editor";
1839 gchar
*macros_fname
;
1840 GArray
*macros
; /* current macro */
1842 gboolean have_macro
= FALSE
;
1843 char *skeyname
= NULL
;
1845 hotkey
= editcmd_dialog_raw_key_query (_("Save macro"), _("Press the macro's new hotkey:"), 1);
1846 if (hotkey
== ESC_CHAR
)
1849 tmp_act
= keybind_lookup_keymap_command (editor_map
, hotkey
);
1851 /* return FALSE if try assign macro into restricted hotkeys */
1852 if (tmp_act
== CK_MacroStartRecord
1853 || tmp_act
== CK_MacroStopRecord
|| tmp_act
== CK_MacroStartStopRecord
)
1856 edit_delete_macro (edit
, hotkey
);
1858 macros_fname
= mc_config_get_full_path (MC_MACRO_FILE
);
1859 macros_config
= mc_config_init (macros_fname
);
1860 g_free (macros_fname
);
1862 if (macros_config
== NULL
)
1865 edit_push_undo_action (edit
, KEY_PRESS
+ edit
->start_display
);
1867 marcros_string
= g_string_sized_new (250);
1868 macros
= g_array_new (TRUE
, FALSE
, sizeof (macro_action_t
));
1870 skeyname
= lookup_key_by_code (hotkey
);
1872 for (i
= 0; i
< macro_index
; i
++)
1874 macro_action_t m_act
;
1875 const char *action_name
;
1877 action_name
= keybind_lookup_actionname (record_macro_buf
[i
].action
);
1879 if (action_name
== NULL
)
1882 m_act
.action
= record_macro_buf
[i
].action
;
1883 m_act
.ch
= record_macro_buf
[i
].ch
;
1884 g_array_append_val (macros
, m_act
);
1886 g_string_append_printf (marcros_string
, "%s:%i;", action_name
,
1887 (int) record_macro_buf
[i
].ch
);
1892 macro
.hotkey
= hotkey
;
1893 macro
.macro
= macros
;
1894 g_array_append_val (macros_list
, macro
);
1895 mc_config_set_string (macros_config
, section_name
, skeyname
, marcros_string
->str
);
1898 mc_config_del_key (macros_config
, section_name
, skeyname
);
1901 edit_macro_sort_by_hotkey ();
1903 g_string_free (marcros_string
, TRUE
);
1904 mc_config_save_file (macros_config
, NULL
);
1905 mc_config_deinit (macros_config
);
1909 /* --------------------------------------------------------------------------------------------- */
1912 edit_repeat_macro_cmd (WEdit
* edit
)
1919 f
= input_dialog (_("Repeat last commands"), _("Repeat times:"), MC_HISTORY_EDIT_REPEAT
, NULL
);
1920 if (f
== NULL
|| *f
== '\0')
1926 count_repeat
= strtol (f
, &error
, 0);
1936 edit_push_undo_action (edit
, KEY_PRESS
+ edit
->start_display
);
1937 edit
->force
|= REDRAW_PAGE
;
1939 for (j
= 0; j
< count_repeat
; j
++)
1940 for (i
= 0; i
< macro_index
; i
++)
1941 edit_execute_cmd (edit
, record_macro_buf
[i
].action
, record_macro_buf
[i
].ch
);
1942 edit_update_screen (edit
);
1946 /* --------------------------------------------------------------------------------------------- */
1947 /** return FALSE on error */
1950 edit_load_macro_cmd (WEdit
* edit
)
1952 mc_config_t
*macros_config
= NULL
;
1953 gchar
**profile_keys
, **keys
;
1954 gchar
**values
, **curr_values
;
1955 gsize len
, values_len
;
1956 const char *section_name
= "editor";
1957 gchar
*macros_fname
;
1962 macros_fname
= mc_config_get_full_path (MC_MACRO_FILE
);
1963 macros_config
= mc_config_init (macros_fname
);
1964 g_free (macros_fname
);
1966 if (macros_config
== NULL
)
1969 profile_keys
= keys
= mc_config_get_keys (macros_config
, section_name
, &len
);
1970 while (*profile_keys
!= NULL
)
1972 gboolean have_macro
;
1976 macros
= g_array_new (TRUE
, FALSE
, sizeof (macro_action_t
));
1978 curr_values
= values
= mc_config_get_string_list (macros_config
, section_name
,
1979 *profile_keys
, &values_len
);
1980 hotkey
= lookup_key (*profile_keys
, NULL
);
1983 while (*curr_values
!= NULL
&& *curr_values
[0] != '\0')
1985 char **macro_pair
= NULL
;
1987 macro_pair
= g_strsplit (*curr_values
, ":", 2);
1989 if (macro_pair
!= NULL
)
1991 macro_action_t m_act
;
1992 if (macro_pair
[0] == NULL
|| macro_pair
[0][0] == '\0')
1996 m_act
.action
= keybind_lookup_action (macro_pair
[0]);
1997 g_free (macro_pair
[0]);
1998 macro_pair
[0] = NULL
;
2000 if (macro_pair
[1] == NULL
|| macro_pair
[1][0] == '\0')
2004 m_act
.ch
= strtol (macro_pair
[1], NULL
, 0);
2005 g_free (macro_pair
[1]);
2006 macro_pair
[1] = NULL
;
2008 if (m_act
.action
!= 0)
2010 /* a shell command */
2011 if ((m_act
.action
/ CK_PipeBlock (0)) == 1)
2013 m_act
.action
= CK_PipeBlock (0) + (m_act
.ch
> 0 ? m_act
.ch
: 0);
2016 g_array_append_val (macros
, m_act
);
2019 g_strfreev (macro_pair
);
2026 macro
.hotkey
= hotkey
;
2027 macro
.macro
= macros
;
2028 g_array_append_val (macros_list
, macro
);
2031 g_strfreev (values
);
2034 mc_config_deinit (macros_config
);
2035 edit_macro_sort_by_hotkey ();
2039 /* }}} Macro stuff end here */
2041 /* --------------------------------------------------------------------------------------------- */
2042 /** returns 1 on success */
2045 edit_save_confirm_cmd (WEdit
* edit
)
2049 if (edit
->filename_vpath
== NULL
)
2050 return edit_save_as_cmd (edit
);
2052 if (!edit_check_newline (edit
))
2055 if (edit_confirm_save
)
2060 filename
= vfs_path_to_str (edit
->filename_vpath
);
2061 f
= g_strdup_printf (_("Confirm save file: \"%s\""), filename
);
2063 ok
= (edit_query_dialog2 (_("Save file"), f
, _("&Save"), _("&Cancel")) == 0);
2068 return edit_save_cmd (edit
);
2071 /* --------------------------------------------------------------------------------------------- */
2072 /** returns 1 on success */
2075 edit_new_cmd (WEdit
* edit
)
2079 if (edit_query_dialog2
2082 ("Current text was modified without a file save.\nContinue discards these changes"),
2083 _("C&ontinue"), _("&Cancel")))
2085 edit
->force
|= REDRAW_COMPLETELY
;
2089 edit
->force
|= REDRAW_COMPLETELY
;
2091 return edit_renew (edit
); /* if this gives an error, something has really screwed up */
2094 /* --------------------------------------------------------------------------------------------- */
2097 edit_load_cmd (WEdit
* edit
, edit_current_file_t what
)
2100 && (edit_query_dialog2
2102 _("Current text was modified without a file save.\n"
2103 "Continue discards these changes"), _("C&ontinue"), _("&Cancel")) == 1))
2105 edit
->force
|= REDRAW_COMPLETELY
;
2111 case EDIT_FILE_COMMON
:
2113 char *filename
, *exp
;
2115 filename
= vfs_path_to_str (edit
->filename_vpath
);
2116 exp
= input_expand_dialog (_("Load"), _("Enter file name:"),
2117 MC_HISTORY_EDIT_LOAD
, filename
);
2124 vfs_path_t
*exp_vpath
;
2126 exp_vpath
= vfs_path_from_str (exp
);
2127 edit_load_file_from_filename (edit
, exp_vpath
);
2128 vfs_path_free (exp_vpath
);
2135 case EDIT_FILE_SYNTAX
:
2136 edit_load_syntax_file (edit
);
2139 case EDIT_FILE_MENU
:
2140 edit_load_menu_file (edit
);
2147 edit
->force
|= REDRAW_COMPLETELY
;
2151 /* --------------------------------------------------------------------------------------------- */
2153 if mark2 is -1 then marking is from mark1 to the cursor.
2154 Otherwise its between the markers. This handles this.
2155 Returns 1 if no text is marked.
2159 eval_marks (WEdit
* edit
, long *start_mark
, long *end_mark
)
2161 if (edit
->mark1
!= edit
->mark2
)
2163 long start_bol
, start_eol
;
2164 long end_bol
, end_eol
;
2169 if (edit
->end_mark_curs
< 0)
2170 end_mark_curs
= edit
->curs1
;
2172 end_mark_curs
= edit
->end_mark_curs
;
2174 if (edit
->mark2
>= 0)
2176 *start_mark
= min (edit
->mark1
, edit
->mark2
);
2177 *end_mark
= max (edit
->mark1
, edit
->mark2
);
2181 *start_mark
= min (edit
->mark1
, end_mark_curs
);
2182 *end_mark
= max (edit
->mark1
, end_mark_curs
);
2183 edit
->column2
= edit
->curs_col
+ edit
->over_col
;
2186 if (edit
->column_highlight
2187 && (((edit
->mark1
> end_mark_curs
) && (edit
->column1
< edit
->column2
))
2188 || ((edit
->mark1
< end_mark_curs
) && (edit
->column1
> edit
->column2
))))
2190 start_bol
= edit_bol (edit
, *start_mark
);
2191 start_eol
= edit_eol (edit
, start_bol
- 1) + 1;
2192 end_bol
= edit_bol (edit
, *end_mark
);
2193 end_eol
= edit_eol (edit
, *end_mark
);
2194 col1
= min (edit
->column1
, edit
->column2
);
2195 col2
= max (edit
->column1
, edit
->column2
);
2198 edit_move_forward3 (edit
, start_bol
, col2
, 0) - edit_move_forward3 (edit
, start_bol
,
2201 edit_move_forward3 (edit
, end_bol
, col2
, 0) - edit_move_forward3 (edit
, end_bol
,
2204 *start_mark
-= diff1
;
2206 *start_mark
= max (*start_mark
, start_eol
);
2207 *end_mark
= min (*end_mark
, end_eol
);
2213 *start_mark
= *end_mark
= 0;
2214 edit
->column2
= edit
->column1
= 0;
2219 /* --------------------------------------------------------------------------------------------- */
2222 edit_insert_over (WEdit
* edit
)
2226 for (i
= 0; i
< edit
->over_col
; i
++)
2228 edit_insert (edit
, ' ');
2233 /* --------------------------------------------------------------------------------------------- */
2236 edit_insert_column_of_text_from_file (WEdit
* edit
, int file
,
2237 long *start_pos
, long *end_pos
, int *col1
, int *col2
)
2241 int blocklen
= -1, width
= 0;
2242 unsigned char *data
;
2244 cursor
= edit
->curs1
;
2245 col
= edit_get_col (edit
);
2246 data
= g_malloc0 (TEMP_BUF_LEN
);
2248 while ((blocklen
= mc_read (file
, (char *) data
, TEMP_BUF_LEN
)) > 0)
2251 for (width
= 0; width
< blocklen
; width
++)
2253 if (data
[width
] == '\n')
2256 for (i
= 0; i
< blocklen
; i
++)
2258 if (data
[i
] == '\n')
2259 { /* fill in and move to next line */
2262 if (edit_get_byte (edit
, edit
->curs1
) != '\n')
2264 l
= width
- (edit_get_col (edit
) - col
);
2267 edit_insert (edit
, ' ');
2271 for (p
= edit
->curs1
;; p
++)
2273 if (p
== edit
->last_byte
)
2275 edit_cursor_move (edit
, edit
->last_byte
- edit
->curs1
);
2276 edit_insert_ahead (edit
, '\n');
2280 if (edit_get_byte (edit
, p
) == '\n')
2286 edit_cursor_move (edit
, edit_move_forward3 (edit
, p
, col
, 0) - edit
->curs1
);
2287 l
= col
- edit_get_col (edit
);
2288 while (l
>= space_width
)
2290 edit_insert (edit
, ' ');
2295 edit_insert (edit
, data
[i
]);
2299 *col2
= col
+ width
;
2300 *start_pos
= cursor
;
2301 *end_pos
= edit
->curs1
;
2302 edit_cursor_move (edit
, cursor
- edit
->curs1
);
2308 /* --------------------------------------------------------------------------------------------- */
2311 edit_block_copy_cmd (WEdit
* edit
)
2313 long start_mark
, end_mark
, current
= edit
->curs1
;
2318 unsigned char *copy_buf
;
2320 edit_update_curs_col (edit
);
2321 if (eval_marks (edit
, &start_mark
, &end_mark
))
2324 copy_buf
= edit_get_block (edit
, start_mark
, end_mark
, &size
);
2326 /* all that gets pushed are deletes hence little space is used on the stack */
2328 edit_push_markers (edit
);
2330 if (edit
->column_highlight
)
2332 col_delta
= abs (edit
->column2
- edit
->column1
);
2333 edit_insert_column_of_text (edit
, copy_buf
, size
, col_delta
, &mark1
, &mark2
, &c1
, &c2
);
2338 edit_insert_ahead (edit
, copy_buf
[size
]);
2342 edit_scroll_screen_over_cursor (edit
);
2344 if (edit
->column_highlight
)
2345 edit_set_markers (edit
, edit
->curs1
, mark2
, c1
, c2
);
2346 else if (start_mark
< current
&& end_mark
> current
)
2347 edit_set_markers (edit
, start_mark
, end_mark
+ end_mark
- start_mark
, 0, 0);
2349 edit
->force
|= REDRAW_PAGE
;
2353 /* --------------------------------------------------------------------------------------------- */
2356 edit_block_move_cmd (WEdit
* edit
)
2359 unsigned char *copy_buf
= NULL
;
2360 long start_mark
, end_mark
;
2363 if (eval_marks (edit
, &start_mark
, &end_mark
))
2366 line
= edit
->curs_line
;
2367 if (edit
->mark2
< 0)
2368 edit_mark_cmd (edit
, 0);
2369 edit_push_markers (edit
);
2371 if (edit
->column_highlight
)
2379 c1
= min (edit
->column1
, edit
->column2
);
2380 c2
= max (edit
->column1
, edit
->column2
);
2381 b_width
= (c2
- c1
);
2383 edit_update_curs_col (edit
);
2386 x2
= x
+ edit
->over_col
;
2388 /* do nothing when cursor inside first line of selected area */
2389 if ((edit_eol (edit
, edit
->curs1
) == edit_eol (edit
, start_mark
)) && (x2
> c1
&& x2
<= c2
))
2392 if (edit
->curs1
> start_mark
&& edit
->curs1
< edit_eol (edit
, end_mark
))
2396 else if (x
> c1
&& x
<= c2
)
2399 /* save current selection into buffer */
2400 copy_buf
= edit_get_block (edit
, start_mark
, end_mark
, &size
);
2402 /* remove current selection */
2403 edit_block_delete_cmd (edit
);
2405 edit
->over_col
= max (0, edit
->over_col
- b_width
);
2406 /* calculate the cursor pos after delete block */
2407 current
= edit_move_forward3 (edit
, edit_bol (edit
, edit
->curs1
), x
, 0);
2408 edit_cursor_move (edit
, current
- edit
->curs1
);
2409 edit_scroll_screen_over_cursor (edit
);
2411 /* add TWS if need before block insertion */
2412 if (option_cursor_beyond_eol
&& edit
->over_col
> 0)
2413 edit_insert_over (edit
);
2415 edit_insert_column_of_text (edit
, copy_buf
, size
, b_width
, &mark1
, &mark2
, &c1
, &c2
);
2416 edit_set_markers (edit
, mark1
, mark2
, c1
, c2
);
2422 current
= edit
->curs1
;
2423 copy_buf
= g_malloc0 (end_mark
- start_mark
);
2424 edit_cursor_move (edit
, start_mark
- edit
->curs1
);
2425 edit_scroll_screen_over_cursor (edit
);
2427 while (count
< end_mark
)
2429 copy_buf
[end_mark
- count
- 1] = edit_delete (edit
, 1);
2432 edit_scroll_screen_over_cursor (edit
);
2433 edit_cursor_move (edit
,
2434 current
- edit
->curs1
-
2435 (((current
- edit
->curs1
) > 0) ? end_mark
- start_mark
: 0));
2436 edit_scroll_screen_over_cursor (edit
);
2437 while (count
-- > start_mark
)
2438 edit_insert_ahead (edit
, copy_buf
[end_mark
- count
- 1]);
2439 edit_set_markers (edit
, edit
->curs1
, edit
->curs1
+ end_mark
- start_mark
, 0, 0);
2442 edit_scroll_screen_over_cursor (edit
);
2444 edit
->force
|= REDRAW_PAGE
;
2447 /* --------------------------------------------------------------------------------------------- */
2448 /** returns 1 if canceelled by user */
2451 edit_block_delete_cmd (WEdit
* edit
)
2453 long start_mark
, end_mark
;
2454 if (eval_marks (edit
, &start_mark
, &end_mark
))
2456 edit_delete_line (edit
);
2459 return edit_block_delete (edit
);
2462 /* --------------------------------------------------------------------------------------------- */
2463 /** call with edit = 0 before shutdown to close memory leaks */
2466 edit_replace_cmd (WEdit
* edit
, int again
)
2468 /* 1 = search string, 2 = replace with */
2469 static char *saved1
= NULL
; /* saved default[123] */
2470 static char *saved2
= NULL
;
2471 char *input1
= NULL
; /* user input from the dialog */
2472 char *input2
= NULL
;
2473 GString
*input2_str
= NULL
;
2476 long times_replaced
= 0;
2477 gboolean once_found
= FALSE
;
2481 g_free (saved1
), saved1
= NULL
;
2482 g_free (saved2
), saved2
= NULL
;
2486 edit
->force
|= REDRAW_COMPLETELY
;
2488 if (again
&& !saved1
&& !saved2
)
2493 input1
= g_strdup (saved1
? saved1
: "");
2494 input2
= g_strdup (saved2
? saved2
: "");
2498 char *tmp_inp1
, *tmp_inp2
;
2500 disp1
= edit_replace_cmd__conv_to_display (saved1
? saved1
: (char *) "");
2501 disp2
= edit_replace_cmd__conv_to_display (saved2
? saved2
: (char *) "");
2503 edit_push_undo_action (edit
, KEY_PRESS
+ edit
->start_display
);
2505 editcmd_dialog_replace_show (edit
, disp1
, disp2
, &input1
, &input2
);
2510 if (input1
== NULL
|| *input1
== '\0')
2512 edit
->force
= REDRAW_COMPLETELY
;
2518 input1
= edit_replace_cmd__conv_to_input (input1
);
2519 input2
= edit_replace_cmd__conv_to_input (input2
);
2523 g_free (saved1
), saved1
= g_strdup (input1
);
2524 g_free (saved2
), saved2
= g_strdup (input2
);
2526 mc_search_free (edit
->search
);
2527 edit
->search
= NULL
;
2530 input2_str
= g_string_new (input2
);
2534 edit
->search
= mc_search_new (input1
, -1);
2535 if (edit
->search
== NULL
)
2537 edit
->search_start
= edit
->curs1
;
2540 edit
->search
->search_type
= edit_search_options
.type
;
2541 edit
->search
->is_all_charsets
= edit_search_options
.all_codepages
;
2542 edit
->search
->is_case_sensitive
= edit_search_options
.case_sens
;
2543 edit
->search
->whole_words
= edit_search_options
.whole_words
;
2544 edit
->search
->search_fn
= edit_search_cmd_callback
;
2545 edit
->search_line_type
= edit_get_search_line_type (edit
->search
);
2546 edit_search_fix_search_start_if_selection (edit
);
2549 if (edit
->found_len
&& edit
->search_start
== edit
->found_start
+ 1
2550 && edit_search_options
.backwards
)
2551 edit
->search_start
--;
2553 if (edit
->found_len
&& edit
->search_start
== edit
->found_start
- 1
2554 && !edit_search_options
.backwards
)
2555 edit
->search_start
++;
2561 if (!editcmd_find (edit
, &len
))
2563 if (!(edit
->search
->error
== MC_SEARCH_E_OK
||
2564 (once_found
&& edit
->search
->error
== MC_SEARCH_E_NOTFOUND
)))
2566 edit_error_dialog (_("Search"), edit
->search
->error_str
);
2572 edit
->search_start
= edit
->search
->normal_offset
;
2573 /*returns negative on not found or error in pattern */
2575 if ((edit
->search_start
>= 0) && (edit
->search_start
< edit
->last_byte
))
2580 edit
->found_start
= edit
->search_start
;
2581 i
= edit
->found_len
= len
;
2583 edit_cursor_move (edit
, edit
->search_start
- edit
->curs1
);
2584 edit_scroll_screen_over_cursor (edit
);
2586 if (edit
->replace_mode
== 0)
2591 l
= edit
->curs_row
- edit
->widget
.lines
/ 3;
2593 edit_scroll_downward (edit
, l
);
2595 edit_scroll_upward (edit
, -l
);
2597 edit_scroll_screen_over_cursor (edit
);
2598 edit
->force
|= REDRAW_PAGE
;
2599 edit_render_keypress (edit
);
2601 /*so that undo stops at each query */
2602 edit_push_key_press (edit
);
2603 /* and prompt 2/3 down */
2604 disp1
= edit_replace_cmd__conv_to_display (saved1
);
2605 disp2
= edit_replace_cmd__conv_to_display (saved2
);
2606 prompt
= editcmd_dialog_replace_prompt_show (edit
, disp1
, disp2
, -1, -1);
2610 if (prompt
== B_REPLACE_ALL
)
2611 edit
->replace_mode
= 1;
2612 else if (prompt
== B_SKIP_REPLACE
)
2614 if (edit_search_options
.backwards
)
2615 edit
->search_start
--;
2617 edit
->search_start
++;
2618 continue; /* loop */
2620 else if (prompt
== B_CANCEL
)
2622 edit
->replace_mode
= -1;
2627 repl_str
= mc_search_prepare_replace_str (edit
->search
, input2_str
);
2629 if (edit
->search
->error
!= MC_SEARCH_E_OK
)
2631 edit_error_dialog (_("Replace"), edit
->search
->error_str
);
2632 g_string_free (repl_str
, TRUE
);
2636 /* delete then insert new */
2637 for (i
= 0; i
< len
; i
++)
2638 edit_delete (edit
, 1);
2640 for (i
= 0; i
< repl_str
->len
; i
++)
2641 edit_insert (edit
, repl_str
->str
[i
]);
2643 edit
->found_len
= repl_str
->len
;
2644 g_string_free (repl_str
, TRUE
);
2647 /* so that we don't find the same string again */
2648 if (edit_search_options
.backwards
)
2650 edit
->search_start
--;
2654 edit
->search_start
+= edit
->found_len
+ (len
== 0 ? 1 : 0);
2656 if (edit
->search_start
>= edit
->last_byte
)
2660 edit_scroll_screen_over_cursor (edit
);
2664 /* try and find from right here for next search */
2665 edit
->search_start
= edit
->curs1
;
2666 edit_update_curs_col (edit
);
2668 edit
->force
|= REDRAW_PAGE
;
2669 edit_render_keypress (edit
);
2671 if (times_replaced
== 0)
2672 query_dialog (_("Replace"), _("Search string not found"), D_NORMAL
, 1, _("&OK"));
2676 while (edit
->replace_mode
>= 0);
2678 edit_scroll_screen_over_cursor (edit
);
2679 edit
->force
|= REDRAW_COMPLETELY
;
2680 edit_render_keypress (edit
);
2682 if ((edit
->replace_mode
== 1) && (times_replaced
!= 0))
2683 message (D_NORMAL
, _("Replace"), _("%ld replacements made"), times_replaced
);
2688 if (input2_str
!= NULL
)
2689 g_string_free (input2_str
, TRUE
);
2692 /* --------------------------------------------------------------------------------------------- */
2695 edit_search_cmd_callback (const void *user_data
, gsize char_offset
, int *current_char
)
2697 *current_char
= edit_get_byte ((WEdit
*) user_data
, (long) char_offset
);
2698 return MC_SEARCH_CB_OK
;
2701 /* --------------------------------------------------------------------------------------------- */
2704 edit_search_cmd (WEdit
* edit
, gboolean again
)
2712 else if (edit
->last_search_string
!= NULL
)
2713 edit_do_search (edit
);
2716 /* find last search string in history */
2719 history
= history_get (MC_HISTORY_SHARED_SEARCH
);
2720 if (history
!= NULL
&& history
->data
!= NULL
)
2722 edit
->last_search_string
= (char *) history
->data
;
2723 history
->data
= NULL
;
2724 history
= g_list_first (history
);
2725 g_list_foreach (history
, (GFunc
) g_free
, NULL
);
2726 g_list_free (history
);
2728 edit
->search
= mc_search_new (edit
->last_search_string
, -1);
2729 if (edit
->search
== NULL
)
2731 /* if not... then ask for an expression */
2732 g_free (edit
->last_search_string
);
2733 edit
->last_search_string
= NULL
;
2738 edit
->search
->search_type
= edit_search_options
.type
;
2739 edit
->search
->is_all_charsets
= edit_search_options
.all_codepages
;
2740 edit
->search
->is_case_sensitive
= edit_search_options
.case_sens
;
2741 edit
->search
->whole_words
= edit_search_options
.whole_words
;
2742 edit
->search
->search_fn
= edit_search_cmd_callback
;
2743 edit
->search_line_type
= edit_get_search_line_type (edit
->search
);
2744 edit_do_search (edit
);
2749 /* if not... then ask for an expression */
2750 g_free (edit
->last_search_string
);
2751 edit
->last_search_string
= NULL
;
2758 /* --------------------------------------------------------------------------------------------- */
2760 * Check if it's OK to close the editor. If there are unsaved changes,
2761 * ask user. Return 1 if it's OK to exit, 0 to continue editing.
2765 edit_ok_to_exit (WEdit
* edit
)
2769 if (!edit
->modified
)
2772 if (!mc_global
.midnight_shutdown
)
2774 if (!edit_check_newline (edit
))
2778 act
= edit_query_dialog3 (_("Quit"), _("File was modified. Save with exit?"),
2779 _("&Yes"), _("&No"), _("&Cancel quit"));
2784 edit_query_dialog2 (_("Quit"),
2785 _("Midnight Commander is being shut down.\nSave modified file?"),
2786 _("&Yes"), _("&No"));
2796 edit_push_markers (edit
);
2797 edit_set_markers (edit
, 0, 0, 0, 0);
2798 if (!edit_save_cmd (edit
) || mc_global
.midnight_shutdown
)
2799 return mc_global
.midnight_shutdown
;
2803 case 2: /* Cancel quit */
2811 /* --------------------------------------------------------------------------------------------- */
2812 /** save block, returns 1 on success */
2815 edit_save_block (WEdit
* edit
, const char *filename
, long start
, long finish
)
2820 vpath
= vfs_path_from_str (filename
);
2821 file
= mc_open (vpath
, O_CREAT
| O_WRONLY
| O_TRUNC
,
2822 S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IROTH
| O_BINARY
);
2823 vfs_path_free (vpath
);
2827 if (edit
->column_highlight
)
2831 r
= mc_write (file
, VERTICAL_MAGIC
, sizeof (VERTICAL_MAGIC
));
2834 unsigned char *block
, *p
;
2836 p
= block
= edit_get_block (edit
, start
, finish
, &len
);
2839 r
= mc_write (file
, p
, len
);
2853 len
= finish
- start
;
2854 buf
= g_malloc0 (TEMP_BUF_LEN
);
2855 while (start
!= finish
)
2857 end
= min (finish
, start
+ TEMP_BUF_LEN
);
2858 for (; i
< end
; i
++)
2859 buf
[i
- start
] = edit_get_byte (edit
, i
);
2860 len
-= mc_write (file
, (char *) buf
, end
- start
);
2871 /* --------------------------------------------------------------------------------------------- */
2874 edit_paste_from_history (WEdit
* edit
)
2877 edit_error_dialog (_("Error"), _("This function is not implemented"));
2880 /* --------------------------------------------------------------------------------------------- */
2883 edit_copy_to_X_buf_cmd (WEdit
* edit
)
2885 long start_mark
, end_mark
;
2886 if (eval_marks (edit
, &start_mark
, &end_mark
))
2888 if (!edit_save_block_to_clip_file (edit
, start_mark
, end_mark
))
2890 edit_error_dialog (_("Copy to clipboard"), get_sys_error (_("Unable to save to file")));
2893 /* try use external clipboard utility */
2894 mc_event_raise (MCEVENT_GROUP_CORE
, "clipboard_file_to_ext_clip", NULL
);
2899 /* --------------------------------------------------------------------------------------------- */
2902 edit_cut_to_X_buf_cmd (WEdit
* edit
)
2904 long start_mark
, end_mark
;
2905 if (eval_marks (edit
, &start_mark
, &end_mark
))
2907 if (!edit_save_block_to_clip_file (edit
, start_mark
, end_mark
))
2909 edit_error_dialog (_("Cut to clipboard"), _("Unable to save to file"));
2912 /* try use external clipboard utility */
2913 mc_event_raise (MCEVENT_GROUP_CORE
, "clipboard_file_to_ext_clip", NULL
);
2915 edit_block_delete_cmd (edit
);
2916 edit_mark_cmd (edit
, 1);
2920 /* --------------------------------------------------------------------------------------------- */
2923 edit_paste_from_X_buf_cmd (WEdit
* edit
)
2927 /* try use external clipboard utility */
2928 mc_event_raise (MCEVENT_GROUP_CORE
, "clipboard_file_from_ext_clip", NULL
);
2929 tmp
= mc_config_get_full_vpath (EDIT_CLIP_FILE
);
2930 edit_insert_file (edit
, tmp
);
2931 vfs_path_free (tmp
);
2935 /* --------------------------------------------------------------------------------------------- */
2937 * Ask user for the line and go to that line.
2938 * Negative numbers mean line from the end (i.e. -1 is the last line).
2942 edit_goto_cmd (WEdit
* edit
)
2945 static long line
= 0; /* line as typed, saved as default */
2950 g_snprintf (s
, sizeof (s
), "%ld", line
);
2951 f
= input_dialog (_("Goto line"), _("Enter line:"), MC_HISTORY_EDIT_GOTO_LINE
, line
? s
: "");
2961 l
= strtol (f
, &error
, 0);
2970 l
= edit
->total_lines
+ l
+ 2;
2971 edit_move_display (edit
, l
- edit
->widget
.lines
/ 2 - 1);
2972 edit_move_to_line (edit
, l
- 1);
2973 edit
->force
|= REDRAW_COMPLETELY
;
2978 /* --------------------------------------------------------------------------------------------- */
2979 /** Return 1 on success */
2982 edit_save_block_cmd (WEdit
* edit
)
2984 long start_mark
, end_mark
;
2987 if (eval_marks (edit
, &start_mark
, &end_mark
))
2990 tmp
= mc_config_get_full_path (EDIT_CLIP_FILE
);
2992 input_expand_dialog (_("Save block"), _("Enter file name:"),
2993 MC_HISTORY_EDIT_SAVE_BLOCK
, tmp
);
2995 edit_push_undo_action (edit
, KEY_PRESS
+ edit
->start_display
);
3005 if (edit_save_block (edit
, exp
, start_mark
, end_mark
))
3008 edit
->force
|= REDRAW_COMPLETELY
;
3014 edit_error_dialog (_("Save block"), get_sys_error (_("Cannot save file")));
3018 edit
->force
|= REDRAW_COMPLETELY
;
3023 /* --------------------------------------------------------------------------------------------- */
3024 /** returns TRUE on success */
3027 edit_insert_file_cmd (WEdit
* edit
)
3031 gboolean ret
= FALSE
;
3033 tmp
= mc_config_get_full_path (EDIT_CLIP_FILE
);
3034 exp
= input_expand_dialog (_("Insert file"), _("Enter file name:"),
3035 MC_HISTORY_EDIT_INSERT_FILE
, tmp
);
3038 edit_push_undo_action (edit
, KEY_PRESS
+ edit
->start_display
);
3040 if (exp
!= NULL
&& *exp
!= '\0')
3042 vfs_path_t
*exp_vpath
;
3044 exp_vpath
= vfs_path_from_str (exp
);
3045 ret
= (edit_insert_file (edit
, exp_vpath
) >= 0);
3046 vfs_path_free (exp_vpath
);
3049 edit_error_dialog (_("Insert file"), get_sys_error (_("Cannot insert file")));
3054 edit
->force
|= REDRAW_COMPLETELY
;
3058 /* --------------------------------------------------------------------------------------------- */
3059 /** sorts a block, returns -1 on system fail, 1 on cancel and 0 on success */
3062 edit_sort_cmd (WEdit
* edit
)
3064 static char *old
= 0;
3065 char *exp
, *tmp
, *tmp_edit_block_name
, *tmp_edit_temp_name
;
3066 long start_mark
, end_mark
;
3069 if (eval_marks (edit
, &start_mark
, &end_mark
))
3071 edit_error_dialog (_("Sort block"), _("You must first highlight a block of text"));
3075 tmp
= mc_config_get_full_path (EDIT_BLOCK_FILE
);
3076 edit_save_block (edit
, tmp
, start_mark
, end_mark
);
3079 exp
= input_dialog (_("Run sort"),
3080 _("Enter sort options (see manpage) separated by whitespace:"),
3081 MC_HISTORY_EDIT_SORT
, (old
!= NULL
) ? old
: "");
3087 tmp_edit_block_name
= mc_config_get_full_path (EDIT_BLOCK_FILE
);
3088 tmp_edit_temp_name
= mc_config_get_full_path (EDIT_TEMP_FILE
);
3090 g_strconcat (" sort ", exp
, " ", tmp_edit_block_name
,
3091 " > ", tmp_edit_temp_name
, (char *) NULL
);
3092 g_free (tmp_edit_temp_name
);
3093 g_free (tmp_edit_block_name
);
3099 if (e
== -1 || e
== 127)
3101 edit_error_dialog (_("Sort"), get_sys_error (_("Cannot execute sort command")));
3106 sprintf (q
, "%d ", e
);
3107 tmp
= g_strdup_printf (_("Sort returned non-zero: %s"), q
);
3108 edit_error_dialog (_("Sort"), tmp
);
3114 edit
->force
|= REDRAW_COMPLETELY
;
3116 if (edit_block_delete_cmd (edit
))
3120 vfs_path_t
*tmp_vpath
;
3122 tmp_vpath
= mc_config_get_full_vpath (EDIT_TEMP_FILE
);
3123 edit_insert_file (edit
, tmp_vpath
);
3124 vfs_path_free (tmp_vpath
);
3129 /* --------------------------------------------------------------------------------------------- */
3131 * Ask user for a command, execute it and paste its output back to the
3136 edit_ext_cmd (WEdit
* edit
)
3138 char *exp
, *tmp
, *tmp_edit_temp_file
;
3142 input_dialog (_("Paste output of external command"),
3143 _("Enter shell command(s):"), MC_HISTORY_EDIT_PASTE_EXTCMD
, NULL
);
3148 tmp_edit_temp_file
= mc_config_get_full_path (EDIT_TEMP_FILE
);
3149 tmp
= g_strconcat (exp
, " > ", tmp_edit_temp_file
, (char *) NULL
);
3150 g_free (tmp_edit_temp_file
);
3157 edit_error_dialog (_("External command"), get_sys_error (_("Cannot execute command")));
3161 edit
->force
|= REDRAW_COMPLETELY
;
3164 vfs_path_t
*tmp_vpath
;
3166 tmp_vpath
= mc_config_get_full_vpath (EDIT_TEMP_FILE
);
3167 edit_insert_file (edit
, tmp_vpath
);
3168 vfs_path_free (tmp_vpath
);
3173 /* --------------------------------------------------------------------------------------------- */
3174 /** if block is 1, a block must be highlighted and the shell command
3175 processes it. If block is 0 the shell command is a straight system
3176 command, that just produces some output which is to be inserted */
3179 edit_block_process_cmd (WEdit
* edit
, int macro_number
)
3182 char *macros_fname
= NULL
;
3184 fname
= g_strdup_printf ("%s.%i.sh", MC_EXTMACRO_FILE
, macro_number
);
3185 macros_fname
= g_build_filename (mc_config_get_data_path (), fname
, (char *) NULL
);
3186 user_menu (edit
, macros_fname
, 0);
3188 g_free (macros_fname
);
3189 edit
->force
|= REDRAW_COMPLETELY
;
3192 /* --------------------------------------------------------------------------------------------- */
3195 edit_mail_dialog (WEdit
* edit
)
3198 char *tmail_subject
;
3201 static char *mail_cc_last
= 0;
3202 static char *mail_subject_last
= 0;
3203 static char *mail_to_last
= 0;
3205 QuickWidget quick_widgets
[] = {
3206 /* 0 */ QUICK_BUTTON (6, 10, 9, MAIL_DLG_HEIGHT
, N_("&Cancel"), B_CANCEL
, NULL
),
3207 /* 1 */ QUICK_BUTTON (2, 10, 9, MAIL_DLG_HEIGHT
, N_("&OK"), B_ENTER
, NULL
),
3208 /* 2 */ QUICK_INPUT (3, 50, 8, MAIL_DLG_HEIGHT
, "", 44, 0, "mail-dlg-input", &tmail_cc
),
3209 /* 3 */ QUICK_LABEL (3, 50, 7, MAIL_DLG_HEIGHT
, N_("Copies to")),
3210 /* 4 */ QUICK_INPUT (3, 50, 6, MAIL_DLG_HEIGHT
, "", 44, 0, "mail-dlg-input-2",
3212 /* 5 */ QUICK_LABEL (3, 50, 5, MAIL_DLG_HEIGHT
, N_("Subject")),
3213 /* 6 */ QUICK_INPUT (3, 50, 4, MAIL_DLG_HEIGHT
, "", 44, 0, "mail-dlg-input-3", &tmail_to
),
3214 /* 7 */ QUICK_LABEL (3, 50, 3, MAIL_DLG_HEIGHT
, N_("To")),
3215 /* 8 */ QUICK_LABEL (3, 50, 2, MAIL_DLG_HEIGHT
, N_("mail -s <subject> -c <cc> <to>")),
3219 QuickDialog Quick_input
= {
3220 50, MAIL_DLG_HEIGHT
, -1, -1, N_("Mail"),
3221 "[Input Line Keys]", quick_widgets
, NULL
, FALSE
3224 quick_widgets
[2].u
.input
.text
= mail_cc_last
? mail_cc_last
: "";
3225 quick_widgets
[4].u
.input
.text
= mail_subject_last
? mail_subject_last
: "";
3226 quick_widgets
[6].u
.input
.text
= mail_to_last
? mail_to_last
: "";
3228 if (quick_dialog (&Quick_input
) != B_CANCEL
)
3230 g_free (mail_cc_last
);
3231 g_free (mail_subject_last
);
3232 g_free (mail_to_last
);
3233 mail_cc_last
= tmail_cc
;
3234 mail_subject_last
= tmail_subject
;
3235 mail_to_last
= tmail_to
;
3236 pipe_mail (edit
, mail_to_last
, mail_subject_last
, mail_cc_last
);
3241 /*******************/
3242 /* Word Completion */
3243 /*******************/
3245 /* --------------------------------------------------------------------------------------------- */
3247 * Complete current word using regular expression search
3248 * backwards beginning at the current cursor position.
3252 edit_complete_word_cmd (WEdit
* edit
)
3254 gsize i
, max_len
, word_len
= 0, num_compl
= 0;
3255 long word_start
= 0;
3256 unsigned char *bufpos
;
3258 struct selection
compl[MAX_WORD_COMPLETIONS
]; /* completions */
3260 /* search start of word to be completed */
3261 if (!edit_find_word_start (edit
, &word_start
, &word_len
))
3264 /* prepare match expression */
3265 bufpos
= &edit
->buffers1
[word_start
>> S_EDIT_BUF_SIZE
][word_start
& M_EDIT_BUF_SIZE
];
3267 /* match_expr = g_strdup_printf ("\\b%.*s[a-zA-Z_0-9]+", word_len, bufpos); */
3270 ("(^|\\s+|\\b)%.*s[^\\s\\.=\\+\\[\\]\\(\\)\\,\\;\\:\\\"\\'\\-\\?\\/\\|\\\\\\{\\}\\*\\&\\^\\%%\\$#@\\!]+",
3271 (int) word_len
, bufpos
);
3273 /* collect the possible completions */
3274 /* start search from begin to end of file */
3276 edit_collect_completions (edit
, word_start
, word_len
, match_expr
,
3277 (struct selection
*) &compl, &num_compl
);
3281 /* insert completed word if there is only one match */
3284 for (i
= word_len
; i
< compl[0].len
; i
++)
3285 edit_insert (edit
, *(compl[0].text
+ i
));
3287 /* more than one possible completion => ask the user */
3290 /* !!! usually only a beep is expected and when <ALT-TAB> is !!! */
3291 /* !!! pressed again the selection dialog pops up, but that !!! */
3292 /* !!! seems to require a further internal state !!! */
3295 /* let the user select the preferred completion */
3296 editcmd_dialog_completion_show (edit
, max_len
, word_len
,
3297 (struct selection
*) &compl, num_compl
);
3301 g_free (match_expr
);
3302 /* release memory before return */
3303 for (i
= 0; i
< num_compl
; i
++)
3304 g_free (compl[i
].text
);
3307 /* --------------------------------------------------------------------------------------------- */
3310 edit_select_codepage_cmd (WEdit
* edit
)
3313 if (do_select_codepage ())
3314 edit_set_codeset (edit
);
3316 edit
->force
= REDRAW_COMPLETELY
;
3317 edit_refresh_cmd (edit
);
3323 /* --------------------------------------------------------------------------------------------- */
3326 edit_insert_literal_cmd (WEdit
* edit
)
3328 int char_for_insertion
= editcmd_dialog_raw_key_query (_("Insert literal"),
3329 _("Press any key:"), 0);
3330 edit_execute_key_command (edit
, -1, ascii_alpha_to_cntrl (char_for_insertion
));
3333 /* --------------------------------------------------------------------------------------------- */
3336 edit_begin_end_macro_cmd (WEdit
* edit
)
3338 /* edit is a pointer to the widget */
3341 unsigned long command
= macro_index
< 0 ? CK_MacroStartRecord
: CK_MacroStopRecord
;
3342 edit_execute_key_command (edit
, command
, -1);
3346 /* --------------------------------------------------------------------------------------------- */
3349 edit_begin_end_repeat_cmd (WEdit
* edit
)
3351 /* edit is a pointer to the widget */
3354 unsigned long command
= macro_index
< 0 ? CK_RepeatStartRecord
: CK_RepeatStopRecord
;
3355 edit_execute_key_command (edit
, command
, -1);
3359 /* --------------------------------------------------------------------------------------------- */
3362 edit_load_forward_cmd (WEdit
* edit
)
3366 if (edit_query_dialog2
3368 _("Current text was modified without a file save\n"
3369 "Continue discards these changes"), _("C&ontinue"), _("&Cancel")))
3371 edit
->force
|= REDRAW_COMPLETELY
;
3375 if (edit_stack_iterator
+ 1 < MAX_HISTORY_MOVETO
)
3377 if (edit_history_moveto
[edit_stack_iterator
+ 1].line
< 1)
3381 edit_stack_iterator
++;
3382 if (edit_history_moveto
[edit_stack_iterator
].filename_vpath
)
3384 edit_reload_line (edit
, edit_history_moveto
[edit_stack_iterator
].filename_vpath
,
3385 edit_history_moveto
[edit_stack_iterator
].line
);
3399 /* --------------------------------------------------------------------------------------------- */
3402 edit_load_back_cmd (WEdit
* edit
)
3406 if (edit_query_dialog2
3408 _("Current text was modified without a file save\n"
3409 "Continue discards these changes"), _("C&ontinue"), _("&Cancel")))
3411 edit
->force
|= REDRAW_COMPLETELY
;
3415 if (edit_stack_iterator
> 0)
3417 edit_stack_iterator
--;
3418 if (edit_history_moveto
[edit_stack_iterator
].filename_vpath
)
3420 edit_reload_line (edit
, edit_history_moveto
[edit_stack_iterator
].filename_vpath
,
3421 edit_history_moveto
[edit_stack_iterator
].line
);
3435 /* --------------------------------------------------------------------------------------------- */
3438 edit_get_match_keyword_cmd (WEdit
* edit
)
3440 gsize word_len
= 0, max_len
= 0;
3443 long word_start
= 0;
3444 unsigned char *bufpos
;
3448 char *tagfile
= NULL
;
3450 etags_hash_t def_hash
[MAX_DEFINITIONS
];
3452 for (i
= 0; i
< MAX_DEFINITIONS
; i
++)
3454 def_hash
[i
].filename
= NULL
;
3457 /* search start of word to be completed */
3458 if (!edit_find_word_start (edit
, &word_start
, &word_len
))
3461 /* prepare match expression */
3462 bufpos
= &edit
->buffers1
[word_start
>> S_EDIT_BUF_SIZE
][word_start
& M_EDIT_BUF_SIZE
];
3463 match_expr
= g_strdup_printf ("%.*s", (int) word_len
, bufpos
);
3465 ptr
= g_get_current_dir ();
3466 path
= g_strconcat (ptr
, G_DIR_SEPARATOR_S
, (char *) NULL
);
3469 /* Recursive search file 'TAGS' in parent dirs */
3472 ptr
= g_path_get_dirname (path
);
3476 tagfile
= mc_build_filename (path
, TAGS_NAME
, (char *) NULL
);
3477 if (exist_file (tagfile
))
3480 while (strcmp (path
, G_DIR_SEPARATOR_S
) != 0);
3485 etags_set_definition_hash (tagfile
, path
, match_expr
, (etags_hash_t
*) & def_hash
);
3490 max_len
= MAX_WIDTH_DEF_DIALOG
;
3494 editcmd_dialog_select_definition_show (edit
, match_expr
, max_len
, word_len
,
3495 (etags_hash_t
*) & def_hash
, num_def
);
3497 g_free (match_expr
);
3500 /* --------------------------------------------------------------------------------------------- */