2 Editor high level editing commands
4 Copyright (C) 1996, 1997, 1998, 2001, 2002, 2003, 2004, 2005, 2006,
6 The Free Software Foundation, Inc.
11 This file is part of the Midnight Commander.
13 The Midnight Commander is free software: you can redistribute it
14 and/or modify it under the terms of the GNU General Public License as
15 published by the Free Software Foundation, either version 3 of the License,
16 or (at your option) any later version.
18 The Midnight Commander is distributed in the hope that it will be useful,
19 but WITHOUT ANY WARRANTY; without even the implied warranty of
20 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 GNU General Public License for more details.
23 You should have received a copy of the GNU General Public License
24 along with this program. If not, see <http://www.gnu.org/licenses/>.
28 * \brief Source: editor high level editing commands
33 /* #define PIPE_BLOCKS_SO_READ_BYTE_BY_BYTE */
44 #include <sys/types.h>
52 #include "lib/global.h"
53 #include "lib/tty/tty.h"
54 #include "lib/tty/key.h" /* XCTRL */
55 #include "lib/mcconfig.h"
57 #include "lib/strutil.h" /* utf string functions */
59 #include "lib/util.h" /* tilde_expand() */
60 #include "lib/vfs/vfs.h"
61 #include "lib/widget.h"
62 #include "lib/charsets.h"
63 #include "lib/event.h" /* mc_event_raise() */
65 #include "src/history.h"
66 #include "src/setup.h" /* option_tab_spacing */
67 #include "src/main.h" /* mactos_t */
68 #include "src/selcodepage.h"
69 #include "src/keybind-defaults.h"
70 #include "src/util.h" /* check_for_default() */
71 #include "src/filemanager/layout.h" /* mc_refresh() */
73 #include "edit-impl.h"
74 #include "edit-widget.h"
75 #include "editcmd_dialogs.h"
78 /*** global variables ****************************************************************************/
80 /* search and replace: */
81 int search_create_bookmark
= FALSE
;
83 /* queries on a save */
84 int edit_confirm_save
= 1;
86 /*** file scope macro definitions ****************************************************************/
90 #define TEMP_BUF_LEN 1024
94 /* thanks to Liviu Daia <daia@stoilow.imar.ro> for getting this
95 (and the above) routines to work properly - paul */
97 #define is_digit(x) ((x) >= '0' && (x) <= '9')
99 #define MAIL_DLG_HEIGHT 12
101 #define MAX_WORD_COMPLETIONS 100 /* in listbox */
103 /*** file scope type declarations ****************************************************************/
105 /*** file scope variables ************************************************************************/
107 /*** file scope functions ************************************************************************/
108 /* --------------------------------------------------------------------------------------------- */
110 /* If 0 (quick save) then a) create/truncate <filename> file,
111 b) save to <filename>;
112 if 1 (safe save) then a) save to <tempnam>,
113 b) rename <tempnam> to <filename>;
114 if 2 (do backups) then a) save to <tempnam>,
115 b) rename <filename> to <filename.backup_ext>,
116 c) rename <tempnam> to <filename>. */
118 /* returns 0 on error, -1 on abort */
121 edit_save_file (WEdit
* edit
, const vfs_path_t
* filename_vpath
)
126 int this_save_mode
, fd
= -1;
127 vfs_path_t
*real_filename_vpath
;
128 vfs_path_t
*savename_vpath
= NULL
;
129 const char *start_filename
;
130 const vfs_path_element_t
*vpath_element
;
132 vpath_element
= vfs_path_get_by_index (filename_vpath
, 0);
133 if (vpath_element
== NULL
)
136 start_filename
= vpath_element
->path
;
137 if (*start_filename
== '\0')
140 if (*start_filename
!= PATH_SEP
&& edit
->dir_vpath
!= NULL
)
142 real_filename_vpath
= vfs_path_append_vpath_new (edit
->dir_vpath
, filename_vpath
, NULL
);
146 real_filename_vpath
= vfs_path_clone (filename_vpath
);
149 this_save_mode
= option_save_mode
;
150 if (this_save_mode
!= EDIT_QUICK_SAVE
)
152 if (!vfs_file_is_local (real_filename_vpath
)
153 || (fd
= mc_open (real_filename_vpath
, O_RDONLY
| O_BINARY
)) == -1)
156 * The file does not exists yet, so no safe save or
157 * backup are necessary.
159 this_save_mode
= EDIT_QUICK_SAVE
;
165 if (this_save_mode
== EDIT_QUICK_SAVE
&& !edit
->skip_detach_prompt
)
170 rv
= mc_stat (real_filename_vpath
, &sb
);
171 if (rv
== 0 && sb
.st_nlink
> 1)
173 rv
= edit_query_dialog3 (_("Warning"),
174 _("File has hard-links. Detach before saving?"),
175 _("&Yes"), _("&No"), _("&Cancel"));
179 this_save_mode
= EDIT_SAFE_SAVE
;
182 edit
->skip_detach_prompt
= 1;
185 vfs_path_free (real_filename_vpath
);
190 /* Prevent overwriting changes from other editor sessions. */
191 if (rv
== 0 && edit
->stat1
.st_mtime
!= 0 && edit
->stat1
.st_mtime
!= sb
.st_mtime
)
193 /* The default action is "Cancel". */
196 rv
= edit_query_dialog2 (_("Warning"),
197 _("The file has been modified in the meantime. Save anyway?"),
198 _("&Yes"), _("&Cancel"));
201 vfs_path_free (real_filename_vpath
);
207 if (this_save_mode
!= EDIT_QUICK_SAVE
)
209 char *savedir
, *saveprefix
;
211 savedir
= vfs_path_tokens_get (real_filename_vpath
, 0, -1);
213 savedir
= g_strdup (".");
215 saveprefix
= mc_build_filename (savedir
, "cooledit", NULL
);
217 fd
= mc_mkstemps (&savename_vpath
, saveprefix
, NULL
);
219 if (savename_vpath
== NULL
)
221 vfs_path_free (real_filename_vpath
);
225 * Close for now because mc_mkstemps use pure open system call
226 * to create temporary file and it needs to be reopened by
227 * VFS-aware mc_open().
232 savename_vpath
= vfs_path_clone (real_filename_vpath
);
234 (void) mc_chown (savename_vpath
, edit
->stat1
.st_uid
, edit
->stat1
.st_gid
);
235 (void) mc_chmod (savename_vpath
, edit
->stat1
.st_mode
);
237 fd
= mc_open (savename_vpath
, O_CREAT
| O_WRONLY
| O_TRUNC
| O_BINARY
, edit
->stat1
.st_mode
);
242 p
= edit_get_write_filter (savename_vpath
, real_filename_vpath
);
248 file
= (FILE *) popen (p
, "w");
252 filelen
= edit_write_stream (edit
, file
);
256 if (pclose (file
) != 0)
258 tmp
= g_strdup_printf (_("Error writing to pipe: %s"), p
);
259 edit_error_dialog (_("Error"), tmp
);
268 tmp
= g_strdup_printf (_("Cannot open pipe for writing: %s"), p
);
269 edit_error_dialog (_("Error"), get_sys_error (tmp
));
276 else if (edit
->lb
== LB_ASIS
)
277 { /* do not change line breaks */
280 filelen
= edit
->last_byte
;
281 while (buf
<= (edit
->curs1
>> S_EDIT_BUF_SIZE
) - 1)
283 if (mc_write (fd
, (char *) edit
->buffers1
[buf
], EDIT_BUF_SIZE
) != EDIT_BUF_SIZE
)
291 (fd
, (char *) edit
->buffers1
[buf
],
292 edit
->curs1
& M_EDIT_BUF_SIZE
) != (edit
->curs1
& M_EDIT_BUF_SIZE
))
296 else if (edit
->curs2
)
299 buf
= (edit
->curs2
>> S_EDIT_BUF_SIZE
);
302 (char *) edit
->buffers2
[buf
] + EDIT_BUF_SIZE
-
303 (edit
->curs2
& M_EDIT_BUF_SIZE
) - 1,
304 1 + (edit
->curs2
& M_EDIT_BUF_SIZE
)) != 1 + (edit
->curs2
& M_EDIT_BUF_SIZE
))
312 if (mc_write (fd
, (char *) edit
->buffers2
[buf
], EDIT_BUF_SIZE
) != EDIT_BUF_SIZE
)
324 /* Update the file information, especially the mtime. */
325 if (mc_stat (savename_vpath
, &edit
->stat1
) == -1)
329 { /* change line breaks */
331 const vfs_path_element_t
*path_element
;
335 path_element
= vfs_path_get_by_index (savename_vpath
, -1);
336 file
= (FILE *) fopen (path_element
->path
, "w");
339 filelen
= edit_write_stream (edit
, file
);
346 msg
= g_strdup_printf (_("Cannot open file for writing: %s"), path_element
->path
);
347 edit_error_dialog (_("Error"), msg
);
353 if (filelen
!= edit
->last_byte
)
356 if (this_save_mode
== EDIT_DO_BACKUP
)
358 vfs_path_t
*tmp_vpath
;
362 assert (option_backup_ext
!= NULL
);
364 tmp_vpath
= vfs_path_append_new (real_filename_vpath
, option_backup_ext
, (char *) NULL
);
365 ok
= (mc_rename (real_filename_vpath
, tmp_vpath
) != -1);
366 vfs_path_free (tmp_vpath
);
371 if (this_save_mode
!= EDIT_QUICK_SAVE
)
372 if (mc_rename (savename_vpath
, real_filename_vpath
) == -1)
375 vfs_path_free (real_filename_vpath
);
376 vfs_path_free (savename_vpath
);
379 /* FIXME: Is this safe ?
380 * if (this_save_mode != EDIT_QUICK_SAVE)
381 * mc_unlink (savename);
383 vfs_path_free (real_filename_vpath
);
384 vfs_path_free (savename_vpath
);
388 /* --------------------------------------------------------------------------------------------- */
391 edit_check_newline (WEdit
* edit
)
393 return !(option_check_nl_at_eof
&& edit
->last_byte
> 0
394 && edit_get_byte (edit
, edit
->last_byte
- 1) != '\n'
395 && edit_query_dialog2 (_("Warning"),
396 _("The file you are saving is not finished with a newline"),
397 _("C&ontinue"), _("&Cancel")));
400 /* --------------------------------------------------------------------------------------------- */
403 edit_get_save_file_as (WEdit
* edit
)
406 #define DLG_HEIGHT 14
408 static LineBreaks cur_lb
= LB_ASIS
;
410 char *filename
= vfs_path_to_str (edit
->filename_vpath
);
413 const char *lb_names
[LB_NAMES
] = {
414 N_("&Do not change"),
415 N_("&Unix format (LF)"),
416 N_("&Windows/DOS format (CR LF)"),
417 N_("&Macintosh format (CR)")
420 QuickWidget quick_widgets
[] = {
421 QUICK_BUTTON (6, 10, DLG_HEIGHT
- 3, DLG_HEIGHT
, N_("&Cancel"), B_CANCEL
, NULL
),
422 QUICK_BUTTON (2, 10, DLG_HEIGHT
- 3, DLG_HEIGHT
, N_("&OK"), B_ENTER
, NULL
),
423 QUICK_RADIO (5, DLG_WIDTH
, DLG_HEIGHT
- 8, DLG_HEIGHT
, LB_NAMES
, lb_names
, (int *) &cur_lb
),
424 QUICK_LABEL (3, DLG_WIDTH
, DLG_HEIGHT
- 9, DLG_HEIGHT
, N_("Change line breaks to:")),
425 QUICK_INPUT (3, DLG_WIDTH
, DLG_HEIGHT
- 11, DLG_HEIGHT
, filename
, DLG_WIDTH
- 6, 0,
426 "save-as", &filename_res
),
427 QUICK_LABEL (3, DLG_WIDTH
, DLG_HEIGHT
- 12, DLG_HEIGHT
, N_("Enter file name:")),
431 QuickDialog Quick_options
= {
432 DLG_WIDTH
, DLG_HEIGHT
, -1, -1,
433 N_("Save As"), "[Save File As]",
434 quick_widgets
, NULL
, FALSE
437 if (quick_dialog (&Quick_options
) != B_CANCEL
)
440 vfs_path_t
*ret_vpath
;
443 fname
= tilde_expand (filename_res
);
444 g_free (filename_res
);
445 ret_vpath
= vfs_path_from_str (fname
);
457 /* --------------------------------------------------------------------------------------------- */
459 /** returns 1 on success */
462 edit_save_cmd (WEdit
* edit
)
464 int res
, save_lock
= 0;
466 if (!edit
->locked
&& !edit
->delete_file
)
467 save_lock
= lock_file (edit
->filename_vpath
);
468 res
= edit_save_file (edit
, edit
->filename_vpath
);
470 /* Maintain modify (not save) lock on failure */
471 if ((res
> 0 && edit
->locked
) || save_lock
)
472 edit
->locked
= unlock_file (edit
->filename_vpath
);
474 /* On failure try 'save as', it does locking on its own */
476 return edit_save_as_cmd (edit
);
477 edit
->force
|= REDRAW_COMPLETELY
;
480 edit
->delete_file
= 0;
487 /* --------------------------------------------------------------------------------------------- */
488 /** returns 1 on error */
491 edit_load_file_from_filename (WEdit
* edit
, const vfs_path_t
* exp_vpath
)
493 int prev_locked
= edit
->locked
;
494 vfs_path_t
*prev_filename
;
497 prev_filename
= vfs_path_clone (edit
->filename_vpath
);
498 if (!edit_reload (edit
, exp_vpath
))
500 else if (prev_locked
)
501 unlock_file (prev_filename
);
503 vfs_path_free (prev_filename
);
507 /* --------------------------------------------------------------------------------------------- */
510 edit_load_syntax_file (WEdit
* edit
)
512 vfs_path_t
*extdir_vpath
;
517 dir
= query_dialog (_("Syntax file edit"),
518 _("Which syntax file you want to edit?"), D_NORMAL
, 2,
519 _("&User"), _("&System Wide"));
523 vfs_path_build_filename (mc_global
.sysconfig_dir
, "syntax", "Syntax", (char *) NULL
);
524 if (!exist_file (vfs_path_get_last_path_str (extdir_vpath
)))
526 vfs_path_free (extdir_vpath
);
528 vfs_path_build_filename (mc_global
.share_data_dir
, "syntax", "Syntax", (char *) NULL
);
533 vfs_path_t
*user_syntax_file_vpath
;
535 user_syntax_file_vpath
= mc_config_get_full_vpath (EDIT_SYNTAX_FILE
);
536 check_for_default (extdir_vpath
, user_syntax_file_vpath
);
537 edit_load_file_from_filename (edit
, user_syntax_file_vpath
);
538 vfs_path_free (user_syntax_file_vpath
);
541 edit_load_file_from_filename (edit
, extdir_vpath
);
543 vfs_path_free (extdir_vpath
);
546 /* --------------------------------------------------------------------------------------------- */
549 edit_load_menu_file (WEdit
* edit
)
551 vfs_path_t
*buffer_vpath
;
552 vfs_path_t
*menufile_vpath
;
555 dir
= query_dialog (_("Menu edit"),
556 _("Which menu file do you want to edit?"), D_NORMAL
,
557 geteuid () != 0 ? 2 : 3, _("&Local"), _("&User"), _("&System Wide"));
559 menufile_vpath
= vfs_path_build_filename (mc_global
.sysconfig_dir
, EDIT_GLOBAL_MENU
, NULL
);
561 if (!exist_file (vfs_path_get_last_path_str (menufile_vpath
)))
563 vfs_path_free (menufile_vpath
);
564 menufile_vpath
= vfs_path_build_filename (mc_global
.share_data_dir
, EDIT_GLOBAL_MENU
, NULL
);
570 buffer_vpath
= vfs_path_from_str (EDIT_LOCAL_MENU
);
571 check_for_default (menufile_vpath
, buffer_vpath
);
572 chmod (vfs_path_get_last_path_str (buffer_vpath
), 0600);
576 buffer_vpath
= mc_config_get_full_vpath (EDIT_HOME_MENU
);
577 check_for_default (menufile_vpath
, buffer_vpath
);
581 buffer_vpath
= vfs_path_build_filename (mc_global
.sysconfig_dir
, EDIT_GLOBAL_MENU
, NULL
);
582 if (!exist_file (vfs_path_get_last_path_str (buffer_vpath
)))
584 vfs_path_free (buffer_vpath
);
586 vfs_path_build_filename (mc_global
.share_data_dir
, EDIT_GLOBAL_MENU
, NULL
);
591 vfs_path_free (menufile_vpath
);
595 edit_load_file_from_filename (edit
, buffer_vpath
);
597 vfs_path_free (buffer_vpath
);
598 vfs_path_free (menufile_vpath
);
601 /* --------------------------------------------------------------------------------------------- */
604 edit_delete_column_of_text (WEdit
* edit
)
606 long p
, q
, r
, m1
, m2
;
609 eval_marks (edit
, &m1
, &m2
);
610 n
= edit_move_forward (edit
, m1
, 0, m2
) + 1;
611 c
= edit_move_forward3 (edit
, edit_bol (edit
, m1
), 0, m1
);
612 d
= edit_move_forward3 (edit
, edit_bol (edit
, m2
), 0, m2
);
613 b
= max (min (c
, d
), min (edit
->column1
, edit
->column2
));
614 c
= max (c
, max (edit
->column1
, edit
->column2
));
618 r
= edit_bol (edit
, edit
->curs1
);
619 p
= edit_move_forward3 (edit
, r
, b
, 0);
620 q
= edit_move_forward3 (edit
, r
, c
, 0);
625 edit_cursor_move (edit
, p
- edit
->curs1
);
628 /* delete line between margins */
629 if (edit_get_byte (edit
, edit
->curs1
) != '\n')
630 edit_delete (edit
, 1);
634 /* move to next line except on the last delete */
635 edit_cursor_move (edit
, edit_move_forward (edit
, edit
->curs1
, 1, 0) - edit
->curs1
);
639 /* --------------------------------------------------------------------------------------------- */
640 /** if success return 0 */
643 edit_block_delete (WEdit
* edit
)
646 long start_mark
, end_mark
;
647 int curs_pos
, line_width
;
648 long curs_line
, c1
, c2
;
650 if (eval_marks (edit
, &start_mark
, &end_mark
))
652 if (edit
->column_highlight
&& edit
->mark2
< 0)
653 edit_mark_cmd (edit
, 0);
654 if ((end_mark
- start_mark
) > option_max_undo
/ 2)
656 /* Warning message with a query to continue or cancel the operation */
657 if (edit_query_dialog2
660 ("Block is large, you may not be able to undo this action"),
661 _("C&ontinue"), _("&Cancel")))
666 c1
= min (edit
->column1
, edit
->column2
);
667 c2
= max (edit
->column1
, edit
->column2
);
671 edit_push_markers (edit
);
673 curs_line
= edit
->curs_line
;
675 /* calculate line width and cursor position before cut */
676 line_width
= edit_move_forward3 (edit
, edit_bol (edit
, edit
->curs1
), 0,
677 edit_eol (edit
, edit
->curs1
));
678 curs_pos
= edit
->curs_col
+ edit
->over_col
;
680 /* move cursor to start of selection */
681 edit_cursor_move (edit
, start_mark
- edit
->curs1
);
682 edit_scroll_screen_over_cursor (edit
);
684 if (start_mark
< end_mark
)
686 if (edit
->column_highlight
)
689 edit_mark_cmd (edit
, 0);
690 edit_delete_column_of_text (edit
);
691 /* move cursor to the saved position */
692 edit_move_to_line (edit
, curs_line
);
693 /* calculate line width after cut */
694 line_width
= edit_move_forward3 (edit
, edit_bol (edit
, edit
->curs1
), 0,
695 edit_eol (edit
, edit
->curs1
));
696 if (option_cursor_beyond_eol
&& curs_pos
> line_width
)
697 edit
->over_col
= curs_pos
- line_width
;
701 while (count
< end_mark
)
703 edit_delete (edit
, 1);
708 edit_set_markers (edit
, 0, 0, 0, 0);
709 edit
->force
|= REDRAW_PAGE
;
713 /* --------------------------------------------------------------------------------------------- */
715 * Get EOL symbol for searching.
717 * @param edit editor object
722 edit_search_get_current_end_line_char (const WEdit
* edit
)
733 /* --------------------------------------------------------------------------------------------- */
735 * Checking if search condition have BOL(^) or EOL ($) regexp special characters.
737 * @param search search object
738 * @return result of checks.
741 static edit_search_line_t
742 edit_get_search_line_type (mc_search_t
* search
)
744 edit_search_line_t search_line_type
= 0;
746 if (search
->search_type
!= MC_SEARCH_T_REGEX
)
747 return search_line_type
;
749 if (*search
->original
== '^')
750 search_line_type
|= AT_START_LINE
;
752 if (search
->original
[search
->original_len
- 1] == '$')
753 search_line_type
|= AT_END_LINE
;
754 return search_line_type
;
757 /* --------------------------------------------------------------------------------------------- */
759 * Calculating the start position of next line.
761 * @param edit editor object
762 * @param current_pos current position
763 * @param max_pos max position
764 * @param end_string_symbol end of line symbol
765 * @return start position of next line
769 edit_calculate_start_of_next_line (WEdit
* edit
, off_t current_pos
, off_t max_pos
,
770 char end_string_symbol
)
774 for (i
= current_pos
; i
< max_pos
; i
++)
777 if (edit_get_byte (edit
, i
) == end_string_symbol
)
784 /* --------------------------------------------------------------------------------------------- */
786 * Calculating the end position of previous line.
788 * @param edit editor object
789 * @param current_pos current position
790 * @param end_string_symbol end of line symbol
791 * @return end position of previous line
795 edit_calculate_end_of_previous_line (WEdit
* edit
, off_t current_pos
, char end_string_symbol
)
799 for (i
= current_pos
- 1; i
>= 0; i
--)
800 if (edit_get_byte (edit
, i
) == end_string_symbol
)
806 /* --------------------------------------------------------------------------------------------- */
808 * Calculating the start position of previous line.
810 * @param edit editor object
811 * @param current_pos current position
812 * @param end_string_symbol end of line symbol
813 * @return start position of previous line
817 edit_calculate_start_of_previous_line (WEdit
* edit
, off_t current_pos
, char end_string_symbol
)
819 current_pos
= edit_calculate_end_of_previous_line (edit
, current_pos
, end_string_symbol
);
820 current_pos
= edit_calculate_end_of_previous_line (edit
, current_pos
, end_string_symbol
);
822 return (current_pos
+ 1);
825 /* --------------------------------------------------------------------------------------------- */
827 * Calculating the start position of current line.
829 * @param edit editor object
830 * @param current_pos current position
831 * @param end_string_symbol end of line symbol
832 * @return start position of current line
836 edit_calculate_start_of_current_line (WEdit
* edit
, off_t current_pos
, char end_string_symbol
)
838 current_pos
= edit_calculate_end_of_previous_line (edit
, current_pos
, end_string_symbol
);
840 return (current_pos
+ 1);
843 /* --------------------------------------------------------------------------------------------- */
845 * Fixing (if needed) search start position if 'only in selection' option present.
847 * @param edit editor object
851 edit_search_fix_search_start_if_selection (WEdit
* edit
)
856 if (!edit_search_options
.only_in_selection
)
859 if (eval_marks (edit
, &start_mark
, &end_mark
) != 0)
862 if (edit_search_options
.backwards
)
864 if (edit
->search_start
> end_mark
|| edit
->search_start
<= start_mark
)
865 edit
->search_start
= end_mark
;
869 if (edit
->search_start
< start_mark
|| edit
->search_start
>= end_mark
)
870 edit
->search_start
= start_mark
;
874 /* --------------------------------------------------------------------------------------------- */
877 editcmd_find (WEdit
* edit
, gsize
* len
)
879 off_t search_start
= edit
->search_start
;
882 long end_mark
= edit
->last_byte
;
884 char end_string_symbol
;
886 end_string_symbol
= edit_search_get_current_end_line_char (edit
);
888 /* prepare for search */
889 if (edit_search_options
.only_in_selection
)
891 mark_res
= eval_marks (edit
, &start_mark
, &end_mark
);
894 edit
->search
->error
= MC_SEARCH_E_NOTFOUND
;
895 edit
->search
->error_str
= g_strdup (_("Search string not found"));
899 /* fix the start and the end of search block positions */
900 if ((edit
->search_line_type
& AT_START_LINE
) != 0
901 && (start_mark
!= 0 || edit_get_byte (edit
, start_mark
- 1) != end_string_symbol
))
904 edit_calculate_start_of_next_line (edit
, start_mark
, edit
->last_byte
,
907 if ((edit
->search_line_type
& AT_END_LINE
) != 0
908 && (end_mark
- 1 != edit
->last_byte
909 || edit_get_byte (edit
, end_mark
) != end_string_symbol
))
911 end_mark
= edit_calculate_end_of_previous_line (edit
, end_mark
, end_string_symbol
);
913 if (start_mark
>= end_mark
)
915 edit
->search
->error
= MC_SEARCH_E_NOTFOUND
;
916 edit
->search
->error_str
= g_strdup (_("Search string not found"));
922 if (edit_search_options
.backwards
)
923 end_mark
= max (1, edit
->curs1
) - 1;
927 if (edit_search_options
.backwards
)
929 /* backward search */
930 search_end
= end_mark
;
932 if ((edit
->search_line_type
& AT_START_LINE
) != 0)
934 edit_calculate_start_of_current_line (edit
, search_start
, end_string_symbol
);
936 while ((int) search_start
>= start_mark
)
938 if (search_end
> (off_t
) (search_start
+ edit
->search
->original_len
)
939 && mc_search_is_fixed_search_str (edit
->search
))
941 search_end
= search_start
+ edit
->search
->original_len
;
943 if (mc_search_run (edit
->search
, (void *) edit
, search_start
, search_end
, len
)
944 && edit
->search
->normal_offset
== search_start
)
950 if ((edit
->search_line_type
& AT_START_LINE
) != 0)
952 edit_calculate_start_of_previous_line (edit
, search_start
, end_string_symbol
);
956 edit
->search
->error_str
= g_strdup (_("Search string not found"));
961 if ((edit
->search_line_type
& AT_START_LINE
) != 0 && search_start
!= start_mark
)
963 edit_calculate_start_of_next_line (edit
, search_start
, end_mark
, end_string_symbol
);
964 return mc_search_run (edit
->search
, (void *) edit
, search_start
, end_mark
, len
);
969 /* --------------------------------------------------------------------------------------------- */
972 edit_replace_cmd__conv_to_display (char *str
)
977 tmp
= str_convert_to_display (str
);
981 return g_string_free (tmp
, FALSE
);
982 g_string_free (tmp
, TRUE
);
985 return g_strdup (str
);
988 /* --------------------------------------------------------------------------------------------- */
991 edit_replace_cmd__conv_to_input (char *str
)
996 tmp
= str_convert_to_input (str
);
1000 return g_string_free (tmp
, FALSE
);
1001 g_string_free (tmp
, TRUE
);
1004 return g_strdup (str
);
1007 /* --------------------------------------------------------------------------------------------- */
1010 edit_do_search (WEdit
* edit
)
1014 if (edit
->search
== NULL
)
1015 edit
->search_start
= edit
->curs1
;
1017 edit_push_undo_action (edit
, KEY_PRESS
+ edit
->start_display
);
1019 if (search_create_bookmark
)
1021 int found
= 0, books
= 0;
1022 long l
= 0, l_last
= -1;
1025 search_create_bookmark
= FALSE
;
1026 book_mark_flush (edit
, -1);
1030 if (!mc_search_run (edit
->search
, (void *) edit
, q
, edit
->last_byte
, &len
))
1033 edit
->search_start
= edit
->search
->normal_offset
;
1035 l
+= edit_count_lines (edit
, q
, edit
->search
->normal_offset
);
1038 book_mark_insert (edit
, l
, BOOK_MARK_FOUND_COLOR
);
1042 q
= edit
->search
->normal_offset
+ 1;
1046 edit_error_dialog (_("Search"), _("Search string not found"));
1048 edit_cursor_move (edit
, edit
->search_start
- edit
->curs1
);
1052 if (edit
->found_len
!= 0 && edit
->search_start
== edit
->found_start
+ 1
1053 && edit_search_options
.backwards
)
1054 edit
->search_start
--;
1056 if (edit
->found_len
!= 0 && edit
->search_start
== edit
->found_start
- 1
1057 && !edit_search_options
.backwards
)
1058 edit
->search_start
++;
1060 if (editcmd_find (edit
, &len
))
1062 edit
->found_start
= edit
->search_start
= edit
->search
->normal_offset
;
1063 edit
->found_len
= len
;
1065 edit_cursor_move (edit
, edit
->search_start
- edit
->curs1
);
1066 edit_scroll_screen_over_cursor (edit
);
1067 if (edit_search_options
.backwards
)
1068 edit
->search_start
--;
1070 edit
->search_start
++;
1074 edit
->search_start
= edit
->curs1
;
1075 if (edit
->search
->error_str
!= NULL
)
1076 edit_error_dialog (_("Search"), edit
->search
->error_str
);
1080 edit
->force
|= REDRAW_COMPLETELY
;
1081 edit_scroll_screen_over_cursor (edit
);
1084 /* --------------------------------------------------------------------------------------------- */
1087 edit_search (WEdit
* edit
)
1089 if (editcmd_dialog_search_show (edit
))
1091 edit
->search_line_type
= edit_get_search_line_type (edit
->search
);
1092 edit_search_fix_search_start_if_selection (edit
);
1093 edit_do_search (edit
);
1097 /* --------------------------------------------------------------------------------------------- */
1098 /** Return a null terminated length of text. Result must be g_free'd */
1100 static unsigned char *
1101 edit_get_block (WEdit
* edit
, long start
, long finish
, int *l
)
1103 unsigned char *s
, *r
;
1104 r
= s
= g_malloc0 (finish
- start
+ 1);
1105 if (edit
->column_highlight
)
1108 /* copy from buffer, excluding chars that are out of the column 'margins' */
1109 while (start
< finish
)
1113 x
= edit_move_forward3 (edit
, edit_bol (edit
, start
), 0, start
);
1114 c
= edit_get_byte (edit
, start
);
1115 if ((x
>= edit
->column1
&& x
< edit
->column2
)
1116 || (x
>= edit
->column2
&& x
< edit
->column1
) || c
== '\n')
1126 *l
= finish
- start
;
1127 while (start
< finish
)
1128 *s
++ = edit_get_byte (edit
, start
++);
1134 /* --------------------------------------------------------------------------------------------- */
1135 /** copies a block to clipboard file */
1138 edit_save_block_to_clip_file (WEdit
* edit
, long start
, long finish
)
1142 tmp
= mc_config_get_full_path (EDIT_CLIP_FILE
);
1143 ret
= edit_save_block (edit
, tmp
, start
, finish
);
1148 /* --------------------------------------------------------------------------------------------- */
1151 pipe_mail (WEdit
* edit
, char *to
, char *subject
, char *cc
)
1156 to
= name_quote (to
, 0);
1157 subject
= name_quote (subject
, 0);
1158 cc
= name_quote (cc
, 0);
1159 s
= g_strconcat ("mail -s ", subject
, *cc
? " -c " : "", cc
, " ", to
, (char *) NULL
);
1173 for (i
= 0; i
< edit
->last_byte
; i
++)
1174 fputc (edit_get_byte (edit
, i
), p
);
1179 /* --------------------------------------------------------------------------------------------- */
1182 is_break_char (char c
)
1184 return (isspace (c
) || strchr ("{}[]()<>=|/\\!?~-+`'\",.;:#$%^&*", c
));
1187 /* --------------------------------------------------------------------------------------------- */
1188 /** find first character of current word */
1191 edit_find_word_start (WEdit
* edit
, long *word_start
, gsize
* word_len
)
1196 /* return if at begin of file */
1197 if (edit
->curs1
<= 0)
1200 c
= (unsigned char) edit_get_byte (edit
, edit
->curs1
- 1);
1201 /* return if not at end or in word */
1202 if (is_break_char (c
))
1205 /* search start of word to be completed */
1208 /* return if at begin of file */
1209 if ((gsize
) edit
->curs1
< i
)
1213 c
= (unsigned char) edit_get_byte (edit
, edit
->curs1
- i
);
1215 if (is_break_char (c
))
1217 /* return if word starts with digit */
1221 *word_start
= edit
->curs1
- (i
- 1); /* start found */
1230 /* --------------------------------------------------------------------------------------------- */
1232 * Get current word under cursor
1234 * @param edit editor object
1235 * @param srch mc_search object
1236 * @param word_start start word position
1238 * @return newly allocated string or NULL if no any words under cursor
1242 edit_collect_completions_get_current_word (WEdit
* edit
, mc_search_t
* srch
, long word_start
)
1247 if (!mc_search_run (srch
, (void *) edit
, word_start
, edit
->last_byte
, &len
))
1250 temp
= g_string_sized_new (len
);
1252 for (i
= 0; i
< len
; i
++)
1256 chr
= edit_get_byte (edit
, word_start
+ i
);
1258 g_string_append_c (temp
, chr
);
1261 return g_string_free (temp
, temp
->len
== 0);
1264 /* --------------------------------------------------------------------------------------------- */
1265 /** collect the possible completions */
1268 edit_collect_completions (WEdit
* edit
, long word_start
, gsize word_len
,
1269 char *match_expr
, struct selection
*compl, gsize
* num
)
1277 long last_byte
, start
= -1;
1280 srch
= mc_search_new (match_expr
, -1);
1284 if (mc_config_get_bool
1285 (mc_main_config
, CONFIG_APP_SECTION
, "editor_wordcompletion_collect_entire_file", 0))
1287 last_byte
= edit
->last_byte
;
1291 last_byte
= word_start
;
1294 srch
->search_type
= MC_SEARCH_T_REGEX
;
1295 srch
->is_case_sensitive
= TRUE
;
1296 srch
->search_fn
= edit_search_cmd_callback
;
1298 current_word
= edit_collect_completions_get_current_word (edit
, srch
, word_start
);
1300 temp
= g_string_new ("");
1302 /* collect max MAX_WORD_COMPLETIONS completions */
1303 while (mc_search_run (srch
, (void *) edit
, start
+ 1, last_byte
, &len
))
1305 g_string_set_size (temp
, 0);
1306 start
= srch
->normal_offset
;
1308 /* add matched completion if not yet added */
1309 for (i
= 0; i
< len
; i
++)
1311 skip
= edit_get_byte (edit
, start
+ i
);
1315 /* skip current word */
1316 if (start
+ (long) i
== word_start
)
1319 g_string_append_c (temp
, skip
);
1325 if (current_word
!= NULL
&& strcmp (current_word
, temp
->str
) == 0)
1330 for (i
= 0; i
< *num
; i
++)
1333 ((char *) &compl[i
].text
[word_len
],
1334 (char *) &temp
->str
[word_len
], max (len
, compl[i
].len
) - word_len
) == 0)
1336 struct selection
this = compl[i
];
1337 for (++i
; i
< *num
; i
++)
1339 compl[i
- 1] = compl[i
];
1341 compl[*num
- 1] = this;
1343 break; /* skip it, already added */
1349 if (*num
== MAX_WORD_COMPLETIONS
)
1351 g_free (compl[0].text
);
1352 for (i
= 1; i
< *num
; i
++)
1354 compl[i
- 1] = compl[i
];
1361 recoded
= str_convert_to_display (temp
->str
);
1363 if (recoded
&& recoded
->len
)
1364 g_string_assign (temp
, recoded
->str
);
1366 g_string_free (recoded
, TRUE
);
1369 compl[*num
].text
= g_strdup (temp
->str
);
1370 compl[*num
].len
= temp
->len
;
1374 /* note the maximal length needed for the completion dialog */
1379 mc_search_free (srch
);
1380 g_string_free (temp
, TRUE
);
1381 g_free (current_word
);
1386 /* --------------------------------------------------------------------------------------------- */
1389 edit_insert_column_of_text (WEdit
* edit
, unsigned char *data
, int size
, int width
,
1390 long *start_pos
, long *end_pos
, int *col1
, int *col2
)
1395 cursor
= edit
->curs1
;
1396 col
= edit_get_col (edit
);
1398 for (i
= 0; i
< size
; i
++)
1400 if (data
[i
] != '\n')
1401 edit_insert (edit
, data
[i
]);
1403 { /* fill in and move to next line */
1407 if (edit_get_byte (edit
, edit
->curs1
) != '\n')
1409 l
= width
- (edit_get_col (edit
) - col
);
1412 edit_insert (edit
, ' ');
1416 for (p
= edit
->curs1
;; p
++)
1418 if (p
== edit
->last_byte
)
1420 edit_cursor_move (edit
, edit
->last_byte
- edit
->curs1
);
1421 edit_insert_ahead (edit
, '\n');
1425 if (edit_get_byte (edit
, p
) == '\n')
1431 edit_cursor_move (edit
, edit_move_forward3 (edit
, p
, col
, 0) - edit
->curs1
);
1432 l
= col
- edit_get_col (edit
);
1433 while (l
>= space_width
)
1435 edit_insert (edit
, ' ');
1442 *col2
= col
+ width
;
1443 *start_pos
= cursor
;
1444 *end_pos
= edit
->curs1
;
1445 edit_cursor_move (edit
, cursor
- edit
->curs1
);
1448 /* --------------------------------------------------------------------------------------------- */
1451 edit_macro_comparator (gconstpointer
* macro1
, gconstpointer
* macro2
)
1453 const macros_t
*m1
= (const macros_t
*) macro1
;
1454 const macros_t
*m2
= (const macros_t
*) macro2
;
1456 return m1
->hotkey
- m2
->hotkey
;
1459 /* --------------------------------------------------------------------------------------------- */
1462 edit_macro_sort_by_hotkey (void)
1464 if (macros_list
!= NULL
&& macros_list
->len
!= 0)
1465 g_array_sort (macros_list
, (GCompareFunc
) edit_macro_comparator
);
1468 /* --------------------------------------------------------------------------------------------- */
1471 edit_get_macro (WEdit
* edit
, int hotkey
, const macros_t
** macros
, guint
* indx
)
1473 const macros_t
*array_start
= &g_array_index (macros_list
, struct macros_t
, 0);
1475 macros_t search_macro
;
1479 search_macro
.hotkey
= hotkey
;
1480 result
= bsearch (&search_macro
, macros_list
->data
, macros_list
->len
,
1481 sizeof (macros_t
), (GCompareFunc
) edit_macro_comparator
);
1483 if (result
!= NULL
&& result
->macro
!= NULL
)
1485 *indx
= (result
- array_start
);
1493 /* --------------------------------------------------------------------------------------------- */
1494 /** returns FALSE on error */
1497 edit_delete_macro (WEdit
* edit
, int hotkey
)
1499 mc_config_t
*macros_config
= NULL
;
1500 const char *section_name
= "editor";
1501 gchar
*macros_fname
;
1504 const macros_t
*macros
= NULL
;
1506 /* clear array of actions for current hotkey */
1507 while (edit_get_macro (edit
, hotkey
, ¯os
, &indx
))
1509 if (macros
->macro
!= NULL
)
1510 g_array_free (macros
->macro
, TRUE
);
1512 g_array_remove_index (macros_list
, indx
);
1513 edit_macro_sort_by_hotkey ();
1516 macros_fname
= mc_config_get_full_path (MC_MACRO_FILE
);
1517 macros_config
= mc_config_init (macros_fname
);
1518 g_free (macros_fname
);
1520 if (macros_config
== NULL
)
1523 skeyname
= lookup_key_by_code (hotkey
);
1524 while (mc_config_del_key (macros_config
, section_name
, skeyname
))
1527 mc_config_save_file (macros_config
, NULL
);
1528 mc_config_deinit (macros_config
);
1533 /* --------------------------------------------------------------------------------------------- */
1534 /*** public functions ****************************************************************************/
1535 /* --------------------------------------------------------------------------------------------- */
1538 edit_help_cmd (WEdit
* edit
)
1540 ev_help_t event_data
= { NULL
, "[Internal File Editor]" };
1541 mc_event_raise (MCEVENT_GROUP_CORE
, "help", &event_data
);
1543 edit
->force
|= REDRAW_COMPLETELY
;
1546 /* --------------------------------------------------------------------------------------------- */
1549 edit_refresh_cmd (WEdit
* edit
)
1554 edit_get_syntax_color (edit
, -1, &color
);
1555 tty_touch_screen ();
1562 #endif /* !HAVE_SLANG */
1566 /* --------------------------------------------------------------------------------------------- */
1569 menu_save_mode_cmd (void)
1572 const int DLG_X
= 38;
1573 const int DLG_Y
= 13;
1577 const char *str
[] = {
1580 N_("&Do backups with following extension:")
1583 QuickWidget widgets
[] = {
1585 QUICK_BUTTON (18, DLG_X
, DLG_Y
- 3, DLG_Y
, N_("&Cancel"), B_CANCEL
, NULL
),
1587 QUICK_BUTTON (6, DLG_X
, DLG_Y
- 3, DLG_Y
, N_("&OK"), B_ENTER
, NULL
),
1589 QUICK_CHECKBOX (4, DLG_X
, 8, DLG_Y
, N_("Check &POSIX new line"), &option_check_nl_at_eof
),
1591 QUICK_INPUT (8, DLG_X
, 6, DLG_Y
, option_backup_ext
, 9, 0, "edit-backup-ext", &str_result
),
1593 QUICK_RADIO (4, DLG_X
, 3, DLG_Y
, 3, str
, &option_save_mode
),
1597 QuickDialog dialog
= {
1598 DLG_X
, DLG_Y
, -1, -1, N_("Edit Save Mode"),
1599 "[Edit Save Mode]", widgets
, NULL
, FALSE
1604 size_t w0
, w1
, b_len
, w3
;
1606 #ifdef HAVE_ASSERT_H
1607 assert (option_backup_ext
!= NULL
);
1610 /* OK/Cancel buttons */
1611 w0
= str_term_width1 (_(widgets
[0].u
.button
.text
)) + 3;
1612 w1
= str_term_width1 (_(widgets
[1].u
.button
.text
)) + 5; /* default button */
1613 b_len
= w0
+ w1
+ 3;
1615 maxlen
= max (b_len
, (size_t) str_term_width1 (_(dialog
.title
)) + 2);
1618 for (i
= 0; i
< 3; i
++)
1623 w3
= max (w3
, (size_t) str_term_width1 (str
[i
]));
1626 maxlen
= max (maxlen
, w3
+ 4);
1628 dialog
.xlen
= min ((size_t) COLS
, maxlen
+ 8);
1630 widgets
[3].u
.input
.len
= w3
;
1631 widgets
[1].relative_x
= (dialog
.xlen
- b_len
) / 2;
1632 widgets
[0].relative_x
= widgets
[1].relative_x
+ w0
+ 2;
1634 for (i
= 0; i
< sizeof (widgets
) / sizeof (widgets
[0]); i
++)
1635 widgets
[i
].x_divisions
= dialog
.xlen
;
1637 if (quick_dialog (&dialog
) != B_CANCEL
)
1639 g_free (option_backup_ext
);
1640 option_backup_ext
= str_result
;
1644 /* --------------------------------------------------------------------------------------------- */
1647 edit_set_filename (WEdit
* edit
, const vfs_path_t
* name_vpath
)
1649 vfs_path_free (edit
->filename_vpath
);
1650 edit
->filename_vpath
= vfs_path_clone (name_vpath
);
1652 if (edit
->dir_vpath
== NULL
)
1653 edit
->dir_vpath
= vfs_path_clone (vfs_get_raw_current_dir ());
1656 /* --------------------------------------------------------------------------------------------- */
1657 /* Here we want to warn the users of overwriting an existing file,
1658 but only if they have made a change to the filename */
1659 /* returns 1 on success */
1661 edit_save_as_cmd (WEdit
* edit
)
1663 /* This heads the 'Save As' dialog box */
1664 vfs_path_t
*exp_vpath
;
1666 int different_filename
= 0;
1668 if (!edit_check_newline (edit
))
1671 exp_vpath
= edit_get_save_file_as (edit
);
1672 edit_push_undo_action (edit
, KEY_PRESS
+ edit
->start_display
);
1674 if (exp_vpath
!= NULL
)
1676 if (vfs_path_len (exp_vpath
) == 0)
1682 if (vfs_path_cmp (edit
->filename_vpath
, exp_vpath
) != 0)
1687 if (mc_stat (exp_vpath
, &sb
) == 0 && !S_ISREG (sb
.st_mode
))
1689 edit_error_dialog (_("Save as"),
1691 ("Cannot save: destination is not a regular file")));
1695 different_filename
= 1;
1696 file
= mc_open (exp_vpath
, O_RDONLY
| O_BINARY
);
1700 /* the file exists */
1702 /* Overwrite the current file or cancel the operation */
1703 if (edit_query_dialog2
1705 _("A file already exists with this name"), _("&Overwrite"), _("&Cancel")))
1710 edit
->stat1
.st_mode
|= S_IWUSR
;
1712 save_lock
= lock_file (exp_vpath
);
1716 /* filenames equal, check if already locked */
1717 if (!edit
->locked
&& !edit
->delete_file
)
1718 save_lock
= lock_file (exp_vpath
);
1721 if (different_filename
)
1724 * Allow user to write into saved (under another name) file
1725 * even if original file had r/o user permissions.
1727 edit
->stat1
.st_mode
|= S_IWRITE
;
1730 rv
= edit_save_file (edit
, exp_vpath
);
1734 /* Succesful, so unlock both files */
1735 if (different_filename
)
1738 unlock_file (exp_vpath
);
1740 edit
->locked
= unlock_file (edit
->filename_vpath
);
1744 if (edit
->locked
|| save_lock
)
1745 edit
->locked
= unlock_file (edit
->filename_vpath
);
1748 edit_set_filename (edit
, exp_vpath
);
1749 if (edit
->lb
!= LB_ASIS
)
1750 edit_reload (edit
, exp_vpath
);
1752 edit
->delete_file
= 0;
1753 if (different_filename
)
1754 edit_load_syntax (edit
, NULL
, edit
->syntax_type
);
1755 vfs_path_free (exp_vpath
);
1756 edit
->force
|= REDRAW_COMPLETELY
;
1759 edit_error_dialog (_("Save as"), get_sys_error (_("Cannot save file")));
1762 /* Failed, so maintain modify (not save) lock */
1764 unlock_file (exp_vpath
);
1771 vfs_path_free (exp_vpath
);
1772 edit
->force
|= REDRAW_COMPLETELY
;
1776 /* {{{ Macro stuff starts here */
1777 /* --------------------------------------------------------------------------------------------- */
1780 edit_delete_macro_cmd (WEdit
* edit
)
1784 hotkey
= editcmd_dialog_raw_key_query (_("Delete macro"), _("Press macro hotkey:"), 1);
1786 if (hotkey
!= 0 && !edit_delete_macro (edit
, hotkey
))
1787 message (D_ERROR
, _("Delete macro"), _("Macro not deleted"));
1790 /* --------------------------------------------------------------------------------------------- */
1792 /** returns FALSE on error */
1794 edit_execute_macro (WEdit
* edit
, int hotkey
)
1796 gboolean res
= FALSE
;
1800 const macros_t
*macros
;
1803 if (edit_get_macro (edit
, hotkey
, ¯os
, &indx
) &&
1804 macros
->macro
!= NULL
&& macros
->macro
->len
!= 0)
1808 edit
->force
|= REDRAW_PAGE
;
1810 for (i
= 0; i
< macros
->macro
->len
; i
++)
1812 const macro_action_t
*m_act
;
1814 m_act
= &g_array_index (macros
->macro
, struct macro_action_t
, i
);
1815 edit_execute_cmd (edit
, m_act
->action
, m_act
->ch
);
1824 /* --------------------------------------------------------------------------------------------- */
1826 /** returns FALSE on error */
1828 edit_store_macro_cmd (WEdit
* edit
)
1832 GString
*marcros_string
;
1833 mc_config_t
*macros_config
= NULL
;
1834 const char *section_name
= "editor";
1835 gchar
*macros_fname
;
1836 GArray
*macros
; /* current macro */
1838 gboolean have_macro
= FALSE
;
1839 char *skeyname
= NULL
;
1841 hotkey
= editcmd_dialog_raw_key_query (_("Save macro"), _("Press the macro's new hotkey:"), 1);
1842 if (hotkey
== ESC_CHAR
)
1845 tmp_act
= keybind_lookup_keymap_command (editor_map
, hotkey
);
1847 /* return FALSE if try assign macro into restricted hotkeys */
1848 if (tmp_act
== CK_MacroStartRecord
1849 || tmp_act
== CK_MacroStopRecord
|| tmp_act
== CK_MacroStartStopRecord
)
1852 edit_delete_macro (edit
, hotkey
);
1854 macros_fname
= mc_config_get_full_path (MC_MACRO_FILE
);
1855 macros_config
= mc_config_init (macros_fname
);
1856 g_free (macros_fname
);
1858 if (macros_config
== NULL
)
1861 edit_push_undo_action (edit
, KEY_PRESS
+ edit
->start_display
);
1863 marcros_string
= g_string_sized_new (250);
1864 macros
= g_array_new (TRUE
, FALSE
, sizeof (macro_action_t
));
1866 skeyname
= lookup_key_by_code (hotkey
);
1868 for (i
= 0; i
< macro_index
; i
++)
1870 macro_action_t m_act
;
1871 const char *action_name
;
1873 action_name
= keybind_lookup_actionname (record_macro_buf
[i
].action
);
1875 if (action_name
== NULL
)
1878 m_act
.action
= record_macro_buf
[i
].action
;
1879 m_act
.ch
= record_macro_buf
[i
].ch
;
1880 g_array_append_val (macros
, m_act
);
1882 g_string_append_printf (marcros_string
, "%s:%i;", action_name
,
1883 (int) record_macro_buf
[i
].ch
);
1888 macro
.hotkey
= hotkey
;
1889 macro
.macro
= macros
;
1890 g_array_append_val (macros_list
, macro
);
1891 mc_config_set_string (macros_config
, section_name
, skeyname
, marcros_string
->str
);
1894 mc_config_del_key (macros_config
, section_name
, skeyname
);
1897 edit_macro_sort_by_hotkey ();
1899 g_string_free (marcros_string
, TRUE
);
1900 mc_config_save_file (macros_config
, NULL
);
1901 mc_config_deinit (macros_config
);
1905 /* --------------------------------------------------------------------------------------------- */
1908 edit_repeat_macro_cmd (WEdit
* edit
)
1915 f
= input_dialog (_("Repeat last commands"), _("Repeat times:"), MC_HISTORY_EDIT_REPEAT
, NULL
);
1916 if (f
== NULL
|| *f
== '\0')
1922 count_repeat
= strtol (f
, &error
, 0);
1932 edit_push_undo_action (edit
, KEY_PRESS
+ edit
->start_display
);
1933 edit
->force
|= REDRAW_PAGE
;
1935 for (j
= 0; j
< count_repeat
; j
++)
1936 for (i
= 0; i
< macro_index
; i
++)
1937 edit_execute_cmd (edit
, record_macro_buf
[i
].action
, record_macro_buf
[i
].ch
);
1938 edit_update_screen (edit
);
1942 /* --------------------------------------------------------------------------------------------- */
1943 /** return FALSE on error */
1946 edit_load_macro_cmd (WEdit
* edit
)
1948 mc_config_t
*macros_config
= NULL
;
1949 gchar
**profile_keys
, **keys
;
1950 gchar
**values
, **curr_values
;
1951 gsize len
, values_len
;
1952 const char *section_name
= "editor";
1953 gchar
*macros_fname
;
1958 macros_fname
= mc_config_get_full_path (MC_MACRO_FILE
);
1959 macros_config
= mc_config_init (macros_fname
);
1960 g_free (macros_fname
);
1962 if (macros_config
== NULL
)
1965 profile_keys
= keys
= mc_config_get_keys (macros_config
, section_name
, &len
);
1966 while (*profile_keys
!= NULL
)
1968 gboolean have_macro
;
1972 macros
= g_array_new (TRUE
, FALSE
, sizeof (macro_action_t
));
1974 curr_values
= values
= mc_config_get_string_list (macros_config
, section_name
,
1975 *profile_keys
, &values_len
);
1976 hotkey
= lookup_key (*profile_keys
, NULL
);
1979 while (*curr_values
!= NULL
&& *curr_values
[0] != '\0')
1981 char **macro_pair
= NULL
;
1983 macro_pair
= g_strsplit (*curr_values
, ":", 2);
1985 if (macro_pair
!= NULL
)
1987 macro_action_t m_act
;
1988 if (macro_pair
[0] == NULL
|| macro_pair
[0][0] == '\0')
1992 m_act
.action
= keybind_lookup_action (macro_pair
[0]);
1993 g_free (macro_pair
[0]);
1994 macro_pair
[0] = NULL
;
1996 if (macro_pair
[1] == NULL
|| macro_pair
[1][0] == '\0')
2000 m_act
.ch
= strtol (macro_pair
[1], NULL
, 0);
2001 g_free (macro_pair
[1]);
2002 macro_pair
[1] = NULL
;
2004 if (m_act
.action
!= 0)
2006 /* a shell command */
2007 if ((m_act
.action
/ CK_PipeBlock (0)) == 1)
2009 m_act
.action
= CK_PipeBlock (0) + (m_act
.ch
> 0 ? m_act
.ch
: 0);
2012 g_array_append_val (macros
, m_act
);
2015 g_strfreev (macro_pair
);
2022 macro
.hotkey
= hotkey
;
2023 macro
.macro
= macros
;
2024 g_array_append_val (macros_list
, macro
);
2027 g_strfreev (values
);
2030 mc_config_deinit (macros_config
);
2031 edit_macro_sort_by_hotkey ();
2035 /* }}} Macro stuff end here */
2037 /* --------------------------------------------------------------------------------------------- */
2038 /** returns 1 on success */
2041 edit_save_confirm_cmd (WEdit
* edit
)
2045 if (edit
->filename_vpath
== NULL
)
2046 return edit_save_as_cmd (edit
);
2048 if (!edit_check_newline (edit
))
2051 if (edit_confirm_save
)
2056 filename
= vfs_path_to_str (edit
->filename_vpath
);
2057 f
= g_strdup_printf (_("Confirm save file: \"%s\""), filename
);
2059 ok
= (edit_query_dialog2 (_("Save file"), f
, _("&Save"), _("&Cancel")) == 0);
2064 return edit_save_cmd (edit
);
2067 /* --------------------------------------------------------------------------------------------- */
2068 /** returns 1 on success */
2071 edit_new_cmd (WEdit
* edit
)
2075 if (edit_query_dialog2
2078 ("Current text was modified without a file save.\nContinue discards these changes"),
2079 _("C&ontinue"), _("&Cancel")))
2081 edit
->force
|= REDRAW_COMPLETELY
;
2085 edit
->force
|= REDRAW_COMPLETELY
;
2087 return edit_renew (edit
); /* if this gives an error, something has really screwed up */
2090 /* --------------------------------------------------------------------------------------------- */
2093 edit_load_cmd (WEdit
* edit
, edit_current_file_t what
)
2096 && (edit_query_dialog2
2098 _("Current text was modified without a file save.\n"
2099 "Continue discards these changes"), _("C&ontinue"), _("&Cancel")) == 1))
2101 edit
->force
|= REDRAW_COMPLETELY
;
2107 case EDIT_FILE_COMMON
:
2109 char *filename
, *exp
;
2111 filename
= vfs_path_to_str (edit
->filename_vpath
);
2112 exp
= input_expand_dialog (_("Load"), _("Enter file name:"),
2113 MC_HISTORY_EDIT_LOAD
, filename
);
2120 vfs_path_t
*exp_vpath
;
2122 exp_vpath
= vfs_path_from_str (exp
);
2123 edit_load_file_from_filename (edit
, exp_vpath
);
2124 vfs_path_free (exp_vpath
);
2131 case EDIT_FILE_SYNTAX
:
2132 edit_load_syntax_file (edit
);
2135 case EDIT_FILE_MENU
:
2136 edit_load_menu_file (edit
);
2143 edit
->force
|= REDRAW_COMPLETELY
;
2147 /* --------------------------------------------------------------------------------------------- */
2149 if mark2 is -1 then marking is from mark1 to the cursor.
2150 Otherwise its between the markers. This handles this.
2151 Returns 1 if no text is marked.
2155 eval_marks (WEdit
* edit
, long *start_mark
, long *end_mark
)
2157 if (edit
->mark1
!= edit
->mark2
)
2159 long start_bol
, start_eol
;
2160 long end_bol
, end_eol
;
2165 if (edit
->end_mark_curs
< 0)
2166 end_mark_curs
= edit
->curs1
;
2168 end_mark_curs
= edit
->end_mark_curs
;
2170 if (edit
->mark2
>= 0)
2172 *start_mark
= min (edit
->mark1
, edit
->mark2
);
2173 *end_mark
= max (edit
->mark1
, edit
->mark2
);
2177 *start_mark
= min (edit
->mark1
, end_mark_curs
);
2178 *end_mark
= max (edit
->mark1
, end_mark_curs
);
2179 edit
->column2
= edit
->curs_col
+ edit
->over_col
;
2182 if (edit
->column_highlight
2183 && (((edit
->mark1
> end_mark_curs
) && (edit
->column1
< edit
->column2
))
2184 || ((edit
->mark1
< end_mark_curs
) && (edit
->column1
> edit
->column2
))))
2186 start_bol
= edit_bol (edit
, *start_mark
);
2187 start_eol
= edit_eol (edit
, start_bol
- 1) + 1;
2188 end_bol
= edit_bol (edit
, *end_mark
);
2189 end_eol
= edit_eol (edit
, *end_mark
);
2190 col1
= min (edit
->column1
, edit
->column2
);
2191 col2
= max (edit
->column1
, edit
->column2
);
2194 edit_move_forward3 (edit
, start_bol
, col2
, 0) - edit_move_forward3 (edit
, start_bol
,
2197 edit_move_forward3 (edit
, end_bol
, col2
, 0) - edit_move_forward3 (edit
, end_bol
,
2200 *start_mark
-= diff1
;
2202 *start_mark
= max (*start_mark
, start_eol
);
2203 *end_mark
= min (*end_mark
, end_eol
);
2209 *start_mark
= *end_mark
= 0;
2210 edit
->column2
= edit
->column1
= 0;
2215 /* --------------------------------------------------------------------------------------------- */
2218 edit_insert_over (WEdit
* edit
)
2222 for (i
= 0; i
< edit
->over_col
; i
++)
2224 edit_insert (edit
, ' ');
2229 /* --------------------------------------------------------------------------------------------- */
2232 edit_insert_column_of_text_from_file (WEdit
* edit
, int file
,
2233 long *start_pos
, long *end_pos
, int *col1
, int *col2
)
2237 int blocklen
= -1, width
= 0;
2238 unsigned char *data
;
2240 cursor
= edit
->curs1
;
2241 col
= edit_get_col (edit
);
2242 data
= g_malloc0 (TEMP_BUF_LEN
);
2244 while ((blocklen
= mc_read (file
, (char *) data
, TEMP_BUF_LEN
)) > 0)
2247 for (width
= 0; width
< blocklen
; width
++)
2249 if (data
[width
] == '\n')
2252 for (i
= 0; i
< blocklen
; i
++)
2254 if (data
[i
] == '\n')
2255 { /* fill in and move to next line */
2258 if (edit_get_byte (edit
, edit
->curs1
) != '\n')
2260 l
= width
- (edit_get_col (edit
) - col
);
2263 edit_insert (edit
, ' ');
2267 for (p
= edit
->curs1
;; p
++)
2269 if (p
== edit
->last_byte
)
2271 edit_cursor_move (edit
, edit
->last_byte
- edit
->curs1
);
2272 edit_insert_ahead (edit
, '\n');
2276 if (edit_get_byte (edit
, p
) == '\n')
2282 edit_cursor_move (edit
, edit_move_forward3 (edit
, p
, col
, 0) - edit
->curs1
);
2283 l
= col
- edit_get_col (edit
);
2284 while (l
>= space_width
)
2286 edit_insert (edit
, ' ');
2291 edit_insert (edit
, data
[i
]);
2295 *col2
= col
+ width
;
2296 *start_pos
= cursor
;
2297 *end_pos
= edit
->curs1
;
2298 edit_cursor_move (edit
, cursor
- edit
->curs1
);
2304 /* --------------------------------------------------------------------------------------------- */
2307 edit_block_copy_cmd (WEdit
* edit
)
2309 long start_mark
, end_mark
, current
= edit
->curs1
;
2314 unsigned char *copy_buf
;
2316 edit_update_curs_col (edit
);
2317 if (eval_marks (edit
, &start_mark
, &end_mark
))
2320 copy_buf
= edit_get_block (edit
, start_mark
, end_mark
, &size
);
2322 /* all that gets pushed are deletes hence little space is used on the stack */
2324 edit_push_markers (edit
);
2326 if (edit
->column_highlight
)
2328 col_delta
= abs (edit
->column2
- edit
->column1
);
2329 edit_insert_column_of_text (edit
, copy_buf
, size
, col_delta
, &mark1
, &mark2
, &c1
, &c2
);
2334 edit_insert_ahead (edit
, copy_buf
[size
]);
2338 edit_scroll_screen_over_cursor (edit
);
2340 if (edit
->column_highlight
)
2341 edit_set_markers (edit
, edit
->curs1
, mark2
, c1
, c2
);
2342 else if (start_mark
< current
&& end_mark
> current
)
2343 edit_set_markers (edit
, start_mark
, end_mark
+ end_mark
- start_mark
, 0, 0);
2345 edit
->force
|= REDRAW_PAGE
;
2349 /* --------------------------------------------------------------------------------------------- */
2352 edit_block_move_cmd (WEdit
* edit
)
2355 unsigned char *copy_buf
= NULL
;
2356 long start_mark
, end_mark
;
2359 if (eval_marks (edit
, &start_mark
, &end_mark
))
2362 line
= edit
->curs_line
;
2363 if (edit
->mark2
< 0)
2364 edit_mark_cmd (edit
, 0);
2365 edit_push_markers (edit
);
2367 if (edit
->column_highlight
)
2375 c1
= min (edit
->column1
, edit
->column2
);
2376 c2
= max (edit
->column1
, edit
->column2
);
2377 b_width
= (c2
- c1
);
2379 edit_update_curs_col (edit
);
2382 x2
= x
+ edit
->over_col
;
2384 /* do nothing when cursor inside first line of selected area */
2385 if ((edit_eol (edit
, edit
->curs1
) == edit_eol (edit
, start_mark
)) && (x2
> c1
&& x2
<= c2
))
2388 if (edit
->curs1
> start_mark
&& edit
->curs1
< edit_eol (edit
, end_mark
))
2392 else if (x
> c1
&& x
<= c2
)
2395 /* save current selection into buffer */
2396 copy_buf
= edit_get_block (edit
, start_mark
, end_mark
, &size
);
2398 /* remove current selection */
2399 edit_block_delete_cmd (edit
);
2401 edit
->over_col
= max (0, edit
->over_col
- b_width
);
2402 /* calculate the cursor pos after delete block */
2403 current
= edit_move_forward3 (edit
, edit_bol (edit
, edit
->curs1
), x
, 0);
2404 edit_cursor_move (edit
, current
- edit
->curs1
);
2405 edit_scroll_screen_over_cursor (edit
);
2407 /* add TWS if need before block insertion */
2408 if (option_cursor_beyond_eol
&& edit
->over_col
> 0)
2409 edit_insert_over (edit
);
2411 edit_insert_column_of_text (edit
, copy_buf
, size
, b_width
, &mark1
, &mark2
, &c1
, &c2
);
2412 edit_set_markers (edit
, mark1
, mark2
, c1
, c2
);
2418 current
= edit
->curs1
;
2419 copy_buf
= g_malloc0 (end_mark
- start_mark
);
2420 edit_cursor_move (edit
, start_mark
- edit
->curs1
);
2421 edit_scroll_screen_over_cursor (edit
);
2423 while (count
< end_mark
)
2425 copy_buf
[end_mark
- count
- 1] = edit_delete (edit
, 1);
2428 edit_scroll_screen_over_cursor (edit
);
2429 edit_cursor_move (edit
,
2430 current
- edit
->curs1
-
2431 (((current
- edit
->curs1
) > 0) ? end_mark
- start_mark
: 0));
2432 edit_scroll_screen_over_cursor (edit
);
2433 while (count
-- > start_mark
)
2434 edit_insert_ahead (edit
, copy_buf
[end_mark
- count
- 1]);
2435 edit_set_markers (edit
, edit
->curs1
, edit
->curs1
+ end_mark
- start_mark
, 0, 0);
2438 edit_scroll_screen_over_cursor (edit
);
2440 edit
->force
|= REDRAW_PAGE
;
2443 /* --------------------------------------------------------------------------------------------- */
2444 /** returns 1 if canceelled by user */
2447 edit_block_delete_cmd (WEdit
* edit
)
2449 long start_mark
, end_mark
;
2450 if (eval_marks (edit
, &start_mark
, &end_mark
))
2452 edit_delete_line (edit
);
2455 return edit_block_delete (edit
);
2458 /* --------------------------------------------------------------------------------------------- */
2459 /** call with edit = 0 before shutdown to close memory leaks */
2462 edit_replace_cmd (WEdit
* edit
, int again
)
2464 /* 1 = search string, 2 = replace with */
2465 static char *saved1
= NULL
; /* saved default[123] */
2466 static char *saved2
= NULL
;
2467 char *input1
= NULL
; /* user input from the dialog */
2468 char *input2
= NULL
;
2471 long times_replaced
= 0;
2472 gboolean once_found
= FALSE
;
2476 g_free (saved1
), saved1
= NULL
;
2477 g_free (saved2
), saved2
= NULL
;
2481 edit
->force
|= REDRAW_COMPLETELY
;
2483 if (again
&& !saved1
&& !saved2
)
2488 input1
= g_strdup (saved1
? saved1
: "");
2489 input2
= g_strdup (saved2
? saved2
: "");
2493 char *tmp_inp1
, *tmp_inp2
;
2494 disp1
= edit_replace_cmd__conv_to_display (saved1
? saved1
: (char *) "");
2495 disp2
= edit_replace_cmd__conv_to_display (saved2
? saved2
: (char *) "");
2497 edit_push_undo_action (edit
, KEY_PRESS
+ edit
->start_display
);
2499 editcmd_dialog_replace_show (edit
, disp1
, disp2
, &input1
, &input2
);
2504 if (input1
== NULL
|| *input1
== '\0')
2506 edit
->force
= REDRAW_COMPLETELY
;
2512 input1
= edit_replace_cmd__conv_to_input (input1
);
2513 input2
= edit_replace_cmd__conv_to_input (input2
);
2517 g_free (saved1
), saved1
= g_strdup (input1
);
2518 g_free (saved2
), saved2
= g_strdup (input2
);
2520 mc_search_free (edit
->search
);
2521 edit
->search
= NULL
;
2527 edit
->search
= mc_search_new (input1
, -1);
2528 if (edit
->search
== NULL
)
2530 edit
->search_start
= edit
->curs1
;
2533 edit
->search
->search_type
= edit_search_options
.type
;
2534 edit
->search
->is_all_charsets
= edit_search_options
.all_codepages
;
2535 edit
->search
->is_case_sensitive
= edit_search_options
.case_sens
;
2536 edit
->search
->whole_words
= edit_search_options
.whole_words
;
2537 edit
->search
->search_fn
= edit_search_cmd_callback
;
2538 edit
->search_line_type
= edit_get_search_line_type (edit
->search
);
2539 edit_search_fix_search_start_if_selection (edit
);
2542 if (edit
->found_len
&& edit
->search_start
== edit
->found_start
+ 1
2543 && edit_search_options
.backwards
)
2544 edit
->search_start
--;
2546 if (edit
->found_len
&& edit
->search_start
== edit
->found_start
- 1
2547 && !edit_search_options
.backwards
)
2548 edit
->search_start
++;
2554 if (!editcmd_find (edit
, &len
))
2556 if (!(edit
->search
->error
== MC_SEARCH_E_OK
||
2557 (once_found
&& edit
->search
->error
== MC_SEARCH_E_NOTFOUND
)))
2559 edit_error_dialog (_("Search"), edit
->search
->error_str
);
2565 edit
->search_start
= edit
->search
->normal_offset
;
2566 /*returns negative on not found or error in pattern */
2568 if ((edit
->search_start
>= 0) && (edit
->search_start
< edit
->last_byte
))
2571 GString
*tmp_str
, *repl_str
;
2573 edit
->found_start
= edit
->search_start
;
2574 i
= edit
->found_len
= len
;
2576 edit_cursor_move (edit
, edit
->search_start
- edit
->curs1
);
2577 edit_scroll_screen_over_cursor (edit
);
2579 if (edit
->replace_mode
== 0)
2584 l
= edit
->curs_row
- edit
->widget
.lines
/ 3;
2586 edit_scroll_downward (edit
, l
);
2588 edit_scroll_upward (edit
, -l
);
2590 edit_scroll_screen_over_cursor (edit
);
2591 edit
->force
|= REDRAW_PAGE
;
2592 edit_render_keypress (edit
);
2594 /*so that undo stops at each query */
2595 edit_push_key_press (edit
);
2596 /* and prompt 2/3 down */
2597 disp1
= edit_replace_cmd__conv_to_display (saved1
);
2598 disp2
= edit_replace_cmd__conv_to_display (saved2
);
2599 prompt
= editcmd_dialog_replace_prompt_show (edit
, disp1
, disp2
, -1, -1);
2603 if (prompt
== B_REPLACE_ALL
)
2604 edit
->replace_mode
= 1;
2605 else if (prompt
== B_SKIP_REPLACE
)
2607 if (edit_search_options
.backwards
)
2608 edit
->search_start
--;
2610 edit
->search_start
++;
2611 continue; /* loop */
2613 else if (prompt
== B_CANCEL
)
2615 edit
->replace_mode
= -1;
2620 /* don't process string each time */
2621 tmp_str
= g_string_new (input2
);
2622 repl_str
= mc_search_prepare_replace_str (edit
->search
, tmp_str
);
2623 g_string_free (tmp_str
, TRUE
);
2625 if (edit
->search
->error
!= MC_SEARCH_E_OK
)
2627 edit_error_dialog (_("Replace"), edit
->search
->error_str
);
2628 g_string_free (repl_str
, TRUE
);
2632 /* delete then insert new */
2633 for (i
= 0; i
< len
; i
++)
2634 edit_delete (edit
, 1);
2636 for (i
= 0; i
< repl_str
->len
; i
++)
2637 edit_insert (edit
, repl_str
->str
[i
]);
2639 edit
->found_len
= repl_str
->len
;
2640 g_string_free (repl_str
, TRUE
);
2643 /* so that we don't find the same string again */
2644 if (edit_search_options
.backwards
)
2646 edit
->search_start
--;
2650 edit
->search_start
+= edit
->found_len
+ (len
== 0 ? 1 : 0);
2652 if (edit
->search_start
>= edit
->last_byte
)
2656 edit_scroll_screen_over_cursor (edit
);
2660 /* try and find from right here for next search */
2661 edit
->search_start
= edit
->curs1
;
2662 edit_update_curs_col (edit
);
2664 edit
->force
|= REDRAW_PAGE
;
2665 edit_render_keypress (edit
);
2667 if (times_replaced
== 0)
2668 query_dialog (_("Replace"), _("Search string not found"), D_NORMAL
, 1, _("&OK"));
2672 while (edit
->replace_mode
>= 0);
2674 edit_scroll_screen_over_cursor (edit
);
2675 edit
->force
|= REDRAW_COMPLETELY
;
2676 edit_render_keypress (edit
);
2678 if ((edit
->replace_mode
== 1) && (times_replaced
!= 0))
2679 message (D_NORMAL
, _("Replace"), _("%ld replacements made"), times_replaced
);
2686 /* --------------------------------------------------------------------------------------------- */
2689 edit_search_cmd_callback (const void *user_data
, gsize char_offset
)
2691 return edit_get_byte ((WEdit
*) user_data
, (long) char_offset
);
2694 /* --------------------------------------------------------------------------------------------- */
2697 edit_search_cmd (WEdit
* edit
, gboolean again
)
2705 else if (edit
->last_search_string
!= NULL
)
2706 edit_do_search (edit
);
2709 /* find last search string in history */
2712 history
= history_get (MC_HISTORY_SHARED_SEARCH
);
2713 if (history
!= NULL
&& history
->data
!= NULL
)
2715 edit
->last_search_string
= (char *) history
->data
;
2716 history
->data
= NULL
;
2717 history
= g_list_first (history
);
2718 g_list_foreach (history
, (GFunc
) g_free
, NULL
);
2719 g_list_free (history
);
2721 edit
->search
= mc_search_new (edit
->last_search_string
, -1);
2722 if (edit
->search
== NULL
)
2724 /* if not... then ask for an expression */
2725 g_free (edit
->last_search_string
);
2726 edit
->last_search_string
= NULL
;
2731 edit
->search
->search_type
= edit_search_options
.type
;
2732 edit
->search
->is_all_charsets
= edit_search_options
.all_codepages
;
2733 edit
->search
->is_case_sensitive
= edit_search_options
.case_sens
;
2734 edit
->search
->whole_words
= edit_search_options
.whole_words
;
2735 edit
->search
->search_fn
= edit_search_cmd_callback
;
2736 edit
->search_line_type
= edit_get_search_line_type (edit
->search
);
2737 edit_do_search (edit
);
2742 /* if not... then ask for an expression */
2743 g_free (edit
->last_search_string
);
2744 edit
->last_search_string
= NULL
;
2751 /* --------------------------------------------------------------------------------------------- */
2753 * Check if it's OK to close the editor. If there are unsaved changes,
2754 * ask user. Return 1 if it's OK to exit, 0 to continue editing.
2758 edit_ok_to_exit (WEdit
* edit
)
2762 if (!edit
->modified
)
2765 if (!mc_global
.midnight_shutdown
)
2767 if (!edit_check_newline (edit
))
2771 act
= edit_query_dialog3 (_("Quit"), _("File was modified. Save with exit?"),
2772 _("&Yes"), _("&No"), _("&Cancel quit"));
2777 edit_query_dialog2 (_("Quit"),
2778 _("Midnight Commander is being shut down.\nSave modified file?"),
2779 _("&Yes"), _("&No"));
2789 edit_push_markers (edit
);
2790 edit_set_markers (edit
, 0, 0, 0, 0);
2791 if (!edit_save_cmd (edit
) || mc_global
.midnight_shutdown
)
2792 return mc_global
.midnight_shutdown
;
2796 case 2: /* Cancel quit */
2804 /* --------------------------------------------------------------------------------------------- */
2805 /** save block, returns 1 on success */
2808 edit_save_block (WEdit
* edit
, const char *filename
, long start
, long finish
)
2813 vpath
= vfs_path_from_str (filename
);
2814 file
= mc_open (vpath
, O_CREAT
| O_WRONLY
| O_TRUNC
,
2815 S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IROTH
| O_BINARY
);
2816 vfs_path_free (vpath
);
2820 if (edit
->column_highlight
)
2824 r
= mc_write (file
, VERTICAL_MAGIC
, sizeof (VERTICAL_MAGIC
));
2827 unsigned char *block
, *p
;
2829 p
= block
= edit_get_block (edit
, start
, finish
, &len
);
2832 r
= mc_write (file
, p
, len
);
2846 len
= finish
- start
;
2847 buf
= g_malloc0 (TEMP_BUF_LEN
);
2848 while (start
!= finish
)
2850 end
= min (finish
, start
+ TEMP_BUF_LEN
);
2851 for (; i
< end
; i
++)
2852 buf
[i
- start
] = edit_get_byte (edit
, i
);
2853 len
-= mc_write (file
, (char *) buf
, end
- start
);
2864 /* --------------------------------------------------------------------------------------------- */
2867 edit_paste_from_history (WEdit
* edit
)
2870 edit_error_dialog (_("Error"), _("This function is not implemented"));
2873 /* --------------------------------------------------------------------------------------------- */
2876 edit_copy_to_X_buf_cmd (WEdit
* edit
)
2878 long start_mark
, end_mark
;
2879 if (eval_marks (edit
, &start_mark
, &end_mark
))
2881 if (!edit_save_block_to_clip_file (edit
, start_mark
, end_mark
))
2883 edit_error_dialog (_("Copy to clipboard"), get_sys_error (_("Unable to save to file")));
2886 /* try use external clipboard utility */
2887 mc_event_raise (MCEVENT_GROUP_CORE
, "clipboard_file_to_ext_clip", NULL
);
2892 /* --------------------------------------------------------------------------------------------- */
2895 edit_cut_to_X_buf_cmd (WEdit
* edit
)
2897 long start_mark
, end_mark
;
2898 if (eval_marks (edit
, &start_mark
, &end_mark
))
2900 if (!edit_save_block_to_clip_file (edit
, start_mark
, end_mark
))
2902 edit_error_dialog (_("Cut to clipboard"), _("Unable to save to file"));
2905 /* try use external clipboard utility */
2906 mc_event_raise (MCEVENT_GROUP_CORE
, "clipboard_file_to_ext_clip", NULL
);
2908 edit_block_delete_cmd (edit
);
2909 edit_mark_cmd (edit
, 1);
2913 /* --------------------------------------------------------------------------------------------- */
2916 edit_paste_from_X_buf_cmd (WEdit
* edit
)
2920 /* try use external clipboard utility */
2921 mc_event_raise (MCEVENT_GROUP_CORE
, "clipboard_file_from_ext_clip", NULL
);
2922 tmp
= mc_config_get_full_vpath (EDIT_CLIP_FILE
);
2923 edit_insert_file (edit
, tmp
);
2924 vfs_path_free (tmp
);
2928 /* --------------------------------------------------------------------------------------------- */
2930 * Ask user for the line and go to that line.
2931 * Negative numbers mean line from the end (i.e. -1 is the last line).
2935 edit_goto_cmd (WEdit
* edit
)
2938 static long line
= 0; /* line as typed, saved as default */
2943 g_snprintf (s
, sizeof (s
), "%ld", line
);
2944 f
= input_dialog (_("Goto line"), _("Enter line:"), MC_HISTORY_EDIT_GOTO_LINE
, line
? s
: "");
2954 l
= strtol (f
, &error
, 0);
2963 l
= edit
->total_lines
+ l
+ 2;
2964 edit_move_display (edit
, l
- edit
->widget
.lines
/ 2 - 1);
2965 edit_move_to_line (edit
, l
- 1);
2966 edit
->force
|= REDRAW_COMPLETELY
;
2971 /* --------------------------------------------------------------------------------------------- */
2972 /** Return 1 on success */
2975 edit_save_block_cmd (WEdit
* edit
)
2977 long start_mark
, end_mark
;
2980 if (eval_marks (edit
, &start_mark
, &end_mark
))
2983 tmp
= mc_config_get_full_path (EDIT_CLIP_FILE
);
2985 input_expand_dialog (_("Save block"), _("Enter file name:"),
2986 MC_HISTORY_EDIT_SAVE_BLOCK
, tmp
);
2988 edit_push_undo_action (edit
, KEY_PRESS
+ edit
->start_display
);
2998 if (edit_save_block (edit
, exp
, start_mark
, end_mark
))
3001 edit
->force
|= REDRAW_COMPLETELY
;
3007 edit_error_dialog (_("Save block"), get_sys_error (_("Cannot save file")));
3011 edit
->force
|= REDRAW_COMPLETELY
;
3016 /* --------------------------------------------------------------------------------------------- */
3017 /** returns TRUE on success */
3020 edit_insert_file_cmd (WEdit
* edit
)
3024 gboolean ret
= FALSE
;
3026 tmp
= mc_config_get_full_path (EDIT_CLIP_FILE
);
3027 exp
= input_expand_dialog (_("Insert file"), _("Enter file name:"),
3028 MC_HISTORY_EDIT_INSERT_FILE
, tmp
);
3031 edit_push_undo_action (edit
, KEY_PRESS
+ edit
->start_display
);
3033 if (exp
!= NULL
&& *exp
!= '\0')
3035 vfs_path_t
*exp_vpath
;
3037 exp_vpath
= vfs_path_from_str (exp
);
3038 ret
= (edit_insert_file (edit
, exp_vpath
) >= 0);
3039 vfs_path_free (exp_vpath
);
3042 edit_error_dialog (_("Insert file"), get_sys_error (_("Cannot insert file")));
3047 edit
->force
|= REDRAW_COMPLETELY
;
3051 /* --------------------------------------------------------------------------------------------- */
3052 /** sorts a block, returns -1 on system fail, 1 on cancel and 0 on success */
3055 edit_sort_cmd (WEdit
* edit
)
3057 static char *old
= 0;
3058 char *exp
, *tmp
, *tmp_edit_block_name
, *tmp_edit_temp_name
;
3059 long start_mark
, end_mark
;
3062 if (eval_marks (edit
, &start_mark
, &end_mark
))
3064 edit_error_dialog (_("Sort block"), _("You must first highlight a block of text"));
3068 tmp
= mc_config_get_full_path (EDIT_BLOCK_FILE
);
3069 edit_save_block (edit
, tmp
, start_mark
, end_mark
);
3072 exp
= input_dialog (_("Run sort"),
3073 _("Enter sort options (see manpage) separated by whitespace:"),
3074 MC_HISTORY_EDIT_SORT
, (old
!= NULL
) ? old
: "");
3080 tmp_edit_block_name
= mc_config_get_full_path (EDIT_BLOCK_FILE
);
3081 tmp_edit_temp_name
= mc_config_get_full_path (EDIT_TEMP_FILE
);
3083 g_strconcat (" sort ", exp
, " ", tmp_edit_block_name
,
3084 " > ", tmp_edit_temp_name
, (char *) NULL
);
3085 g_free (tmp_edit_temp_name
);
3086 g_free (tmp_edit_block_name
);
3092 if (e
== -1 || e
== 127)
3094 edit_error_dialog (_("Sort"), get_sys_error (_("Cannot execute sort command")));
3099 sprintf (q
, "%d ", e
);
3100 tmp
= g_strdup_printf (_("Sort returned non-zero: %s"), q
);
3101 edit_error_dialog (_("Sort"), tmp
);
3107 edit
->force
|= REDRAW_COMPLETELY
;
3109 if (edit_block_delete_cmd (edit
))
3113 vfs_path_t
*tmp_vpath
;
3115 tmp_vpath
= mc_config_get_full_vpath (EDIT_TEMP_FILE
);
3116 edit_insert_file (edit
, tmp_vpath
);
3117 vfs_path_free (tmp_vpath
);
3122 /* --------------------------------------------------------------------------------------------- */
3124 * Ask user for a command, execute it and paste its output back to the
3129 edit_ext_cmd (WEdit
* edit
)
3131 char *exp
, *tmp
, *tmp_edit_temp_file
;
3135 input_dialog (_("Paste output of external command"),
3136 _("Enter shell command(s):"), MC_HISTORY_EDIT_PASTE_EXTCMD
, NULL
);
3141 tmp_edit_temp_file
= mc_config_get_full_path (EDIT_TEMP_FILE
);
3142 tmp
= g_strconcat (exp
, " > ", tmp_edit_temp_file
, (char *) NULL
);
3143 g_free (tmp_edit_temp_file
);
3150 edit_error_dialog (_("External command"), get_sys_error (_("Cannot execute command")));
3154 edit
->force
|= REDRAW_COMPLETELY
;
3157 vfs_path_t
*tmp_vpath
;
3159 tmp_vpath
= mc_config_get_full_vpath (EDIT_TEMP_FILE
);
3160 edit_insert_file (edit
, tmp_vpath
);
3161 vfs_path_free (tmp_vpath
);
3166 /* --------------------------------------------------------------------------------------------- */
3167 /** if block is 1, a block must be highlighted and the shell command
3168 processes it. If block is 0 the shell command is a straight system
3169 command, that just produces some output which is to be inserted */
3172 edit_block_process_cmd (WEdit
* edit
, int macro_number
)
3175 char *macros_fname
= NULL
;
3177 fname
= g_strdup_printf ("%s.%i.sh", MC_EXTMACRO_FILE
, macro_number
);
3178 macros_fname
= g_build_filename (mc_config_get_data_path (), fname
, (char *) NULL
);
3179 user_menu (edit
, macros_fname
, 0);
3181 g_free (macros_fname
);
3182 edit
->force
|= REDRAW_COMPLETELY
;
3185 /* --------------------------------------------------------------------------------------------- */
3188 edit_mail_dialog (WEdit
* edit
)
3191 char *tmail_subject
;
3194 static char *mail_cc_last
= 0;
3195 static char *mail_subject_last
= 0;
3196 static char *mail_to_last
= 0;
3198 QuickWidget quick_widgets
[] = {
3199 /* 0 */ QUICK_BUTTON (6, 10, 9, MAIL_DLG_HEIGHT
, N_("&Cancel"), B_CANCEL
, NULL
),
3200 /* 1 */ QUICK_BUTTON (2, 10, 9, MAIL_DLG_HEIGHT
, N_("&OK"), B_ENTER
, NULL
),
3201 /* 2 */ QUICK_INPUT (3, 50, 8, MAIL_DLG_HEIGHT
, "", 44, 0, "mail-dlg-input", &tmail_cc
),
3202 /* 3 */ QUICK_LABEL (3, 50, 7, MAIL_DLG_HEIGHT
, N_("Copies to")),
3203 /* 4 */ QUICK_INPUT (3, 50, 6, MAIL_DLG_HEIGHT
, "", 44, 0, "mail-dlg-input-2",
3205 /* 5 */ QUICK_LABEL (3, 50, 5, MAIL_DLG_HEIGHT
, N_("Subject")),
3206 /* 6 */ QUICK_INPUT (3, 50, 4, MAIL_DLG_HEIGHT
, "", 44, 0, "mail-dlg-input-3", &tmail_to
),
3207 /* 7 */ QUICK_LABEL (3, 50, 3, MAIL_DLG_HEIGHT
, N_("To")),
3208 /* 8 */ QUICK_LABEL (3, 50, 2, MAIL_DLG_HEIGHT
, N_("mail -s <subject> -c <cc> <to>")),
3212 QuickDialog Quick_input
= {
3213 50, MAIL_DLG_HEIGHT
, -1, -1, N_("Mail"),
3214 "[Input Line Keys]", quick_widgets
, NULL
, FALSE
3217 quick_widgets
[2].u
.input
.text
= mail_cc_last
? mail_cc_last
: "";
3218 quick_widgets
[4].u
.input
.text
= mail_subject_last
? mail_subject_last
: "";
3219 quick_widgets
[6].u
.input
.text
= mail_to_last
? mail_to_last
: "";
3221 if (quick_dialog (&Quick_input
) != B_CANCEL
)
3223 g_free (mail_cc_last
);
3224 g_free (mail_subject_last
);
3225 g_free (mail_to_last
);
3226 mail_cc_last
= tmail_cc
;
3227 mail_subject_last
= tmail_subject
;
3228 mail_to_last
= tmail_to
;
3229 pipe_mail (edit
, mail_to_last
, mail_subject_last
, mail_cc_last
);
3234 /*******************/
3235 /* Word Completion */
3236 /*******************/
3238 /* --------------------------------------------------------------------------------------------- */
3240 * Complete current word using regular expression search
3241 * backwards beginning at the current cursor position.
3245 edit_complete_word_cmd (WEdit
* edit
)
3247 gsize i
, max_len
, word_len
= 0, num_compl
= 0;
3248 long word_start
= 0;
3249 unsigned char *bufpos
;
3251 struct selection
compl[MAX_WORD_COMPLETIONS
]; /* completions */
3253 /* search start of word to be completed */
3254 if (!edit_find_word_start (edit
, &word_start
, &word_len
))
3257 /* prepare match expression */
3258 bufpos
= &edit
->buffers1
[word_start
>> S_EDIT_BUF_SIZE
][word_start
& M_EDIT_BUF_SIZE
];
3260 /* match_expr = g_strdup_printf ("\\b%.*s[a-zA-Z_0-9]+", word_len, bufpos); */
3263 ("(^|\\s+|\\b)%.*s[^\\s\\.=\\+\\[\\]\\(\\)\\,\\;\\:\\\"\\'\\-\\?\\/\\|\\\\\\{\\}\\*\\&\\^\\%%\\$#@\\!]+",
3264 (int) word_len
, bufpos
);
3266 /* collect the possible completions */
3267 /* start search from begin to end of file */
3269 edit_collect_completions (edit
, word_start
, word_len
, match_expr
,
3270 (struct selection
*) &compl, &num_compl
);
3274 /* insert completed word if there is only one match */
3277 for (i
= word_len
; i
< compl[0].len
; i
++)
3278 edit_insert (edit
, *(compl[0].text
+ i
));
3280 /* more than one possible completion => ask the user */
3283 /* !!! usually only a beep is expected and when <ALT-TAB> is !!! */
3284 /* !!! pressed again the selection dialog pops up, but that !!! */
3285 /* !!! seems to require a further internal state !!! */
3288 /* let the user select the preferred completion */
3289 editcmd_dialog_completion_show (edit
, max_len
, word_len
,
3290 (struct selection
*) &compl, num_compl
);
3294 g_free (match_expr
);
3295 /* release memory before return */
3296 for (i
= 0; i
< num_compl
; i
++)
3297 g_free (compl[i
].text
);
3300 /* --------------------------------------------------------------------------------------------- */
3303 edit_select_codepage_cmd (WEdit
* edit
)
3306 if (do_select_codepage ())
3307 edit_set_codeset (edit
);
3309 edit
->force
= REDRAW_COMPLETELY
;
3310 edit_refresh_cmd (edit
);
3316 /* --------------------------------------------------------------------------------------------- */
3319 edit_insert_literal_cmd (WEdit
* edit
)
3321 int char_for_insertion
= editcmd_dialog_raw_key_query (_("Insert literal"),
3322 _("Press any key:"), 0);
3323 edit_execute_key_command (edit
, -1, ascii_alpha_to_cntrl (char_for_insertion
));
3326 /* --------------------------------------------------------------------------------------------- */
3329 edit_begin_end_macro_cmd (WEdit
* edit
)
3331 /* edit is a pointer to the widget */
3334 unsigned long command
= macro_index
< 0 ? CK_MacroStartRecord
: CK_MacroStopRecord
;
3335 edit_execute_key_command (edit
, command
, -1);
3339 /* --------------------------------------------------------------------------------------------- */
3342 edit_begin_end_repeat_cmd (WEdit
* edit
)
3344 /* edit is a pointer to the widget */
3347 unsigned long command
= macro_index
< 0 ? CK_RepeatStartRecord
: CK_RepeatStopRecord
;
3348 edit_execute_key_command (edit
, command
, -1);
3352 /* --------------------------------------------------------------------------------------------- */
3355 edit_load_forward_cmd (WEdit
* edit
)
3359 if (edit_query_dialog2
3361 _("Current text was modified without a file save\n"
3362 "Continue discards these changes"), _("C&ontinue"), _("&Cancel")))
3364 edit
->force
|= REDRAW_COMPLETELY
;
3368 if (edit_stack_iterator
+ 1 < MAX_HISTORY_MOVETO
)
3370 if (edit_history_moveto
[edit_stack_iterator
+ 1].line
< 1)
3374 edit_stack_iterator
++;
3375 if (edit_history_moveto
[edit_stack_iterator
].filename_vpath
)
3377 edit_reload_line (edit
, edit_history_moveto
[edit_stack_iterator
].filename_vpath
,
3378 edit_history_moveto
[edit_stack_iterator
].line
);
3392 /* --------------------------------------------------------------------------------------------- */
3395 edit_load_back_cmd (WEdit
* edit
)
3399 if (edit_query_dialog2
3401 _("Current text was modified without a file save\n"
3402 "Continue discards these changes"), _("C&ontinue"), _("&Cancel")))
3404 edit
->force
|= REDRAW_COMPLETELY
;
3408 if (edit_stack_iterator
> 0)
3410 edit_stack_iterator
--;
3411 if (edit_history_moveto
[edit_stack_iterator
].filename_vpath
)
3413 edit_reload_line (edit
, edit_history_moveto
[edit_stack_iterator
].filename_vpath
,
3414 edit_history_moveto
[edit_stack_iterator
].line
);
3428 /* --------------------------------------------------------------------------------------------- */
3431 edit_get_match_keyword_cmd (WEdit
* edit
)
3433 gsize word_len
= 0, max_len
= 0;
3436 long word_start
= 0;
3437 unsigned char *bufpos
;
3441 char *tagfile
= NULL
;
3443 etags_hash_t def_hash
[MAX_DEFINITIONS
];
3445 for (i
= 0; i
< MAX_DEFINITIONS
; i
++)
3447 def_hash
[i
].filename
= NULL
;
3450 /* search start of word to be completed */
3451 if (!edit_find_word_start (edit
, &word_start
, &word_len
))
3454 /* prepare match expression */
3455 bufpos
= &edit
->buffers1
[word_start
>> S_EDIT_BUF_SIZE
][word_start
& M_EDIT_BUF_SIZE
];
3456 match_expr
= g_strdup_printf ("%.*s", (int) word_len
, bufpos
);
3458 ptr
= g_get_current_dir ();
3459 path
= g_strconcat (ptr
, G_DIR_SEPARATOR_S
, (char *) NULL
);
3462 /* Recursive search file 'TAGS' in parent dirs */
3465 ptr
= g_path_get_dirname (path
);
3469 tagfile
= mc_build_filename (path
, TAGS_NAME
, (char *) NULL
);
3470 if (exist_file (tagfile
))
3473 while (strcmp (path
, G_DIR_SEPARATOR_S
) != 0);
3478 etags_set_definition_hash (tagfile
, path
, match_expr
, (etags_hash_t
*) & def_hash
);
3483 max_len
= MAX_WIDTH_DEF_DIALOG
;
3487 editcmd_dialog_select_definition_show (edit
, match_expr
, max_len
, word_len
,
3488 (etags_hash_t
*) & def_hash
, num_def
);
3490 g_free (match_expr
);
3493 /* --------------------------------------------------------------------------------------------- */