2 Editor high level editing commands
4 Copyright (C) 1996, 1997, 1998, 2001, 2002, 2003, 2004, 2005, 2006,
6 The Free Software Foundation, Inc.
10 Andrew Borodin <aborodin@vmail.ru> 2012
12 This file is part of the Midnight Commander.
14 The Midnight Commander is free software: you can redistribute it
15 and/or modify it under the terms of the GNU General Public License as
16 published by the Free Software Foundation, either version 3 of the License,
17 or (at your option) any later version.
19 The Midnight Commander is distributed in the hope that it will be useful,
20 but WITHOUT ANY WARRANTY; without even the implied warranty of
21 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 GNU General Public License for more details.
24 You should have received a copy of the GNU General Public License
25 along with this program. If not, see <http://www.gnu.org/licenses/>.
29 * \brief Source: editor high level editing commands
34 /* #define PIPE_BLOCKS_SO_READ_BYTE_BY_BYTE */
45 #include <sys/types.h>
53 #include "lib/global.h"
54 #include "lib/tty/tty.h"
55 #include "lib/tty/key.h" /* XCTRL */
56 #include "lib/mcconfig.h"
58 #include "lib/strutil.h" /* utf string functions */
60 #include "lib/util.h" /* tilde_expand() */
61 #include "lib/vfs/vfs.h"
62 #include "lib/widget.h"
63 #include "lib/event.h" /* mc_event_raise() */
65 #include "lib/charsets.h"
68 #include "src/history.h"
69 #include "src/setup.h" /* option_tab_spacing */
70 #include "src/main.h" /* mactos_t */
72 #include "src/selcodepage.h"
74 #include "src/keybind-defaults.h"
75 #include "src/util.h" /* check_for_default() */
76 #include "src/filemanager/layout.h" /* mc_refresh() */
78 #include "edit-impl.h"
79 #include "editwidget.h"
80 #include "editcmd_dialogs.h"
83 /*** global variables ****************************************************************************/
85 /* search and replace: */
86 int search_create_bookmark
= FALSE
;
88 /* queries on a save */
89 int edit_confirm_save
= 1;
91 /*** file scope macro definitions ****************************************************************/
95 #define TEMP_BUF_LEN 1024
99 /* thanks to Liviu Daia <daia@stoilow.imar.ro> for getting this
100 (and the above) routines to work properly - paul */
102 #define is_digit(x) ((x) >= '0' && (x) <= '9')
104 #define MAIL_DLG_HEIGHT 12
106 #define MAX_WORD_COMPLETIONS 100 /* in listbox */
108 /*** file scope type declarations ****************************************************************/
110 /*** file scope variables ************************************************************************/
112 /*** file scope functions ************************************************************************/
113 /* --------------------------------------------------------------------------------------------- */
115 /* If 0 (quick save) then a) create/truncate <filename> file,
116 b) save to <filename>;
117 if 1 (safe save) then a) save to <tempnam>,
118 b) rename <tempnam> to <filename>;
119 if 2 (do backups) then a) save to <tempnam>,
120 b) rename <filename> to <filename.backup_ext>,
121 c) rename <tempnam> to <filename>. */
123 /* returns 0 on error, -1 on abort */
126 edit_save_file (WEdit
* edit
, const vfs_path_t
* filename_vpath
)
131 int this_save_mode
, fd
= -1;
132 vfs_path_t
*real_filename_vpath
;
133 vfs_path_t
*savename_vpath
= NULL
;
134 const char *start_filename
;
135 const vfs_path_element_t
*vpath_element
;
137 vpath_element
= vfs_path_get_by_index (filename_vpath
, 0);
138 if (vpath_element
== NULL
)
141 start_filename
= vpath_element
->path
;
142 if (*start_filename
== '\0')
145 if (*start_filename
!= PATH_SEP
&& edit
->dir_vpath
!= NULL
)
147 real_filename_vpath
= vfs_path_append_vpath_new (edit
->dir_vpath
, filename_vpath
, NULL
);
151 real_filename_vpath
= vfs_path_clone (filename_vpath
);
154 this_save_mode
= option_save_mode
;
155 if (this_save_mode
!= EDIT_QUICK_SAVE
)
157 if (!vfs_file_is_local (real_filename_vpath
)
158 || (fd
= mc_open (real_filename_vpath
, O_RDONLY
| O_BINARY
)) == -1)
161 * The file does not exists yet, so no safe save or
162 * backup are necessary.
164 this_save_mode
= EDIT_QUICK_SAVE
;
170 if (this_save_mode
== EDIT_QUICK_SAVE
&& !edit
->skip_detach_prompt
)
175 rv
= mc_stat (real_filename_vpath
, &sb
);
176 if (rv
== 0 && sb
.st_nlink
> 1)
178 rv
= edit_query_dialog3 (_("Warning"),
179 _("File has hard-links. Detach before saving?"),
180 _("&Yes"), _("&No"), _("&Cancel"));
184 this_save_mode
= EDIT_SAFE_SAVE
;
187 edit
->skip_detach_prompt
= 1;
190 vfs_path_free (real_filename_vpath
);
195 /* Prevent overwriting changes from other editor sessions. */
196 if (rv
== 0 && edit
->stat1
.st_mtime
!= 0 && edit
->stat1
.st_mtime
!= sb
.st_mtime
)
198 /* The default action is "Cancel". */
201 rv
= edit_query_dialog2 (_("Warning"),
202 _("The file has been modified in the meantime. Save anyway?"),
203 _("&Yes"), _("&Cancel"));
206 vfs_path_free (real_filename_vpath
);
212 if (this_save_mode
!= EDIT_QUICK_SAVE
)
214 char *savedir
, *saveprefix
;
216 savedir
= vfs_path_tokens_get (real_filename_vpath
, 0, -1);
218 savedir
= g_strdup (".");
220 /* Token-related function never return leading slash, so we need add it manually */
221 saveprefix
= mc_build_filename ("/", savedir
, "cooledit", NULL
);
223 fd
= mc_mkstemps (&savename_vpath
, saveprefix
, NULL
);
225 if (savename_vpath
== NULL
)
227 vfs_path_free (real_filename_vpath
);
231 * Close for now because mc_mkstemps use pure open system call
232 * to create temporary file and it needs to be reopened by
233 * VFS-aware mc_open().
238 savename_vpath
= vfs_path_clone (real_filename_vpath
);
240 (void) mc_chown (savename_vpath
, edit
->stat1
.st_uid
, edit
->stat1
.st_gid
);
241 (void) mc_chmod (savename_vpath
, edit
->stat1
.st_mode
);
243 fd
= mc_open (savename_vpath
, O_CREAT
| O_WRONLY
| O_TRUNC
| O_BINARY
, edit
->stat1
.st_mode
);
248 p
= edit_get_write_filter (savename_vpath
, real_filename_vpath
);
254 file
= (FILE *) popen (p
, "w");
258 filelen
= edit_write_stream (edit
, file
);
262 if (pclose (file
) != 0)
264 tmp
= g_strdup_printf (_("Error writing to pipe: %s"), p
);
265 edit_error_dialog (_("Error"), tmp
);
274 tmp
= g_strdup_printf (_("Cannot open pipe for writing: %s"), p
);
275 edit_error_dialog (_("Error"), get_sys_error (tmp
));
282 else if (edit
->lb
== LB_ASIS
)
283 { /* do not change line breaks */
286 filelen
= edit
->last_byte
;
287 while (buf
<= (edit
->curs1
>> S_EDIT_BUF_SIZE
) - 1)
289 if (mc_write (fd
, (char *) edit
->buffers1
[buf
], EDIT_BUF_SIZE
) != EDIT_BUF_SIZE
)
297 (fd
, (char *) edit
->buffers1
[buf
],
298 edit
->curs1
& M_EDIT_BUF_SIZE
) != (edit
->curs1
& M_EDIT_BUF_SIZE
))
302 else if (edit
->curs2
)
305 buf
= (edit
->curs2
>> S_EDIT_BUF_SIZE
);
308 (char *) edit
->buffers2
[buf
] + EDIT_BUF_SIZE
-
309 (edit
->curs2
& M_EDIT_BUF_SIZE
) - 1,
310 1 + (edit
->curs2
& M_EDIT_BUF_SIZE
)) != 1 + (edit
->curs2
& M_EDIT_BUF_SIZE
))
318 if (mc_write (fd
, (char *) edit
->buffers2
[buf
], EDIT_BUF_SIZE
) != EDIT_BUF_SIZE
)
330 /* Update the file information, especially the mtime. */
331 if (mc_stat (savename_vpath
, &edit
->stat1
) == -1)
335 { /* change line breaks */
337 const vfs_path_element_t
*path_element
;
341 path_element
= vfs_path_get_by_index (savename_vpath
, -1);
342 file
= (FILE *) fopen (path_element
->path
, "w");
345 filelen
= edit_write_stream (edit
, file
);
352 msg
= g_strdup_printf (_("Cannot open file for writing: %s"), path_element
->path
);
353 edit_error_dialog (_("Error"), msg
);
359 if (filelen
!= edit
->last_byte
)
362 if (this_save_mode
== EDIT_DO_BACKUP
)
364 char *tmp_store_filename
;
365 vfs_path_element_t
*last_vpath_element
;
366 vfs_path_t
*tmp_vpath
;
370 assert (option_backup_ext
!= NULL
);
372 /* add backup extention to the path */
373 tmp_vpath
= vfs_path_clone (real_filename_vpath
);
374 last_vpath_element
= (vfs_path_element_t
*) vfs_path_get_by_index (tmp_vpath
, -1);
375 tmp_store_filename
= last_vpath_element
->path
;
376 last_vpath_element
->path
= g_strdup_printf ("%s%s", tmp_store_filename
, option_backup_ext
);
377 g_free (tmp_store_filename
);
379 ok
= (mc_rename (real_filename_vpath
, tmp_vpath
) != -1);
380 vfs_path_free (tmp_vpath
);
385 if (this_save_mode
!= EDIT_QUICK_SAVE
)
386 if (mc_rename (savename_vpath
, real_filename_vpath
) == -1)
389 vfs_path_free (real_filename_vpath
);
390 vfs_path_free (savename_vpath
);
393 /* FIXME: Is this safe ?
394 * if (this_save_mode != EDIT_QUICK_SAVE)
395 * mc_unlink (savename);
397 vfs_path_free (real_filename_vpath
);
398 vfs_path_free (savename_vpath
);
402 /* --------------------------------------------------------------------------------------------- */
405 edit_check_newline (WEdit
* edit
)
407 return !(option_check_nl_at_eof
&& edit
->last_byte
> 0
408 && edit_get_byte (edit
, edit
->last_byte
- 1) != '\n'
409 && edit_query_dialog2 (_("Warning"),
410 _("The file you are saving is not finished with a newline"),
411 _("C&ontinue"), _("&Cancel")));
414 /* --------------------------------------------------------------------------------------------- */
417 edit_get_save_file_as (WEdit
* edit
)
420 #define DLG_HEIGHT 14
422 static LineBreaks cur_lb
= LB_ASIS
;
424 char *filename
= vfs_path_to_str (edit
->filename_vpath
);
427 const char *lb_names
[LB_NAMES
] = {
428 N_("&Do not change"),
429 N_("&Unix format (LF)"),
430 N_("&Windows/DOS format (CR LF)"),
431 N_("&Macintosh format (CR)")
434 QuickWidget quick_widgets
[] = {
435 QUICK_BUTTON (6, 10, DLG_HEIGHT
- 3, DLG_HEIGHT
, N_("&Cancel"), B_CANCEL
, NULL
),
436 QUICK_BUTTON (2, 10, DLG_HEIGHT
- 3, DLG_HEIGHT
, N_("&OK"), B_ENTER
, NULL
),
437 QUICK_RADIO (5, DLG_WIDTH
, DLG_HEIGHT
- 8, DLG_HEIGHT
, LB_NAMES
, lb_names
, (int *) &cur_lb
),
438 QUICK_LABEL (3, DLG_WIDTH
, DLG_HEIGHT
- 9, DLG_HEIGHT
, N_("Change line breaks to:")),
439 QUICK_INPUT (3, DLG_WIDTH
, DLG_HEIGHT
- 11, DLG_HEIGHT
, filename
, DLG_WIDTH
- 6, 0,
440 "save-as", &filename_res
),
441 QUICK_LABEL (3, DLG_WIDTH
, DLG_HEIGHT
- 12, DLG_HEIGHT
, N_("Enter file name:")),
445 QuickDialog Quick_options
= {
446 DLG_WIDTH
, DLG_HEIGHT
, -1, -1,
447 N_("Save As"), "[Save File As]",
448 quick_widgets
, NULL
, FALSE
451 if (quick_dialog (&Quick_options
) != B_CANCEL
)
454 vfs_path_t
*ret_vpath
;
457 fname
= tilde_expand (filename_res
);
458 g_free (filename_res
);
459 ret_vpath
= vfs_path_from_str (fname
);
471 /* --------------------------------------------------------------------------------------------- */
473 /** returns TRUE on success */
476 edit_save_cmd (WEdit
* edit
)
478 int res
, save_lock
= 0;
480 if (!edit
->locked
&& !edit
->delete_file
)
481 save_lock
= lock_file (edit
->filename_vpath
);
482 res
= edit_save_file (edit
, edit
->filename_vpath
);
484 /* Maintain modify (not save) lock on failure */
485 if ((res
> 0 && edit
->locked
) || save_lock
)
486 edit
->locked
= unlock_file (edit
->filename_vpath
);
488 /* On failure try 'save as', it does locking on its own */
490 return edit_save_as_cmd (edit
);
491 edit
->force
|= REDRAW_COMPLETELY
;
494 edit
->delete_file
= 0;
501 /* --------------------------------------------------------------------------------------------- */
505 * @param edit widget object
506 * @param exp_vpath vfs file path
507 * @return TRUE if file content was successfully loaded, FALSE otherwise
511 edit_load_file_from_filename (WEdit
* edit
, const vfs_path_t
* exp_vpath
)
513 int prev_locked
= edit
->locked
;
514 vfs_path_t
*prev_filename
;
517 prev_filename
= vfs_path_clone (edit
->filename_vpath
);
518 if (!edit_reload (edit
, exp_vpath
))
520 else if (prev_locked
)
521 unlock_file (prev_filename
);
523 vfs_path_free (prev_filename
);
527 /* --------------------------------------------------------------------------------------------- */
530 edit_load_syntax_file (WEdit
* edit
)
532 vfs_path_t
*extdir_vpath
;
537 dir
= query_dialog (_("Syntax file edit"),
538 _("Which syntax file you want to edit?"), D_NORMAL
, 2,
539 _("&User"), _("&System Wide"));
543 vfs_path_build_filename (mc_global
.sysconfig_dir
, "syntax", "Syntax", (char *) NULL
);
544 if (!exist_file (vfs_path_get_last_path_str (extdir_vpath
)))
546 vfs_path_free (extdir_vpath
);
548 vfs_path_build_filename (mc_global
.share_data_dir
, "syntax", "Syntax", (char *) NULL
);
553 vfs_path_t
*user_syntax_file_vpath
;
555 user_syntax_file_vpath
= mc_config_get_full_vpath (EDIT_SYNTAX_FILE
);
556 check_for_default (extdir_vpath
, user_syntax_file_vpath
);
557 edit_load_file_from_filename (edit
, user_syntax_file_vpath
);
558 vfs_path_free (user_syntax_file_vpath
);
561 edit_load_file_from_filename (edit
, extdir_vpath
);
563 vfs_path_free (extdir_vpath
);
566 /* --------------------------------------------------------------------------------------------- */
569 edit_load_menu_file (WEdit
* edit
)
571 vfs_path_t
*buffer_vpath
;
572 vfs_path_t
*menufile_vpath
;
575 dir
= query_dialog (_("Menu edit"),
576 _("Which menu file do you want to edit?"), D_NORMAL
,
577 geteuid () != 0 ? 2 : 3, _("&Local"), _("&User"), _("&System Wide"));
579 menufile_vpath
= vfs_path_build_filename (mc_global
.sysconfig_dir
, EDIT_GLOBAL_MENU
, NULL
);
581 if (!exist_file (vfs_path_get_last_path_str (menufile_vpath
)))
583 vfs_path_free (menufile_vpath
);
584 menufile_vpath
= vfs_path_build_filename (mc_global
.share_data_dir
, EDIT_GLOBAL_MENU
, NULL
);
590 buffer_vpath
= vfs_path_from_str (EDIT_LOCAL_MENU
);
591 check_for_default (menufile_vpath
, buffer_vpath
);
592 chmod (vfs_path_get_last_path_str (buffer_vpath
), 0600);
596 buffer_vpath
= mc_config_get_full_vpath (EDIT_HOME_MENU
);
597 check_for_default (menufile_vpath
, buffer_vpath
);
601 buffer_vpath
= vfs_path_build_filename (mc_global
.sysconfig_dir
, EDIT_GLOBAL_MENU
, NULL
);
602 if (!exist_file (vfs_path_get_last_path_str (buffer_vpath
)))
604 vfs_path_free (buffer_vpath
);
606 vfs_path_build_filename (mc_global
.share_data_dir
, EDIT_GLOBAL_MENU
, NULL
);
611 vfs_path_free (menufile_vpath
);
615 edit_load_file_from_filename (edit
, buffer_vpath
);
617 vfs_path_free (buffer_vpath
);
618 vfs_path_free (menufile_vpath
);
621 /* --------------------------------------------------------------------------------------------- */
624 edit_delete_column_of_text (WEdit
* edit
)
631 eval_marks (edit
, &m1
, &m2
);
632 n
= edit_move_forward (edit
, m1
, 0, m2
) + 1;
633 c
= (long) edit_move_forward3 (edit
, edit_bol (edit
, m1
), 0, m1
);
634 d
= (long) edit_move_forward3 (edit
, edit_bol (edit
, m2
), 0, m2
);
635 b
= max (min (c
, d
), min (edit
->column1
, edit
->column2
));
636 c
= max (c
, max (edit
->column1
, edit
->column2
));
640 r
= edit_bol (edit
, edit
->curs1
);
641 p
= edit_move_forward3 (edit
, r
, b
, 0);
642 q
= edit_move_forward3 (edit
, r
, c
, 0);
647 edit_cursor_move (edit
, p
- edit
->curs1
);
650 /* delete line between margins */
651 if (edit_get_byte (edit
, edit
->curs1
) != '\n')
652 edit_delete (edit
, 1);
656 /* move to next line except on the last delete */
657 edit_cursor_move (edit
, edit_move_forward (edit
, edit
->curs1
, 1, 0) - edit
->curs1
);
661 /* --------------------------------------------------------------------------------------------- */
662 /** if success return 0 */
665 edit_block_delete (WEdit
* edit
)
668 off_t start_mark
, end_mark
;
670 long curs_line
, c1
, c2
;
672 if (eval_marks (edit
, &start_mark
, &end_mark
))
674 if (edit
->column_highlight
&& edit
->mark2
< 0)
675 edit_mark_cmd (edit
, FALSE
);
676 if ((end_mark
- start_mark
) > option_max_undo
/ 2)
678 /* Warning message with a query to continue or cancel the operation */
679 if (edit_query_dialog2
682 ("Block is large, you may not be able to undo this action"),
683 _("C&ontinue"), _("&Cancel")))
688 c1
= min (edit
->column1
, edit
->column2
);
689 c2
= max (edit
->column1
, edit
->column2
);
693 edit_push_markers (edit
);
695 curs_line
= edit
->curs_line
;
697 curs_pos
= edit
->curs_col
+ edit
->over_col
;
699 /* move cursor to start of selection */
700 edit_cursor_move (edit
, start_mark
- edit
->curs1
);
701 edit_scroll_screen_over_cursor (edit
);
703 if (start_mark
< end_mark
)
705 if (edit
->column_highlight
)
710 edit_mark_cmd (edit
, FALSE
);
711 edit_delete_column_of_text (edit
);
712 /* move cursor to the saved position */
713 edit_move_to_line (edit
, curs_line
);
714 /* calculate line width and cursor position before cut */
715 line_width
= (long) edit_move_forward3 (edit
, edit_bol (edit
, edit
->curs1
), 0,
716 edit_eol (edit
, edit
->curs1
));
717 if (option_cursor_beyond_eol
&& curs_pos
> line_width
)
718 edit
->over_col
= curs_pos
- line_width
;
722 while (count
< end_mark
)
724 edit_delete (edit
, 1);
729 edit_set_markers (edit
, 0, 0, 0, 0);
730 edit
->force
|= REDRAW_PAGE
;
734 /* --------------------------------------------------------------------------------------------- */
736 * Get EOL symbol for searching.
738 * @param edit editor object
743 edit_search_get_current_end_line_char (const WEdit
* edit
)
754 /* --------------------------------------------------------------------------------------------- */
756 * Checking if search condition have BOL(^) or EOL ($) regexp special characters.
758 * @param search search object
759 * @return result of checks.
762 static edit_search_line_t
763 edit_get_search_line_type (mc_search_t
* search
)
765 edit_search_line_t search_line_type
= 0;
767 if (search
->search_type
!= MC_SEARCH_T_REGEX
)
768 return search_line_type
;
770 if (*search
->original
== '^')
771 search_line_type
|= AT_START_LINE
;
773 if (search
->original
[search
->original_len
- 1] == '$')
774 search_line_type
|= AT_END_LINE
;
775 return search_line_type
;
778 /* --------------------------------------------------------------------------------------------- */
780 * Calculating the start position of next line.
782 * @param edit editor object
783 * @param current_pos current position
784 * @param max_pos max position
785 * @param end_string_symbol end of line symbol
786 * @return start position of next line
790 edit_calculate_start_of_next_line (WEdit
* edit
, off_t current_pos
, off_t max_pos
,
791 char end_string_symbol
)
795 for (i
= current_pos
; i
< max_pos
; i
++)
798 if (edit_get_byte (edit
, i
) == end_string_symbol
)
805 /* --------------------------------------------------------------------------------------------- */
807 * Calculating the end position of previous line.
809 * @param edit editor object
810 * @param current_pos current position
811 * @param end_string_symbol end of line symbol
812 * @return end position of previous line
816 edit_calculate_end_of_previous_line (WEdit
* edit
, off_t current_pos
, char end_string_symbol
)
820 for (i
= current_pos
- 1; i
>= 0; i
--)
821 if (edit_get_byte (edit
, i
) == end_string_symbol
)
827 /* --------------------------------------------------------------------------------------------- */
829 * Calculating the start position of previous line.
831 * @param edit editor object
832 * @param current_pos current position
833 * @param end_string_symbol end of line symbol
834 * @return start position of previous line
838 edit_calculate_start_of_previous_line (WEdit
* edit
, off_t current_pos
, char end_string_symbol
)
840 current_pos
= edit_calculate_end_of_previous_line (edit
, current_pos
, end_string_symbol
);
841 current_pos
= edit_calculate_end_of_previous_line (edit
, current_pos
, end_string_symbol
);
843 return (current_pos
+ 1);
846 /* --------------------------------------------------------------------------------------------- */
848 * Calculating the start position of current line.
850 * @param edit editor object
851 * @param current_pos current position
852 * @param end_string_symbol end of line symbol
853 * @return start position of current line
857 edit_calculate_start_of_current_line (WEdit
* edit
, off_t current_pos
, char end_string_symbol
)
859 current_pos
= edit_calculate_end_of_previous_line (edit
, current_pos
, end_string_symbol
);
861 return (current_pos
+ 1);
864 /* --------------------------------------------------------------------------------------------- */
866 * Fixing (if needed) search start position if 'only in selection' option present.
868 * @param edit editor object
872 edit_search_fix_search_start_if_selection (WEdit
* edit
)
874 off_t start_mark
= 0;
877 if (!edit_search_options
.only_in_selection
)
880 if (eval_marks (edit
, &start_mark
, &end_mark
) != 0)
883 if (edit_search_options
.backwards
)
885 if (edit
->search_start
> end_mark
|| edit
->search_start
<= start_mark
)
886 edit
->search_start
= end_mark
;
890 if (edit
->search_start
< start_mark
|| edit
->search_start
>= end_mark
)
891 edit
->search_start
= start_mark
;
895 /* --------------------------------------------------------------------------------------------- */
898 editcmd_find (WEdit
* edit
, gsize
* len
)
900 off_t search_start
= edit
->search_start
;
902 off_t start_mark
= 0;
903 off_t end_mark
= edit
->last_byte
;
905 char end_string_symbol
;
907 end_string_symbol
= edit_search_get_current_end_line_char (edit
);
909 /* prepare for search */
910 if (edit_search_options
.only_in_selection
)
912 mark_res
= eval_marks (edit
, &start_mark
, &end_mark
);
915 edit
->search
->error
= MC_SEARCH_E_NOTFOUND
;
916 edit
->search
->error_str
= g_strdup (_("Search string not found"));
920 /* fix the start and the end of search block positions */
921 if ((edit
->search_line_type
& AT_START_LINE
) != 0
922 && (start_mark
!= 0 || edit_get_byte (edit
, start_mark
- 1) != end_string_symbol
))
925 edit_calculate_start_of_next_line (edit
, start_mark
, edit
->last_byte
,
928 if ((edit
->search_line_type
& AT_END_LINE
) != 0
929 && (end_mark
- 1 != edit
->last_byte
930 || edit_get_byte (edit
, end_mark
) != end_string_symbol
))
932 end_mark
= edit_calculate_end_of_previous_line (edit
, end_mark
, end_string_symbol
);
934 if (start_mark
>= end_mark
)
936 edit
->search
->error
= MC_SEARCH_E_NOTFOUND
;
937 edit
->search
->error_str
= g_strdup (_("Search string not found"));
943 if (edit_search_options
.backwards
)
944 end_mark
= max (1, edit
->curs1
) - 1;
948 if (edit_search_options
.backwards
)
950 /* backward search */
951 search_end
= end_mark
;
953 if ((edit
->search_line_type
& AT_START_LINE
) != 0)
955 edit_calculate_start_of_current_line (edit
, search_start
, end_string_symbol
);
957 while ((int) search_start
>= start_mark
)
959 if (search_end
> (off_t
) (search_start
+ edit
->search
->original_len
)
960 && mc_search_is_fixed_search_str (edit
->search
))
962 search_end
= search_start
+ edit
->search
->original_len
;
964 if (mc_search_run (edit
->search
, (void *) edit
, search_start
, search_end
, len
)
965 && edit
->search
->normal_offset
== search_start
)
971 if ((edit
->search_line_type
& AT_START_LINE
) != 0)
973 edit_calculate_start_of_previous_line (edit
, search_start
, end_string_symbol
);
977 edit
->search
->error_str
= g_strdup (_("Search string not found"));
982 if ((edit
->search_line_type
& AT_START_LINE
) != 0 && search_start
!= start_mark
)
984 edit_calculate_start_of_next_line (edit
, search_start
, end_mark
, end_string_symbol
);
985 return mc_search_run (edit
->search
, (void *) edit
, search_start
, end_mark
, len
);
990 /* --------------------------------------------------------------------------------------------- */
993 edit_replace_cmd__conv_to_display (char *str
)
998 tmp
= str_convert_to_display (str
);
1002 return g_string_free (tmp
, FALSE
);
1003 g_string_free (tmp
, TRUE
);
1006 return g_strdup (str
);
1009 /* --------------------------------------------------------------------------------------------- */
1012 edit_replace_cmd__conv_to_input (char *str
)
1017 tmp
= str_convert_to_input (str
);
1021 return g_string_free (tmp
, FALSE
);
1022 g_string_free (tmp
, TRUE
);
1025 return g_strdup (str
);
1028 /* --------------------------------------------------------------------------------------------- */
1031 edit_do_search (WEdit
* edit
)
1035 if (edit
->search
== NULL
)
1036 edit
->search_start
= edit
->curs1
;
1038 edit_push_undo_action (edit
, KEY_PRESS
+ edit
->start_display
);
1040 if (search_create_bookmark
)
1042 int found
= 0, books
= 0;
1043 long l
= 0, l_last
= -1;
1046 search_create_bookmark
= FALSE
;
1047 book_mark_flush (edit
, -1);
1049 while (mc_search_run (edit
->search
, (void *) edit
, q
, edit
->last_byte
, &len
))
1052 edit
->search_start
= edit
->search
->normal_offset
;
1054 l
+= edit_count_lines (edit
, q
, edit
->search
->normal_offset
);
1057 book_mark_insert (edit
, l
, BOOK_MARK_FOUND_COLOR
);
1061 q
= edit
->search
->normal_offset
+ 1;
1065 edit_error_dialog (_("Search"), _("Search string not found"));
1067 edit_cursor_move (edit
, edit
->search_start
- edit
->curs1
);
1071 if (edit
->found_len
!= 0 && edit
->search_start
== edit
->found_start
+ 1
1072 && edit_search_options
.backwards
)
1073 edit
->search_start
--;
1075 if (edit
->found_len
!= 0 && edit
->search_start
== edit
->found_start
- 1
1076 && !edit_search_options
.backwards
)
1077 edit
->search_start
++;
1079 if (editcmd_find (edit
, &len
))
1081 edit
->found_start
= edit
->search_start
= edit
->search
->normal_offset
;
1082 edit
->found_len
= len
;
1084 edit_cursor_move (edit
, edit
->search_start
- edit
->curs1
);
1085 edit_scroll_screen_over_cursor (edit
);
1086 if (edit_search_options
.backwards
)
1087 edit
->search_start
--;
1089 edit
->search_start
++;
1093 edit
->search_start
= edit
->curs1
;
1094 if (edit
->search
->error_str
!= NULL
)
1095 edit_error_dialog (_("Search"), edit
->search
->error_str
);
1099 edit
->force
|= REDRAW_COMPLETELY
;
1100 edit_scroll_screen_over_cursor (edit
);
1103 /* --------------------------------------------------------------------------------------------- */
1106 edit_search (WEdit
* edit
)
1108 if (editcmd_dialog_search_show (edit
))
1110 edit
->search_line_type
= edit_get_search_line_type (edit
->search
);
1111 edit_search_fix_search_start_if_selection (edit
);
1112 edit_do_search (edit
);
1116 /* --------------------------------------------------------------------------------------------- */
1117 /** Return a null terminated length of text. Result must be g_free'd */
1119 static unsigned char *
1120 edit_get_block (WEdit
* edit
, off_t start
, off_t finish
, int *l
)
1122 unsigned char *s
, *r
;
1123 r
= s
= g_malloc0 (finish
- start
+ 1);
1124 if (edit
->column_highlight
)
1127 /* copy from buffer, excluding chars that are out of the column 'margins' */
1128 while (start
< finish
)
1133 x
= edit_move_forward3 (edit
, edit_bol (edit
, start
), 0, start
);
1134 c
= edit_get_byte (edit
, start
);
1135 if ((x
>= edit
->column1
&& x
< edit
->column2
)
1136 || (x
>= edit
->column2
&& x
< edit
->column1
) || c
== '\n')
1146 *l
= finish
- start
;
1147 while (start
< finish
)
1148 *s
++ = edit_get_byte (edit
, start
++);
1154 /* --------------------------------------------------------------------------------------------- */
1155 /** copies a block to clipboard file */
1158 edit_save_block_to_clip_file (WEdit
* edit
, off_t start
, off_t finish
)
1163 tmp
= mc_config_get_full_path (EDIT_CLIP_FILE
);
1164 ret
= edit_save_block (edit
, tmp
, start
, finish
);
1169 /* --------------------------------------------------------------------------------------------- */
1172 pipe_mail (WEdit
* edit
, char *to
, char *subject
, char *cc
)
1177 to
= name_quote (to
, 0);
1178 subject
= name_quote (subject
, 0);
1179 cc
= name_quote (cc
, 0);
1180 s
= g_strconcat ("mail -s ", subject
, *cc
? " -c " : "", cc
, " ", to
, (char *) NULL
);
1194 for (i
= 0; i
< edit
->last_byte
; i
++)
1195 fputc (edit_get_byte (edit
, i
), p
);
1200 /* --------------------------------------------------------------------------------------------- */
1203 is_break_char (char c
)
1205 return (isspace (c
) || strchr ("{}[]()<>=|/\\!?~-+`'\",.;:#$%^&*", c
));
1208 /* --------------------------------------------------------------------------------------------- */
1209 /** find first character of current word */
1212 edit_find_word_start (WEdit
* edit
, off_t
* word_start
, gsize
* word_len
)
1217 /* return if at begin of file */
1218 if (edit
->curs1
<= 0)
1221 c
= edit_get_byte (edit
, edit
->curs1
- 1);
1222 /* return if not at end or in word */
1223 if (is_break_char (c
))
1226 /* search start of word to be completed */
1229 /* return if at begin of file */
1230 if ((gsize
) edit
->curs1
< i
)
1234 c
= edit_get_byte (edit
, edit
->curs1
- i
);
1236 if (is_break_char (c
))
1238 /* return if word starts with digit */
1242 *word_start
= edit
->curs1
- (i
- 1); /* start found */
1251 /* --------------------------------------------------------------------------------------------- */
1253 * Get current word under cursor
1255 * @param edit editor object
1256 * @param srch mc_search object
1257 * @param word_start start word position
1259 * @return newly allocated string or NULL if no any words under cursor
1263 edit_collect_completions_get_current_word (WEdit
* edit
, mc_search_t
* srch
, off_t word_start
)
1269 if (!mc_search_run (srch
, (void *) edit
, word_start
, edit
->last_byte
, &len
))
1272 temp
= g_string_sized_new (len
);
1274 for (i
= 0; i
< (off_t
) len
; i
++)
1278 chr
= edit_get_byte (edit
, word_start
+ i
);
1280 g_string_append_c (temp
, chr
);
1283 return g_string_free (temp
, temp
->len
== 0);
1286 /* --------------------------------------------------------------------------------------------- */
1287 /** collect the possible completions */
1290 edit_collect_completions (WEdit
* edit
, off_t word_start
, gsize word_len
,
1291 char *match_expr
, struct selection
*compl, gsize
* num
)
1299 off_t last_byte
, start
= -1;
1302 srch
= mc_search_new (match_expr
, -1);
1306 if (mc_config_get_bool
1307 (mc_main_config
, CONFIG_APP_SECTION
, "editor_wordcompletion_collect_entire_file", 0))
1309 last_byte
= edit
->last_byte
;
1313 last_byte
= word_start
;
1316 srch
->search_type
= MC_SEARCH_T_REGEX
;
1317 srch
->is_case_sensitive
= TRUE
;
1318 srch
->search_fn
= edit_search_cmd_callback
;
1320 current_word
= edit_collect_completions_get_current_word (edit
, srch
, word_start
);
1322 temp
= g_string_new ("");
1324 /* collect max MAX_WORD_COMPLETIONS completions */
1325 while (mc_search_run (srch
, (void *) edit
, start
+ 1, last_byte
, &len
))
1327 g_string_set_size (temp
, 0);
1328 start
= srch
->normal_offset
;
1330 /* add matched completion if not yet added */
1331 for (i
= 0; i
< len
; i
++)
1333 skip
= edit_get_byte (edit
, start
+ i
);
1337 /* skip current word */
1338 if (start
+ (off_t
) i
== word_start
)
1341 g_string_append_c (temp
, skip
);
1347 if (current_word
!= NULL
&& strcmp (current_word
, temp
->str
) == 0)
1352 for (i
= 0; i
< *num
; i
++)
1355 ((char *) &compl[i
].text
[word_len
],
1356 (char *) &temp
->str
[word_len
], max (len
, compl[i
].len
) - word_len
) == 0)
1358 struct selection
this = compl[i
];
1359 for (++i
; i
< *num
; i
++)
1361 compl[i
- 1] = compl[i
];
1363 compl[*num
- 1] = this;
1365 break; /* skip it, already added */
1371 if (*num
== MAX_WORD_COMPLETIONS
)
1373 g_free (compl[0].text
);
1374 for (i
= 1; i
< *num
; i
++)
1376 compl[i
- 1] = compl[i
];
1383 recoded
= str_convert_to_display (temp
->str
);
1385 if (recoded
&& recoded
->len
)
1386 g_string_assign (temp
, recoded
->str
);
1388 g_string_free (recoded
, TRUE
);
1391 compl[*num
].text
= g_strdup (temp
->str
);
1392 compl[*num
].len
= temp
->len
;
1396 /* note the maximal length needed for the completion dialog */
1401 mc_search_free (srch
);
1402 g_string_free (temp
, TRUE
);
1403 g_free (current_word
);
1408 /* --------------------------------------------------------------------------------------------- */
1411 edit_insert_column_of_text (WEdit
* edit
, unsigned char *data
, int size
, int width
,
1412 off_t
* start_pos
, off_t
* end_pos
, int *col1
, int *col2
)
1417 cursor
= edit
->curs1
;
1418 col
= edit_get_col (edit
);
1420 for (i
= 0; i
< size
; i
++)
1422 if (data
[i
] != '\n')
1423 edit_insert (edit
, data
[i
]);
1425 { /* fill in and move to next line */
1429 if (edit_get_byte (edit
, edit
->curs1
) != '\n')
1431 l
= width
- (edit_get_col (edit
) - col
);
1434 edit_insert (edit
, ' ');
1438 for (p
= edit
->curs1
;; p
++)
1440 if (p
== edit
->last_byte
)
1442 edit_cursor_move (edit
, edit
->last_byte
- edit
->curs1
);
1443 edit_insert_ahead (edit
, '\n');
1447 if (edit_get_byte (edit
, p
) == '\n')
1453 edit_cursor_move (edit
, edit_move_forward3 (edit
, p
, col
, 0) - edit
->curs1
);
1454 l
= col
- edit_get_col (edit
);
1455 while (l
>= space_width
)
1457 edit_insert (edit
, ' ');
1464 *col2
= col
+ width
;
1465 *start_pos
= cursor
;
1466 *end_pos
= edit
->curs1
;
1467 edit_cursor_move (edit
, cursor
- edit
->curs1
);
1470 /* --------------------------------------------------------------------------------------------- */
1473 edit_macro_comparator (gconstpointer
* macro1
, gconstpointer
* macro2
)
1475 const macros_t
*m1
= (const macros_t
*) macro1
;
1476 const macros_t
*m2
= (const macros_t
*) macro2
;
1478 return m1
->hotkey
- m2
->hotkey
;
1481 /* --------------------------------------------------------------------------------------------- */
1484 edit_macro_sort_by_hotkey (void)
1486 if (macros_list
!= NULL
&& macros_list
->len
!= 0)
1487 g_array_sort (macros_list
, (GCompareFunc
) edit_macro_comparator
);
1490 /* --------------------------------------------------------------------------------------------- */
1493 edit_get_macro (WEdit
* edit
, int hotkey
, const macros_t
** macros
, guint
* indx
)
1495 const macros_t
*array_start
= &g_array_index (macros_list
, struct macros_t
, 0);
1497 macros_t search_macro
;
1501 search_macro
.hotkey
= hotkey
;
1502 result
= bsearch (&search_macro
, macros_list
->data
, macros_list
->len
,
1503 sizeof (macros_t
), (GCompareFunc
) edit_macro_comparator
);
1505 if (result
!= NULL
&& result
->macro
!= NULL
)
1507 *indx
= (result
- array_start
);
1515 /* --------------------------------------------------------------------------------------------- */
1516 /** returns FALSE on error */
1519 edit_delete_macro (WEdit
* edit
, int hotkey
)
1521 mc_config_t
*macros_config
= NULL
;
1522 const char *section_name
= "editor";
1523 gchar
*macros_fname
;
1526 const macros_t
*macros
= NULL
;
1528 /* clear array of actions for current hotkey */
1529 while (edit_get_macro (edit
, hotkey
, ¯os
, &indx
))
1531 if (macros
->macro
!= NULL
)
1532 g_array_free (macros
->macro
, TRUE
);
1534 g_array_remove_index (macros_list
, indx
);
1535 edit_macro_sort_by_hotkey ();
1538 macros_fname
= mc_config_get_full_path (MC_MACRO_FILE
);
1539 macros_config
= mc_config_init (macros_fname
, FALSE
);
1540 g_free (macros_fname
);
1542 if (macros_config
== NULL
)
1545 skeyname
= lookup_key_by_code (hotkey
);
1546 while (mc_config_del_key (macros_config
, section_name
, skeyname
))
1549 mc_config_save_file (macros_config
, NULL
);
1550 mc_config_deinit (macros_config
);
1555 /* --------------------------------------------------------------------------------------------- */
1556 /*** public functions ****************************************************************************/
1557 /* --------------------------------------------------------------------------------------------- */
1560 edit_help_cmd (WEdit
* edit
)
1562 ev_help_t event_data
= { NULL
, "[Internal File Editor]" };
1563 mc_event_raise (MCEVENT_GROUP_CORE
, "help", &event_data
);
1565 edit
->force
|= REDRAW_COMPLETELY
;
1568 /* --------------------------------------------------------------------------------------------- */
1571 edit_refresh_cmd (WEdit
* edit
)
1576 edit_get_syntax_color (edit
, -1, &color
);
1577 tty_touch_screen ();
1584 #endif /* !HAVE_SLANG */
1588 /* --------------------------------------------------------------------------------------------- */
1591 menu_save_mode_cmd (void)
1594 const int DLG_X
= 38;
1595 const int DLG_Y
= 13;
1599 const char *str
[] = {
1602 N_("&Do backups with following extension:")
1605 QuickWidget widgets
[] = {
1607 QUICK_BUTTON (18, DLG_X
, DLG_Y
- 3, DLG_Y
, N_("&Cancel"), B_CANCEL
, NULL
),
1609 QUICK_BUTTON (6, DLG_X
, DLG_Y
- 3, DLG_Y
, N_("&OK"), B_ENTER
, NULL
),
1611 QUICK_CHECKBOX (4, DLG_X
, 8, DLG_Y
, N_("Check &POSIX new line"), &option_check_nl_at_eof
),
1613 QUICK_INPUT (8, DLG_X
, 6, DLG_Y
, option_backup_ext
, 9, 0, "edit-backup-ext", &str_result
),
1615 QUICK_RADIO (4, DLG_X
, 3, DLG_Y
, 3, str
, &option_save_mode
),
1619 QuickDialog dialog
= {
1620 DLG_X
, DLG_Y
, -1, -1, N_("Edit Save Mode"),
1621 "[Edit Save Mode]", widgets
, NULL
, FALSE
1626 size_t w0
, w1
, b_len
, w3
;
1628 #ifdef HAVE_ASSERT_H
1629 assert (option_backup_ext
!= NULL
);
1632 /* OK/Cancel buttons */
1633 w0
= str_term_width1 (_(widgets
[0].u
.button
.text
)) + 3;
1634 w1
= str_term_width1 (_(widgets
[1].u
.button
.text
)) + 5; /* default button */
1635 b_len
= w0
+ w1
+ 3;
1637 maxlen
= max (b_len
, (size_t) str_term_width1 (_(dialog
.title
)) + 2);
1640 for (i
= 0; i
< 3; i
++)
1645 w3
= max (w3
, (size_t) str_term_width1 (str
[i
]));
1648 maxlen
= max (maxlen
, w3
+ 4);
1650 dialog
.xlen
= min ((size_t) COLS
, maxlen
+ 8);
1652 widgets
[3].u
.input
.len
= w3
;
1653 widgets
[1].relative_x
= (dialog
.xlen
- b_len
) / 2;
1654 widgets
[0].relative_x
= widgets
[1].relative_x
+ w0
+ 2;
1656 for (i
= 0; i
< sizeof (widgets
) / sizeof (widgets
[0]); i
++)
1657 widgets
[i
].x_divisions
= dialog
.xlen
;
1659 if (quick_dialog (&dialog
) != B_CANCEL
)
1661 g_free (option_backup_ext
);
1662 option_backup_ext
= str_result
;
1666 /* --------------------------------------------------------------------------------------------- */
1669 edit_set_filename (WEdit
* edit
, const vfs_path_t
* name_vpath
)
1671 vfs_path_free (edit
->filename_vpath
);
1672 edit
->filename_vpath
= vfs_path_clone (name_vpath
);
1674 if (edit
->dir_vpath
== NULL
)
1675 edit
->dir_vpath
= vfs_path_clone (vfs_get_raw_current_dir ());
1678 /* --------------------------------------------------------------------------------------------- */
1679 /* Here we want to warn the users of overwriting an existing file,
1680 but only if they have made a change to the filename */
1681 /* returns TRUE on success */
1683 edit_save_as_cmd (WEdit
* edit
)
1685 /* This heads the 'Save As' dialog box */
1686 vfs_path_t
*exp_vpath
;
1688 int different_filename
= 0;
1690 if (!edit_check_newline (edit
))
1693 exp_vpath
= edit_get_save_file_as (edit
);
1694 edit_push_undo_action (edit
, KEY_PRESS
+ edit
->start_display
);
1696 if (exp_vpath
!= NULL
)
1698 if (vfs_path_len (exp_vpath
) == 0)
1704 if (vfs_path_cmp (edit
->filename_vpath
, exp_vpath
) != 0)
1709 if (mc_stat (exp_vpath
, &sb
) == 0 && !S_ISREG (sb
.st_mode
))
1711 edit_error_dialog (_("Save as"),
1713 ("Cannot save: destination is not a regular file")));
1717 different_filename
= 1;
1718 file
= mc_open (exp_vpath
, O_RDONLY
| O_BINARY
);
1722 /* the file exists */
1724 /* Overwrite the current file or cancel the operation */
1725 if (edit_query_dialog2
1727 _("A file already exists with this name"), _("&Overwrite"), _("&Cancel")))
1732 edit
->stat1
.st_mode
|= S_IWUSR
;
1734 save_lock
= lock_file (exp_vpath
);
1738 /* filenames equal, check if already locked */
1739 if (!edit
->locked
&& !edit
->delete_file
)
1740 save_lock
= lock_file (exp_vpath
);
1743 if (different_filename
)
1746 * Allow user to write into saved (under another name) file
1747 * even if original file had r/o user permissions.
1749 edit
->stat1
.st_mode
|= S_IWRITE
;
1752 rv
= edit_save_file (edit
, exp_vpath
);
1756 /* Succesful, so unlock both files */
1757 if (different_filename
)
1760 unlock_file (exp_vpath
);
1762 edit
->locked
= unlock_file (edit
->filename_vpath
);
1766 if (edit
->locked
|| save_lock
)
1767 edit
->locked
= unlock_file (edit
->filename_vpath
);
1770 edit_set_filename (edit
, exp_vpath
);
1771 if (edit
->lb
!= LB_ASIS
)
1772 edit_reload (edit
, exp_vpath
);
1774 edit
->delete_file
= 0;
1775 if (different_filename
)
1776 edit_load_syntax (edit
, NULL
, edit
->syntax_type
);
1777 vfs_path_free (exp_vpath
);
1778 edit
->force
|= REDRAW_COMPLETELY
;
1781 edit_error_dialog (_("Save as"), get_sys_error (_("Cannot save file")));
1784 /* Failed, so maintain modify (not save) lock */
1786 unlock_file (exp_vpath
);
1793 vfs_path_free (exp_vpath
);
1794 edit
->force
|= REDRAW_COMPLETELY
;
1798 /* {{{ Macro stuff starts here */
1799 /* --------------------------------------------------------------------------------------------- */
1802 edit_delete_macro_cmd (WEdit
* edit
)
1806 hotkey
= editcmd_dialog_raw_key_query (_("Delete macro"), _("Press macro hotkey:"), TRUE
);
1808 if (hotkey
!= 0 && !edit_delete_macro (edit
, hotkey
))
1809 message (D_ERROR
, _("Delete macro"), _("Macro not deleted"));
1812 /* --------------------------------------------------------------------------------------------- */
1814 /** returns FALSE on error */
1816 edit_execute_macro (WEdit
* edit
, int hotkey
)
1818 gboolean res
= FALSE
;
1822 const macros_t
*macros
;
1825 if (edit_get_macro (edit
, hotkey
, ¯os
, &indx
) &&
1826 macros
->macro
!= NULL
&& macros
->macro
->len
!= 0)
1830 edit
->force
|= REDRAW_PAGE
;
1832 for (i
= 0; i
< macros
->macro
->len
; i
++)
1834 const macro_action_t
*m_act
;
1836 m_act
= &g_array_index (macros
->macro
, struct macro_action_t
, i
);
1837 edit_execute_cmd (edit
, m_act
->action
, m_act
->ch
);
1846 /* --------------------------------------------------------------------------------------------- */
1848 /** returns FALSE on error */
1850 edit_store_macro_cmd (WEdit
* edit
)
1854 GString
*marcros_string
;
1855 mc_config_t
*macros_config
= NULL
;
1856 const char *section_name
= "editor";
1857 gchar
*macros_fname
;
1858 GArray
*macros
; /* current macro */
1860 gboolean have_macro
= FALSE
;
1861 char *skeyname
= NULL
;
1864 editcmd_dialog_raw_key_query (_("Save macro"), _("Press the macro's new hotkey:"), TRUE
);
1865 if (hotkey
== ESC_CHAR
)
1868 tmp_act
= keybind_lookup_keymap_command (editor_map
, hotkey
);
1870 /* return FALSE if try assign macro into restricted hotkeys */
1871 if (tmp_act
== CK_MacroStartRecord
1872 || tmp_act
== CK_MacroStopRecord
|| tmp_act
== CK_MacroStartStopRecord
)
1875 edit_delete_macro (edit
, hotkey
);
1877 macros_fname
= mc_config_get_full_path (MC_MACRO_FILE
);
1878 macros_config
= mc_config_init (macros_fname
, FALSE
);
1879 g_free (macros_fname
);
1881 if (macros_config
== NULL
)
1884 edit_push_undo_action (edit
, KEY_PRESS
+ edit
->start_display
);
1886 marcros_string
= g_string_sized_new (250);
1887 macros
= g_array_new (TRUE
, FALSE
, sizeof (macro_action_t
));
1889 skeyname
= lookup_key_by_code (hotkey
);
1891 for (i
= 0; i
< macro_index
; i
++)
1893 macro_action_t m_act
;
1894 const char *action_name
;
1896 action_name
= keybind_lookup_actionname (record_macro_buf
[i
].action
);
1898 if (action_name
== NULL
)
1901 m_act
.action
= record_macro_buf
[i
].action
;
1902 m_act
.ch
= record_macro_buf
[i
].ch
;
1903 g_array_append_val (macros
, m_act
);
1905 g_string_append_printf (marcros_string
, "%s:%i;", action_name
,
1906 (int) record_macro_buf
[i
].ch
);
1911 macro
.hotkey
= hotkey
;
1912 macro
.macro
= macros
;
1913 g_array_append_val (macros_list
, macro
);
1914 mc_config_set_string (macros_config
, section_name
, skeyname
, marcros_string
->str
);
1917 mc_config_del_key (macros_config
, section_name
, skeyname
);
1920 edit_macro_sort_by_hotkey ();
1922 g_string_free (marcros_string
, TRUE
);
1923 mc_config_save_file (macros_config
, NULL
);
1924 mc_config_deinit (macros_config
);
1928 /* --------------------------------------------------------------------------------------------- */
1931 edit_repeat_macro_cmd (WEdit
* edit
)
1938 f
= input_dialog (_("Repeat last commands"), _("Repeat times:"), MC_HISTORY_EDIT_REPEAT
, NULL
);
1939 if (f
== NULL
|| *f
== '\0')
1945 count_repeat
= strtol (f
, &error
, 0);
1955 edit_push_undo_action (edit
, KEY_PRESS
+ edit
->start_display
);
1956 edit
->force
|= REDRAW_PAGE
;
1958 for (j
= 0; j
< count_repeat
; j
++)
1959 for (i
= 0; i
< macro_index
; i
++)
1960 edit_execute_cmd (edit
, record_macro_buf
[i
].action
, record_macro_buf
[i
].ch
);
1961 edit_update_screen (edit
);
1965 /* --------------------------------------------------------------------------------------------- */
1966 /** return FALSE on error */
1969 edit_load_macro_cmd (WEdit
* edit
)
1971 mc_config_t
*macros_config
= NULL
;
1972 gchar
**profile_keys
, **keys
;
1973 gchar
**values
, **curr_values
;
1974 gsize len
, values_len
;
1975 const char *section_name
= "editor";
1976 gchar
*macros_fname
;
1981 macros_fname
= mc_config_get_full_path (MC_MACRO_FILE
);
1982 macros_config
= mc_config_init (macros_fname
, TRUE
);
1983 g_free (macros_fname
);
1985 if (macros_config
== NULL
)
1988 profile_keys
= keys
= mc_config_get_keys (macros_config
, section_name
, &len
);
1989 while (*profile_keys
!= NULL
)
1991 gboolean have_macro
;
1995 macros
= g_array_new (TRUE
, FALSE
, sizeof (macro_action_t
));
1997 curr_values
= values
= mc_config_get_string_list (macros_config
, section_name
,
1998 *profile_keys
, &values_len
);
1999 hotkey
= lookup_key (*profile_keys
, NULL
);
2002 while (*curr_values
!= NULL
&& *curr_values
[0] != '\0')
2004 char **macro_pair
= NULL
;
2006 macro_pair
= g_strsplit (*curr_values
, ":", 2);
2008 if (macro_pair
!= NULL
)
2010 macro_action_t m_act
;
2011 if (macro_pair
[0] == NULL
|| macro_pair
[0][0] == '\0')
2015 m_act
.action
= keybind_lookup_action (macro_pair
[0]);
2016 g_free (macro_pair
[0]);
2017 macro_pair
[0] = NULL
;
2019 if (macro_pair
[1] == NULL
|| macro_pair
[1][0] == '\0')
2023 m_act
.ch
= strtol (macro_pair
[1], NULL
, 0);
2024 g_free (macro_pair
[1]);
2025 macro_pair
[1] = NULL
;
2027 if (m_act
.action
!= 0)
2029 /* a shell command */
2030 if ((m_act
.action
/ CK_PipeBlock (0)) == 1)
2032 m_act
.action
= CK_PipeBlock (0) + (m_act
.ch
> 0 ? m_act
.ch
: 0);
2035 g_array_append_val (macros
, m_act
);
2038 g_strfreev (macro_pair
);
2045 macro
.hotkey
= hotkey
;
2046 macro
.macro
= macros
;
2047 g_array_append_val (macros_list
, macro
);
2050 g_strfreev (values
);
2053 mc_config_deinit (macros_config
);
2054 edit_macro_sort_by_hotkey ();
2058 /* }}} Macro stuff end here */
2060 /* --------------------------------------------------------------------------------------------- */
2061 /** returns TRUE on success */
2064 edit_save_confirm_cmd (WEdit
* edit
)
2068 if (edit
->filename_vpath
== NULL
)
2069 return edit_save_as_cmd (edit
);
2071 if (!edit_check_newline (edit
))
2074 if (edit_confirm_save
)
2079 filename
= vfs_path_to_str (edit
->filename_vpath
);
2080 f
= g_strdup_printf (_("Confirm save file: \"%s\""), filename
);
2082 ok
= (edit_query_dialog2 (_("Save file"), f
, _("&Save"), _("&Cancel")) == 0);
2087 return edit_save_cmd (edit
);
2090 /* --------------------------------------------------------------------------------------------- */
2092 /* returns TRUE on success */
2094 edit_new_cmd (WEdit
* edit
)
2097 && edit_query_dialog2 (_("Warning"),
2098 _("Current text was modified without a file save.\n"
2099 "Continue discards these changes"),
2100 _("C&ontinue"), _("&Cancel")) == 1)
2102 edit
->force
|= REDRAW_COMPLETELY
;
2106 edit
->force
|= REDRAW_COMPLETELY
;
2108 return edit_renew (edit
); /* if this gives an error, something has really screwed up */
2111 /* --------------------------------------------------------------------------------------------- */
2114 edit_load_cmd (WEdit
* edit
, edit_current_file_t what
)
2116 gboolean ret
= TRUE
;
2119 && edit_query_dialog2 (_("Warning"),
2120 _("Current text was modified without a file save.\n"
2121 "Continue discards these changes"), _("C&ontinue"),
2124 edit
->force
|= REDRAW_COMPLETELY
;
2130 case EDIT_FILE_COMMON
:
2132 char *filename
, *exp
;
2134 filename
= vfs_path_to_str (edit
->filename_vpath
);
2135 exp
= input_expand_dialog (_("Load"), _("Enter file name:"),
2136 MC_HISTORY_EDIT_LOAD
, filename
);
2139 if (exp
!= NULL
&& *exp
!= '\0')
2141 vfs_path_t
*exp_vpath
;
2143 exp_vpath
= vfs_path_from_str (exp
);
2144 ret
= edit_load_file_from_filename (edit
, exp_vpath
);
2145 vfs_path_free (exp_vpath
);
2152 case EDIT_FILE_SYNTAX
:
2153 edit_load_syntax_file (edit
);
2156 case EDIT_FILE_MENU
:
2157 edit_load_menu_file (edit
);
2164 edit
->force
|= REDRAW_COMPLETELY
;
2168 /* --------------------------------------------------------------------------------------------- */
2170 if mark2 is -1 then marking is from mark1 to the cursor.
2171 Otherwise its between the markers. This handles this.
2172 Returns 1 if no text is marked.
2176 eval_marks (WEdit
* edit
, off_t
* start_mark
, off_t
* end_mark
)
2178 if (edit
->mark1
!= edit
->mark2
)
2180 off_t start_bol
, start_eol
;
2181 off_t end_bol
, end_eol
;
2186 if (edit
->end_mark_curs
< 0)
2187 end_mark_curs
= edit
->curs1
;
2189 end_mark_curs
= edit
->end_mark_curs
;
2191 if (edit
->mark2
>= 0)
2193 *start_mark
= min (edit
->mark1
, edit
->mark2
);
2194 *end_mark
= max (edit
->mark1
, edit
->mark2
);
2198 *start_mark
= min (edit
->mark1
, end_mark_curs
);
2199 *end_mark
= max (edit
->mark1
, end_mark_curs
);
2200 edit
->column2
= edit
->curs_col
+ edit
->over_col
;
2203 if (edit
->column_highlight
2204 && (((edit
->mark1
> end_mark_curs
) && (edit
->column1
< edit
->column2
))
2205 || ((edit
->mark1
< end_mark_curs
) && (edit
->column1
> edit
->column2
))))
2207 start_bol
= edit_bol (edit
, *start_mark
);
2208 start_eol
= edit_eol (edit
, start_bol
- 1) + 1;
2209 end_bol
= edit_bol (edit
, *end_mark
);
2210 end_eol
= edit_eol (edit
, *end_mark
);
2211 col1
= min (edit
->column1
, edit
->column2
);
2212 col2
= max (edit
->column1
, edit
->column2
);
2214 diff1
= edit_move_forward3 (edit
, start_bol
, col2
, 0) -
2215 edit_move_forward3 (edit
, start_bol
, col1
, 0);
2216 diff2
= edit_move_forward3 (edit
, end_bol
, col2
, 0) -
2217 edit_move_forward3 (edit
, end_bol
, col1
, 0);
2219 *start_mark
-= diff1
;
2221 *start_mark
= max (*start_mark
, start_eol
);
2222 *end_mark
= min (*end_mark
, end_eol
);
2228 *start_mark
= *end_mark
= 0;
2229 edit
->column2
= edit
->column1
= 0;
2234 /* --------------------------------------------------------------------------------------------- */
2237 edit_insert_over (WEdit
* edit
)
2241 for (i
= 0; i
< edit
->over_col
; i
++)
2243 edit_insert (edit
, ' ');
2248 /* --------------------------------------------------------------------------------------------- */
2251 edit_insert_column_of_text_from_file (WEdit
* edit
, int file
,
2252 off_t
* start_pos
, off_t
* end_pos
, long *col1
, long *col2
)
2256 off_t blocklen
= -1, width
= 0;
2257 unsigned char *data
;
2259 cursor
= edit
->curs1
;
2260 col
= edit_get_col (edit
);
2261 data
= g_malloc0 (TEMP_BUF_LEN
);
2263 while ((blocklen
= mc_read (file
, (char *) data
, TEMP_BUF_LEN
)) > 0)
2266 for (width
= 0; width
< blocklen
; width
++)
2268 if (data
[width
] == '\n')
2271 for (i
= 0; i
< blocklen
; i
++)
2273 if (data
[i
] == '\n')
2274 { /* fill in and move to next line */
2277 if (edit_get_byte (edit
, edit
->curs1
) != '\n')
2279 l
= width
- (edit_get_col (edit
) - col
);
2282 edit_insert (edit
, ' ');
2286 for (p
= edit
->curs1
;; p
++)
2288 if (p
== edit
->last_byte
)
2290 edit_cursor_move (edit
, edit
->last_byte
- edit
->curs1
);
2291 edit_insert_ahead (edit
, '\n');
2295 if (edit_get_byte (edit
, p
) == '\n')
2301 edit_cursor_move (edit
, edit_move_forward3 (edit
, p
, col
, 0) - edit
->curs1
);
2302 l
= col
- edit_get_col (edit
);
2303 while (l
>= space_width
)
2305 edit_insert (edit
, ' ');
2310 edit_insert (edit
, data
[i
]);
2314 *col2
= col
+ width
;
2315 *start_pos
= cursor
;
2316 *end_pos
= edit
->curs1
;
2317 edit_cursor_move (edit
, cursor
- edit
->curs1
);
2323 /* --------------------------------------------------------------------------------------------- */
2326 edit_block_copy_cmd (WEdit
* edit
)
2328 off_t start_mark
, end_mark
, current
= edit
->curs1
;
2333 unsigned char *copy_buf
;
2335 edit_update_curs_col (edit
);
2336 if (eval_marks (edit
, &start_mark
, &end_mark
))
2339 copy_buf
= edit_get_block (edit
, start_mark
, end_mark
, &size
);
2341 /* all that gets pushed are deletes hence little space is used on the stack */
2343 edit_push_markers (edit
);
2345 if (edit
->column_highlight
)
2347 col_delta
= abs (edit
->column2
- edit
->column1
);
2348 edit_insert_column_of_text (edit
, copy_buf
, size
, col_delta
, &mark1
, &mark2
, &c1
, &c2
);
2353 edit_insert_ahead (edit
, copy_buf
[size
]);
2357 edit_scroll_screen_over_cursor (edit
);
2359 if (edit
->column_highlight
)
2360 edit_set_markers (edit
, edit
->curs1
, mark2
, c1
, c2
);
2361 else if (start_mark
< current
&& end_mark
> current
)
2362 edit_set_markers (edit
, start_mark
, end_mark
+ end_mark
- start_mark
, 0, 0);
2364 edit
->force
|= REDRAW_PAGE
;
2368 /* --------------------------------------------------------------------------------------------- */
2371 edit_block_move_cmd (WEdit
* edit
)
2374 unsigned char *copy_buf
= NULL
;
2375 off_t start_mark
, end_mark
;
2378 if (eval_marks (edit
, &start_mark
, &end_mark
))
2381 line
= edit
->curs_line
;
2382 if (edit
->mark2
< 0)
2383 edit_mark_cmd (edit
, FALSE
);
2384 edit_push_markers (edit
);
2386 if (edit
->column_highlight
)
2394 c1
= min (edit
->column1
, edit
->column2
);
2395 c2
= max (edit
->column1
, edit
->column2
);
2396 b_width
= (c2
- c1
);
2398 edit_update_curs_col (edit
);
2401 x2
= x
+ edit
->over_col
;
2403 /* do nothing when cursor inside first line of selected area */
2404 if ((edit_eol (edit
, edit
->curs1
) == edit_eol (edit
, start_mark
)) && (x2
> c1
&& x2
<= c2
))
2407 if (edit
->curs1
> start_mark
&& edit
->curs1
< edit_eol (edit
, end_mark
))
2411 else if (x
> c1
&& x
<= c2
)
2414 /* save current selection into buffer */
2415 copy_buf
= edit_get_block (edit
, start_mark
, end_mark
, &size
);
2417 /* remove current selection */
2418 edit_block_delete_cmd (edit
);
2420 edit
->over_col
= max (0, edit
->over_col
- b_width
);
2421 /* calculate the cursor pos after delete block */
2422 current
= edit_move_forward3 (edit
, edit_bol (edit
, edit
->curs1
), x
, 0);
2423 edit_cursor_move (edit
, current
- edit
->curs1
);
2424 edit_scroll_screen_over_cursor (edit
);
2426 /* add TWS if need before block insertion */
2427 if (option_cursor_beyond_eol
&& edit
->over_col
> 0)
2428 edit_insert_over (edit
);
2430 edit_insert_column_of_text (edit
, copy_buf
, size
, b_width
, &mark1
, &mark2
, &c1
, &c2
);
2431 edit_set_markers (edit
, mark1
, mark2
, c1
, c2
);
2437 current
= edit
->curs1
;
2438 copy_buf
= g_malloc0 (end_mark
- start_mark
);
2439 edit_cursor_move (edit
, start_mark
- edit
->curs1
);
2440 edit_scroll_screen_over_cursor (edit
);
2442 while (count
< end_mark
)
2444 copy_buf
[end_mark
- count
- 1] = edit_delete (edit
, 1);
2447 edit_scroll_screen_over_cursor (edit
);
2448 edit_cursor_move (edit
,
2449 current
- edit
->curs1
-
2450 (((current
- edit
->curs1
) > 0) ? end_mark
- start_mark
: 0));
2451 edit_scroll_screen_over_cursor (edit
);
2452 while (count
-- > start_mark
)
2453 edit_insert_ahead (edit
, copy_buf
[end_mark
- count
- 1]);
2454 edit_set_markers (edit
, edit
->curs1
, edit
->curs1
+ end_mark
- start_mark
, 0, 0);
2457 edit_scroll_screen_over_cursor (edit
);
2459 edit
->force
|= REDRAW_PAGE
;
2462 /* --------------------------------------------------------------------------------------------- */
2463 /** returns 1 if canceelled by user */
2466 edit_block_delete_cmd (WEdit
* edit
)
2468 off_t start_mark
, end_mark
;
2469 if (eval_marks (edit
, &start_mark
, &end_mark
))
2471 edit_delete_line (edit
);
2474 return edit_block_delete (edit
);
2477 /* --------------------------------------------------------------------------------------------- */
2478 /** call with edit = 0 before shutdown to close memory leaks */
2481 edit_replace_cmd (WEdit
* edit
, int again
)
2483 /* 1 = search string, 2 = replace with */
2484 static char *saved1
= NULL
; /* saved default[123] */
2485 static char *saved2
= NULL
;
2486 char *input1
= NULL
; /* user input from the dialog */
2487 char *input2
= NULL
;
2488 GString
*input2_str
= NULL
;
2491 long times_replaced
= 0;
2492 gboolean once_found
= FALSE
;
2496 g_free (saved1
), saved1
= NULL
;
2497 g_free (saved2
), saved2
= NULL
;
2501 edit
->force
|= REDRAW_COMPLETELY
;
2503 if (again
&& !saved1
&& !saved2
)
2508 input1
= g_strdup (saved1
? saved1
: "");
2509 input2
= g_strdup (saved2
? saved2
: "");
2513 char *tmp_inp1
, *tmp_inp2
;
2515 disp1
= edit_replace_cmd__conv_to_display (saved1
? saved1
: (char *) "");
2516 disp2
= edit_replace_cmd__conv_to_display (saved2
? saved2
: (char *) "");
2518 edit_push_undo_action (edit
, KEY_PRESS
+ edit
->start_display
);
2520 editcmd_dialog_replace_show (edit
, disp1
, disp2
, &input1
, &input2
);
2525 if (input1
== NULL
|| *input1
== '\0')
2527 edit
->force
= REDRAW_COMPLETELY
;
2533 input1
= edit_replace_cmd__conv_to_input (input1
);
2534 input2
= edit_replace_cmd__conv_to_input (input2
);
2538 g_free (saved1
), saved1
= g_strdup (input1
);
2539 g_free (saved2
), saved2
= g_strdup (input2
);
2541 mc_search_free (edit
->search
);
2542 edit
->search
= NULL
;
2545 input2_str
= g_string_new (input2
);
2549 edit
->search
= mc_search_new (input1
, -1);
2550 if (edit
->search
== NULL
)
2552 edit
->search_start
= edit
->curs1
;
2555 edit
->search
->search_type
= edit_search_options
.type
;
2556 edit
->search
->is_all_charsets
= edit_search_options
.all_codepages
;
2557 edit
->search
->is_case_sensitive
= edit_search_options
.case_sens
;
2558 edit
->search
->whole_words
= edit_search_options
.whole_words
;
2559 edit
->search
->search_fn
= edit_search_cmd_callback
;
2560 edit
->search_line_type
= edit_get_search_line_type (edit
->search
);
2561 edit_search_fix_search_start_if_selection (edit
);
2564 if (edit
->found_len
&& edit
->search_start
== edit
->found_start
+ 1
2565 && edit_search_options
.backwards
)
2566 edit
->search_start
--;
2568 if (edit
->found_len
&& edit
->search_start
== edit
->found_start
- 1
2569 && !edit_search_options
.backwards
)
2570 edit
->search_start
++;
2576 if (!editcmd_find (edit
, &len
))
2578 if (!(edit
->search
->error
== MC_SEARCH_E_OK
||
2579 (once_found
&& edit
->search
->error
== MC_SEARCH_E_NOTFOUND
)))
2581 edit_error_dialog (_("Search"), edit
->search
->error_str
);
2587 edit
->search_start
= edit
->search
->normal_offset
;
2588 /*returns negative on not found or error in pattern */
2590 if ((edit
->search_start
>= 0) && (edit
->search_start
< edit
->last_byte
))
2595 edit
->found_start
= edit
->search_start
;
2596 i
= edit
->found_len
= len
;
2598 edit_cursor_move (edit
, edit
->search_start
- edit
->curs1
);
2599 edit_scroll_screen_over_cursor (edit
);
2601 if (edit
->replace_mode
== 0)
2606 l
= (long) (edit
->curs_row
- edit
->widget
.lines
/ 3);
2608 edit_scroll_downward (edit
, l
);
2610 edit_scroll_upward (edit
, -l
);
2612 edit_scroll_screen_over_cursor (edit
);
2613 edit
->force
|= REDRAW_PAGE
;
2614 edit_render_keypress (edit
);
2616 /*so that undo stops at each query */
2617 edit_push_key_press (edit
);
2618 /* and prompt 2/3 down */
2619 disp1
= edit_replace_cmd__conv_to_display (saved1
);
2620 disp2
= edit_replace_cmd__conv_to_display (saved2
);
2621 prompt
= editcmd_dialog_replace_prompt_show (edit
, disp1
, disp2
, -1, -1);
2625 if (prompt
== B_REPLACE_ALL
)
2626 edit
->replace_mode
= 1;
2627 else if (prompt
== B_SKIP_REPLACE
)
2629 if (edit_search_options
.backwards
)
2630 edit
->search_start
--;
2632 edit
->search_start
++;
2633 continue; /* loop */
2635 else if (prompt
== B_CANCEL
)
2637 edit
->replace_mode
= -1;
2642 repl_str
= mc_search_prepare_replace_str (edit
->search
, input2_str
);
2644 if (edit
->search
->error
!= MC_SEARCH_E_OK
)
2646 edit_error_dialog (_("Replace"), edit
->search
->error_str
);
2647 g_string_free (repl_str
, TRUE
);
2651 /* delete then insert new */
2652 for (i
= 0; i
< len
; i
++)
2653 edit_delete (edit
, 1);
2655 for (i
= 0; i
< repl_str
->len
; i
++)
2656 edit_insert (edit
, repl_str
->str
[i
]);
2658 edit
->found_len
= repl_str
->len
;
2659 g_string_free (repl_str
, TRUE
);
2662 /* so that we don't find the same string again */
2663 if (edit_search_options
.backwards
)
2665 edit
->search_start
--;
2669 edit
->search_start
+= edit
->found_len
+ (len
== 0 ? 1 : 0);
2671 if (edit
->search_start
>= edit
->last_byte
)
2675 edit_scroll_screen_over_cursor (edit
);
2679 /* try and find from right here for next search */
2680 edit
->search_start
= edit
->curs1
;
2681 edit_update_curs_col (edit
);
2683 edit
->force
|= REDRAW_PAGE
;
2684 edit_render_keypress (edit
);
2686 if (times_replaced
== 0)
2687 query_dialog (_("Replace"), _("Search string not found"), D_NORMAL
, 1, _("&OK"));
2691 while (edit
->replace_mode
>= 0);
2693 edit_scroll_screen_over_cursor (edit
);
2694 edit
->force
|= REDRAW_COMPLETELY
;
2695 edit_render_keypress (edit
);
2697 if ((edit
->replace_mode
== 1) && (times_replaced
!= 0))
2698 message (D_NORMAL
, _("Replace"), _("%ld replacements made"), times_replaced
);
2703 if (input2_str
!= NULL
)
2704 g_string_free (input2_str
, TRUE
);
2707 /* --------------------------------------------------------------------------------------------- */
2710 edit_search_cmd_callback (const void *user_data
, gsize char_offset
, int *current_char
)
2712 *current_char
= edit_get_byte ((WEdit
*) user_data
, (off_t
) char_offset
);
2713 return MC_SEARCH_CB_OK
;
2716 /* --------------------------------------------------------------------------------------------- */
2719 edit_search_cmd (WEdit
* edit
, gboolean again
)
2727 else if (edit
->last_search_string
!= NULL
)
2728 edit_do_search (edit
);
2731 /* find last search string in history */
2734 history
= history_get (MC_HISTORY_SHARED_SEARCH
);
2735 if (history
!= NULL
&& history
->data
!= NULL
)
2737 edit
->last_search_string
= (char *) history
->data
;
2738 history
->data
= NULL
;
2739 history
= g_list_first (history
);
2740 g_list_foreach (history
, (GFunc
) g_free
, NULL
);
2741 g_list_free (history
);
2743 edit
->search
= mc_search_new (edit
->last_search_string
, -1);
2744 if (edit
->search
== NULL
)
2746 /* if not... then ask for an expression */
2747 g_free (edit
->last_search_string
);
2748 edit
->last_search_string
= NULL
;
2753 edit
->search
->search_type
= edit_search_options
.type
;
2754 edit
->search
->is_all_charsets
= edit_search_options
.all_codepages
;
2755 edit
->search
->is_case_sensitive
= edit_search_options
.case_sens
;
2756 edit
->search
->whole_words
= edit_search_options
.whole_words
;
2757 edit
->search
->search_fn
= edit_search_cmd_callback
;
2758 edit
->search_line_type
= edit_get_search_line_type (edit
->search
);
2759 edit_do_search (edit
);
2764 /* if not... then ask for an expression */
2765 g_free (edit
->last_search_string
);
2766 edit
->last_search_string
= NULL
;
2773 /* --------------------------------------------------------------------------------------------- */
2775 * Check if it's OK to close the editor. If there are unsaved changes,
2776 * ask user. Return 1 if it's OK to exit, 0 to continue editing.
2780 edit_ok_to_exit (WEdit
* edit
)
2784 if (!edit
->modified
)
2787 if (!mc_global
.midnight_shutdown
)
2789 if (!edit_check_newline (edit
))
2793 act
= edit_query_dialog3 (_("Quit"), _("File was modified. Save with exit?"),
2794 _("&Yes"), _("&No"), _("&Cancel quit"));
2799 edit_query_dialog2 (_("Quit"),
2800 _("Midnight Commander is being shut down.\nSave modified file?"),
2801 _("&Yes"), _("&No"));
2811 edit_push_markers (edit
);
2812 edit_set_markers (edit
, 0, 0, 0, 0);
2813 if (!edit_save_cmd (edit
) || mc_global
.midnight_shutdown
)
2814 return mc_global
.midnight_shutdown
;
2818 case 2: /* Cancel quit */
2826 /* --------------------------------------------------------------------------------------------- */
2827 /** save block, returns TRUE on success */
2830 edit_save_block (WEdit
* edit
, const char *filename
, off_t start
, off_t finish
)
2835 vpath
= vfs_path_from_str (filename
);
2836 file
= mc_open (vpath
, O_CREAT
| O_WRONLY
| O_TRUNC
,
2837 S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IROTH
| O_BINARY
);
2838 vfs_path_free (vpath
);
2842 if (edit
->column_highlight
)
2846 r
= mc_write (file
, VERTICAL_MAGIC
, sizeof (VERTICAL_MAGIC
));
2849 unsigned char *block
, *p
;
2851 p
= block
= edit_get_block (edit
, start
, finish
, &len
);
2854 r
= mc_write (file
, p
, len
);
2869 len
= finish
- start
;
2870 buf
= g_malloc0 (TEMP_BUF_LEN
);
2871 while (start
!= finish
)
2873 end
= min (finish
, start
+ TEMP_BUF_LEN
);
2874 for (; i
< end
; i
++)
2875 buf
[i
- start
] = edit_get_byte (edit
, i
);
2876 len
-= mc_write (file
, (char *) buf
, end
- start
);
2886 /* --------------------------------------------------------------------------------------------- */
2889 edit_paste_from_history (WEdit
* edit
)
2892 edit_error_dialog (_("Error"), _("This function is not implemented"));
2895 /* --------------------------------------------------------------------------------------------- */
2898 edit_copy_to_X_buf_cmd (WEdit
* edit
)
2900 off_t start_mark
, end_mark
;
2902 if (eval_marks (edit
, &start_mark
, &end_mark
))
2904 if (!edit_save_block_to_clip_file (edit
, start_mark
, end_mark
))
2906 edit_error_dialog (_("Copy to clipboard"), get_sys_error (_("Unable to save to file")));
2909 /* try use external clipboard utility */
2910 mc_event_raise (MCEVENT_GROUP_CORE
, "clipboard_file_to_ext_clip", NULL
);
2915 /* --------------------------------------------------------------------------------------------- */
2918 edit_cut_to_X_buf_cmd (WEdit
* edit
)
2920 off_t start_mark
, end_mark
;
2922 if (eval_marks (edit
, &start_mark
, &end_mark
))
2924 if (!edit_save_block_to_clip_file (edit
, start_mark
, end_mark
))
2926 edit_error_dialog (_("Cut to clipboard"), _("Unable to save to file"));
2929 /* try use external clipboard utility */
2930 mc_event_raise (MCEVENT_GROUP_CORE
, "clipboard_file_to_ext_clip", NULL
);
2932 edit_block_delete_cmd (edit
);
2933 edit_mark_cmd (edit
, TRUE
);
2938 /* --------------------------------------------------------------------------------------------- */
2941 edit_paste_from_X_buf_cmd (WEdit
* edit
)
2946 /* try use external clipboard utility */
2947 mc_event_raise (MCEVENT_GROUP_CORE
, "clipboard_file_from_ext_clip", NULL
);
2948 tmp
= mc_config_get_full_vpath (EDIT_CLIP_FILE
);
2949 ret
= (edit_insert_file (edit
, tmp
) >= 0);
2950 vfs_path_free (tmp
);
2955 /* --------------------------------------------------------------------------------------------- */
2957 * Ask user for the line and go to that line.
2958 * Negative numbers mean line from the end (i.e. -1 is the last line).
2962 edit_goto_cmd (WEdit
* edit
)
2965 static long line
= 0; /* line as typed, saved as default */
2970 g_snprintf (s
, sizeof (s
), "%ld", line
);
2971 f
= input_dialog (_("Goto line"), _("Enter line:"), MC_HISTORY_EDIT_GOTO_LINE
, line
? s
: "");
2981 l
= strtol (f
, &error
, 0);
2990 l
= edit
->total_lines
+ l
+ 2;
2991 edit_move_display (edit
, l
- edit
->widget
.lines
/ 2 - 1);
2992 edit_move_to_line (edit
, l
- 1);
2993 edit
->force
|= REDRAW_COMPLETELY
;
2998 /* --------------------------------------------------------------------------------------------- */
2999 /** Return TRUE on success */
3002 edit_save_block_cmd (WEdit
* edit
)
3004 off_t start_mark
, end_mark
;
3006 gboolean ret
= FALSE
;
3008 if (eval_marks (edit
, &start_mark
, &end_mark
))
3011 tmp
= mc_config_get_full_path (EDIT_CLIP_FILE
);
3013 input_expand_dialog (_("Save block"), _("Enter file name:"),
3014 MC_HISTORY_EDIT_SAVE_BLOCK
, tmp
);
3016 edit_push_undo_action (edit
, KEY_PRESS
+ edit
->start_display
);
3018 if (exp
!= NULL
&& *exp
!= '\0')
3020 if (edit_save_block (edit
, exp
, start_mark
, end_mark
))
3023 edit_error_dialog (_("Save block"), get_sys_error (_("Cannot save file")));
3025 edit
->force
|= REDRAW_COMPLETELY
;
3034 /* --------------------------------------------------------------------------------------------- */
3036 /** returns TRUE on success */
3038 edit_insert_file_cmd (WEdit
* edit
)
3042 gboolean ret
= FALSE
;
3044 tmp
= mc_config_get_full_path (EDIT_CLIP_FILE
);
3045 exp
= input_expand_dialog (_("Insert file"), _("Enter file name:"),
3046 MC_HISTORY_EDIT_INSERT_FILE
, tmp
);
3049 edit_push_undo_action (edit
, KEY_PRESS
+ edit
->start_display
);
3051 if (exp
!= NULL
&& *exp
!= '\0')
3053 vfs_path_t
*exp_vpath
;
3055 exp_vpath
= vfs_path_from_str (exp
);
3056 ret
= (edit_insert_file (edit
, exp_vpath
) >= 0);
3057 vfs_path_free (exp_vpath
);
3060 edit_error_dialog (_("Insert file"), get_sys_error (_("Cannot insert file")));
3065 edit
->force
|= REDRAW_COMPLETELY
;
3069 /* --------------------------------------------------------------------------------------------- */
3070 /** sorts a block, returns -1 on system fail, 1 on cancel and 0 on success */
3073 edit_sort_cmd (WEdit
* edit
)
3075 static char *old
= 0;
3076 char *exp
, *tmp
, *tmp_edit_block_name
, *tmp_edit_temp_name
;
3077 off_t start_mark
, end_mark
;
3080 if (eval_marks (edit
, &start_mark
, &end_mark
))
3082 edit_error_dialog (_("Sort block"), _("You must first highlight a block of text"));
3086 tmp
= mc_config_get_full_path (EDIT_BLOCK_FILE
);
3087 edit_save_block (edit
, tmp
, start_mark
, end_mark
);
3090 exp
= input_dialog (_("Run sort"),
3091 _("Enter sort options (see manpage) separated by whitespace:"),
3092 MC_HISTORY_EDIT_SORT
, (old
!= NULL
) ? old
: "");
3098 tmp_edit_block_name
= mc_config_get_full_path (EDIT_BLOCK_FILE
);
3099 tmp_edit_temp_name
= mc_config_get_full_path (EDIT_TEMP_FILE
);
3101 g_strconcat (" sort ", exp
, " ", tmp_edit_block_name
,
3102 " > ", tmp_edit_temp_name
, (char *) NULL
);
3103 g_free (tmp_edit_temp_name
);
3104 g_free (tmp_edit_block_name
);
3110 if (e
== -1 || e
== 127)
3112 edit_error_dialog (_("Sort"), get_sys_error (_("Cannot execute sort command")));
3117 sprintf (q
, "%d ", e
);
3118 tmp
= g_strdup_printf (_("Sort returned non-zero: %s"), q
);
3119 edit_error_dialog (_("Sort"), tmp
);
3125 edit
->force
|= REDRAW_COMPLETELY
;
3127 if (edit_block_delete_cmd (edit
))
3131 vfs_path_t
*tmp_vpath
;
3133 tmp_vpath
= mc_config_get_full_vpath (EDIT_TEMP_FILE
);
3134 edit_insert_file (edit
, tmp_vpath
);
3135 vfs_path_free (tmp_vpath
);
3140 /* --------------------------------------------------------------------------------------------- */
3142 * Ask user for a command, execute it and paste its output back to the
3147 edit_ext_cmd (WEdit
* edit
)
3149 char *exp
, *tmp
, *tmp_edit_temp_file
;
3153 input_dialog (_("Paste output of external command"),
3154 _("Enter shell command(s):"), MC_HISTORY_EDIT_PASTE_EXTCMD
, NULL
);
3159 tmp_edit_temp_file
= mc_config_get_full_path (EDIT_TEMP_FILE
);
3160 tmp
= g_strconcat (exp
, " > ", tmp_edit_temp_file
, (char *) NULL
);
3161 g_free (tmp_edit_temp_file
);
3168 edit_error_dialog (_("External command"), get_sys_error (_("Cannot execute command")));
3172 edit
->force
|= REDRAW_COMPLETELY
;
3175 vfs_path_t
*tmp_vpath
;
3177 tmp_vpath
= mc_config_get_full_vpath (EDIT_TEMP_FILE
);
3178 edit_insert_file (edit
, tmp_vpath
);
3179 vfs_path_free (tmp_vpath
);
3184 /* --------------------------------------------------------------------------------------------- */
3185 /** if block is 1, a block must be highlighted and the shell command
3186 processes it. If block is 0 the shell command is a straight system
3187 command, that just produces some output which is to be inserted */
3190 edit_block_process_cmd (WEdit
* edit
, int macro_number
)
3193 char *macros_fname
= NULL
;
3195 fname
= g_strdup_printf ("%s.%i.sh", MC_EXTMACRO_FILE
, macro_number
);
3196 macros_fname
= g_build_filename (mc_config_get_data_path (), fname
, (char *) NULL
);
3197 user_menu (edit
, macros_fname
, 0);
3199 g_free (macros_fname
);
3200 edit
->force
|= REDRAW_COMPLETELY
;
3203 /* --------------------------------------------------------------------------------------------- */
3206 edit_mail_dialog (WEdit
* edit
)
3209 char *tmail_subject
;
3212 static char *mail_cc_last
= 0;
3213 static char *mail_subject_last
= 0;
3214 static char *mail_to_last
= 0;
3216 QuickWidget quick_widgets
[] = {
3217 /* 0 */ QUICK_BUTTON (6, 10, 9, MAIL_DLG_HEIGHT
, N_("&Cancel"), B_CANCEL
, NULL
),
3218 /* 1 */ QUICK_BUTTON (2, 10, 9, MAIL_DLG_HEIGHT
, N_("&OK"), B_ENTER
, NULL
),
3219 /* 2 */ QUICK_INPUT (3, 50, 8, MAIL_DLG_HEIGHT
, "", 44, 0, "mail-dlg-input", &tmail_cc
),
3220 /* 3 */ QUICK_LABEL (3, 50, 7, MAIL_DLG_HEIGHT
, N_("Copies to")),
3221 /* 4 */ QUICK_INPUT (3, 50, 6, MAIL_DLG_HEIGHT
, "", 44, 0, "mail-dlg-input-2",
3223 /* 5 */ QUICK_LABEL (3, 50, 5, MAIL_DLG_HEIGHT
, N_("Subject")),
3224 /* 6 */ QUICK_INPUT (3, 50, 4, MAIL_DLG_HEIGHT
, "", 44, 0, "mail-dlg-input-3", &tmail_to
),
3225 /* 7 */ QUICK_LABEL (3, 50, 3, MAIL_DLG_HEIGHT
, N_("To")),
3226 /* 8 */ QUICK_LABEL (3, 50, 2, MAIL_DLG_HEIGHT
, N_("mail -s <subject> -c <cc> <to>")),
3230 QuickDialog Quick_input
= {
3231 50, MAIL_DLG_HEIGHT
, -1, -1, N_("Mail"),
3232 "[Input Line Keys]", quick_widgets
, NULL
, FALSE
3235 quick_widgets
[2].u
.input
.text
= mail_cc_last
? mail_cc_last
: "";
3236 quick_widgets
[4].u
.input
.text
= mail_subject_last
? mail_subject_last
: "";
3237 quick_widgets
[6].u
.input
.text
= mail_to_last
? mail_to_last
: "";
3239 if (quick_dialog (&Quick_input
) != B_CANCEL
)
3241 g_free (mail_cc_last
);
3242 g_free (mail_subject_last
);
3243 g_free (mail_to_last
);
3244 mail_cc_last
= tmail_cc
;
3245 mail_subject_last
= tmail_subject
;
3246 mail_to_last
= tmail_to
;
3247 pipe_mail (edit
, mail_to_last
, mail_subject_last
, mail_cc_last
);
3252 /*******************/
3253 /* Word Completion */
3254 /*******************/
3256 /* --------------------------------------------------------------------------------------------- */
3258 * Complete current word using regular expression search
3259 * backwards beginning at the current cursor position.
3263 edit_complete_word_cmd (WEdit
* edit
)
3265 gsize i
, max_len
, word_len
= 0, num_compl
= 0;
3266 off_t word_start
= 0;
3267 unsigned char *bufpos
;
3269 struct selection
compl[MAX_WORD_COMPLETIONS
]; /* completions */
3271 /* search start of word to be completed */
3272 if (!edit_find_word_start (edit
, &word_start
, &word_len
))
3275 /* prepare match expression */
3276 bufpos
= &edit
->buffers1
[word_start
>> S_EDIT_BUF_SIZE
][word_start
& M_EDIT_BUF_SIZE
];
3278 /* match_expr = g_strdup_printf ("\\b%.*s[a-zA-Z_0-9]+", word_len, bufpos); */
3281 ("(^|\\s+|\\b)%.*s[^\\s\\.=\\+\\[\\]\\(\\)\\,\\;\\:\\\"\\'\\-\\?\\/\\|\\\\\\{\\}\\*\\&\\^\\%%\\$#@\\!]+",
3282 (int) word_len
, bufpos
);
3284 /* collect the possible completions */
3285 /* start search from begin to end of file */
3287 edit_collect_completions (edit
, word_start
, word_len
, match_expr
,
3288 (struct selection
*) &compl, &num_compl
);
3292 /* insert completed word if there is only one match */
3295 for (i
= word_len
; i
< compl[0].len
; i
++)
3296 edit_insert (edit
, *(compl[0].text
+ i
));
3298 /* more than one possible completion => ask the user */
3301 /* !!! usually only a beep is expected and when <ALT-TAB> is !!! */
3302 /* !!! pressed again the selection dialog pops up, but that !!! */
3303 /* !!! seems to require a further internal state !!! */
3306 /* let the user select the preferred completion */
3307 editcmd_dialog_completion_show (edit
, max_len
, word_len
,
3308 (struct selection
*) &compl, num_compl
);
3312 g_free (match_expr
);
3313 /* release memory before return */
3314 for (i
= 0; i
< num_compl
; i
++)
3315 g_free (compl[i
].text
);
3318 /* --------------------------------------------------------------------------------------------- */
3321 edit_select_codepage_cmd (WEdit
* edit
)
3324 if (do_select_codepage ())
3325 edit_set_codeset (edit
);
3327 edit
->force
= REDRAW_COMPLETELY
;
3328 edit_refresh_cmd (edit
);
3334 /* --------------------------------------------------------------------------------------------- */
3337 edit_insert_literal_cmd (WEdit
* edit
)
3339 int char_for_insertion
;
3341 char_for_insertion
= editcmd_dialog_raw_key_query (_("Insert literal"),
3342 _("Press any key:"), FALSE
);
3343 edit_execute_key_command (edit
, -1, ascii_alpha_to_cntrl (char_for_insertion
));
3346 /* --------------------------------------------------------------------------------------------- */
3349 edit_begin_end_macro_cmd (WEdit
* edit
)
3351 /* edit is a pointer to the widget */
3354 unsigned long command
= macro_index
< 0 ? CK_MacroStartRecord
: CK_MacroStopRecord
;
3355 edit_execute_key_command (edit
, command
, -1);
3359 /* --------------------------------------------------------------------------------------------- */
3362 edit_begin_end_repeat_cmd (WEdit
* edit
)
3364 /* edit is a pointer to the widget */
3367 unsigned long command
= macro_index
< 0 ? CK_RepeatStartRecord
: CK_RepeatStopRecord
;
3368 edit_execute_key_command (edit
, command
, -1);
3372 /* --------------------------------------------------------------------------------------------- */
3375 edit_load_forward_cmd (WEdit
* edit
)
3378 && edit_query_dialog2 (_("Warning"),
3379 _("Current text was modified without a file save.\n"
3380 "Continue discards these changes"), _("C&ontinue"),
3383 edit
->force
|= REDRAW_COMPLETELY
;
3387 if (edit_stack_iterator
+ 1 >= MAX_HISTORY_MOVETO
)
3390 if (edit_history_moveto
[edit_stack_iterator
+ 1].line
< 1)
3393 edit_stack_iterator
++;
3394 if (edit_history_moveto
[edit_stack_iterator
].filename_vpath
!= NULL
)
3395 return edit_reload_line (edit
, edit_history_moveto
[edit_stack_iterator
].filename_vpath
,
3396 edit_history_moveto
[edit_stack_iterator
].line
);
3401 /* --------------------------------------------------------------------------------------------- */
3404 edit_load_back_cmd (WEdit
* edit
)
3407 && edit_query_dialog2 (_("Warning"),
3408 _("Current text was modified without a file save.\n"
3409 "Continue discards these changes"), _("C&ontinue"),
3412 edit
->force
|= REDRAW_COMPLETELY
;
3416 /* we are in the bottom of the stack, NO WAY! */
3417 if (edit_stack_iterator
== 0)
3420 edit_stack_iterator
--;
3421 if (edit_history_moveto
[edit_stack_iterator
].filename_vpath
!= NULL
)
3422 return edit_reload_line (edit
, edit_history_moveto
[edit_stack_iterator
].filename_vpath
,
3423 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 off_t 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 /* --------------------------------------------------------------------------------------------- */