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/charsets.h"
64 #include "lib/event.h" /* mc_event_raise() */
66 #include "src/history.h"
67 #include "src/setup.h" /* option_tab_spacing */
68 #include "src/main.h" /* mactos_t */
69 #include "src/selcodepage.h"
70 #include "src/keybind-defaults.h"
71 #include "src/util.h" /* check_for_default() */
72 #include "src/filemanager/layout.h" /* mc_refresh() */
74 #include "edit-impl.h"
75 #include "editwidget.h"
76 #include "editcmd_dialogs.h"
79 /*** global variables ****************************************************************************/
81 /* search and replace: */
82 int search_create_bookmark
= FALSE
;
84 /* queries on a save */
85 int edit_confirm_save
= 1;
87 /*** file scope macro definitions ****************************************************************/
91 #define TEMP_BUF_LEN 1024
95 /* thanks to Liviu Daia <daia@stoilow.imar.ro> for getting this
96 (and the above) routines to work properly - paul */
98 #define is_digit(x) ((x) >= '0' && (x) <= '9')
100 #define MAIL_DLG_HEIGHT 12
102 #define MAX_WORD_COMPLETIONS 100 /* in listbox */
104 /*** file scope type declarations ****************************************************************/
106 /*** file scope variables ************************************************************************/
108 /*** file scope functions ************************************************************************/
109 /* --------------------------------------------------------------------------------------------- */
111 /* If 0 (quick save) then a) create/truncate <filename> file,
112 b) save to <filename>;
113 if 1 (safe save) then a) save to <tempnam>,
114 b) rename <tempnam> to <filename>;
115 if 2 (do backups) then a) save to <tempnam>,
116 b) rename <filename> to <filename.backup_ext>,
117 c) rename <tempnam> to <filename>. */
119 /* returns 0 on error, -1 on abort */
122 edit_save_file (WEdit
* edit
, const vfs_path_t
* filename_vpath
)
127 int this_save_mode
, fd
= -1;
128 vfs_path_t
*real_filename_vpath
;
129 vfs_path_t
*savename_vpath
= NULL
;
130 const char *start_filename
;
131 const vfs_path_element_t
*vpath_element
;
133 vpath_element
= vfs_path_get_by_index (filename_vpath
, 0);
134 if (vpath_element
== NULL
)
137 start_filename
= vpath_element
->path
;
138 if (*start_filename
== '\0')
141 if (*start_filename
!= PATH_SEP
&& edit
->dir_vpath
!= NULL
)
143 real_filename_vpath
= vfs_path_append_vpath_new (edit
->dir_vpath
, filename_vpath
, NULL
);
147 real_filename_vpath
= vfs_path_clone (filename_vpath
);
150 this_save_mode
= option_save_mode
;
151 if (this_save_mode
!= EDIT_QUICK_SAVE
)
153 if (!vfs_file_is_local (real_filename_vpath
)
154 || (fd
= mc_open (real_filename_vpath
, O_RDONLY
| O_BINARY
)) == -1)
157 * The file does not exists yet, so no safe save or
158 * backup are necessary.
160 this_save_mode
= EDIT_QUICK_SAVE
;
166 if (this_save_mode
== EDIT_QUICK_SAVE
&& !edit
->skip_detach_prompt
)
171 rv
= mc_stat (real_filename_vpath
, &sb
);
172 if (rv
== 0 && sb
.st_nlink
> 1)
174 rv
= edit_query_dialog3 (_("Warning"),
175 _("File has hard-links. Detach before saving?"),
176 _("&Yes"), _("&No"), _("&Cancel"));
180 this_save_mode
= EDIT_SAFE_SAVE
;
183 edit
->skip_detach_prompt
= 1;
186 vfs_path_free (real_filename_vpath
);
191 /* Prevent overwriting changes from other editor sessions. */
192 if (rv
== 0 && edit
->stat1
.st_mtime
!= 0 && edit
->stat1
.st_mtime
!= sb
.st_mtime
)
194 /* The default action is "Cancel". */
197 rv
= edit_query_dialog2 (_("Warning"),
198 _("The file has been modified in the meantime. Save anyway?"),
199 _("&Yes"), _("&Cancel"));
202 vfs_path_free (real_filename_vpath
);
208 if (this_save_mode
!= EDIT_QUICK_SAVE
)
210 char *savedir
, *saveprefix
;
212 savedir
= vfs_path_tokens_get (real_filename_vpath
, 0, -1);
214 savedir
= g_strdup (".");
216 saveprefix
= mc_build_filename (savedir
, "cooledit", NULL
);
218 fd
= mc_mkstemps (&savename_vpath
, saveprefix
, NULL
);
220 if (savename_vpath
== NULL
)
222 vfs_path_free (real_filename_vpath
);
226 * Close for now because mc_mkstemps use pure open system call
227 * to create temporary file and it needs to be reopened by
228 * VFS-aware mc_open().
233 savename_vpath
= vfs_path_clone (real_filename_vpath
);
235 (void) mc_chown (savename_vpath
, edit
->stat1
.st_uid
, edit
->stat1
.st_gid
);
236 (void) mc_chmod (savename_vpath
, edit
->stat1
.st_mode
);
238 fd
= mc_open (savename_vpath
, O_CREAT
| O_WRONLY
| O_TRUNC
| O_BINARY
, edit
->stat1
.st_mode
);
243 p
= edit_get_write_filter (savename_vpath
, real_filename_vpath
);
249 file
= (FILE *) popen (p
, "w");
253 filelen
= edit_write_stream (edit
, file
);
257 if (pclose (file
) != 0)
259 tmp
= g_strdup_printf (_("Error writing to pipe: %s"), p
);
260 edit_error_dialog (_("Error"), tmp
);
269 tmp
= g_strdup_printf (_("Cannot open pipe for writing: %s"), p
);
270 edit_error_dialog (_("Error"), get_sys_error (tmp
));
277 else if (edit
->lb
== LB_ASIS
)
278 { /* do not change line breaks */
281 filelen
= edit
->last_byte
;
282 while (buf
<= (edit
->curs1
>> S_EDIT_BUF_SIZE
) - 1)
284 if (mc_write (fd
, (char *) edit
->buffers1
[buf
], EDIT_BUF_SIZE
) != EDIT_BUF_SIZE
)
292 (fd
, (char *) edit
->buffers1
[buf
],
293 edit
->curs1
& M_EDIT_BUF_SIZE
) != (edit
->curs1
& M_EDIT_BUF_SIZE
))
297 else if (edit
->curs2
)
300 buf
= (edit
->curs2
>> S_EDIT_BUF_SIZE
);
303 (char *) edit
->buffers2
[buf
] + EDIT_BUF_SIZE
-
304 (edit
->curs2
& M_EDIT_BUF_SIZE
) - 1,
305 1 + (edit
->curs2
& M_EDIT_BUF_SIZE
)) != 1 + (edit
->curs2
& M_EDIT_BUF_SIZE
))
313 if (mc_write (fd
, (char *) edit
->buffers2
[buf
], EDIT_BUF_SIZE
) != EDIT_BUF_SIZE
)
325 /* Update the file information, especially the mtime. */
326 if (mc_stat (savename_vpath
, &edit
->stat1
) == -1)
330 { /* change line breaks */
332 const vfs_path_element_t
*path_element
;
336 path_element
= vfs_path_get_by_index (savename_vpath
, -1);
337 file
= (FILE *) fopen (path_element
->path
, "w");
340 filelen
= edit_write_stream (edit
, file
);
347 msg
= g_strdup_printf (_("Cannot open file for writing: %s"), path_element
->path
);
348 edit_error_dialog (_("Error"), msg
);
354 if (filelen
!= edit
->last_byte
)
357 if (this_save_mode
== EDIT_DO_BACKUP
)
359 vfs_path_t
*tmp_vpath
;
363 assert (option_backup_ext
!= NULL
);
365 tmp_vpath
= vfs_path_append_new (real_filename_vpath
, option_backup_ext
, (char *) NULL
);
366 ok
= (mc_rename (real_filename_vpath
, tmp_vpath
) != -1);
367 vfs_path_free (tmp_vpath
);
372 if (this_save_mode
!= EDIT_QUICK_SAVE
)
373 if (mc_rename (savename_vpath
, real_filename_vpath
) == -1)
376 vfs_path_free (real_filename_vpath
);
377 vfs_path_free (savename_vpath
);
380 /* FIXME: Is this safe ?
381 * if (this_save_mode != EDIT_QUICK_SAVE)
382 * mc_unlink (savename);
384 vfs_path_free (real_filename_vpath
);
385 vfs_path_free (savename_vpath
);
389 /* --------------------------------------------------------------------------------------------- */
392 edit_check_newline (WEdit
* edit
)
394 return !(option_check_nl_at_eof
&& edit
->last_byte
> 0
395 && edit_get_byte (edit
, edit
->last_byte
- 1) != '\n'
396 && edit_query_dialog2 (_("Warning"),
397 _("The file you are saving is not finished with a newline"),
398 _("C&ontinue"), _("&Cancel")));
401 /* --------------------------------------------------------------------------------------------- */
404 edit_get_save_file_as (WEdit
* edit
)
407 #define DLG_HEIGHT 14
409 static LineBreaks cur_lb
= LB_ASIS
;
411 char *filename
= vfs_path_to_str (edit
->filename_vpath
);
414 const char *lb_names
[LB_NAMES
] = {
415 N_("&Do not change"),
416 N_("&Unix format (LF)"),
417 N_("&Windows/DOS format (CR LF)"),
418 N_("&Macintosh format (CR)")
421 QuickWidget quick_widgets
[] = {
422 QUICK_BUTTON (6, 10, DLG_HEIGHT
- 3, DLG_HEIGHT
, N_("&Cancel"), B_CANCEL
, NULL
),
423 QUICK_BUTTON (2, 10, DLG_HEIGHT
- 3, DLG_HEIGHT
, N_("&OK"), B_ENTER
, NULL
),
424 QUICK_RADIO (5, DLG_WIDTH
, DLG_HEIGHT
- 8, DLG_HEIGHT
, LB_NAMES
, lb_names
, (int *) &cur_lb
),
425 QUICK_LABEL (3, DLG_WIDTH
, DLG_HEIGHT
- 9, DLG_HEIGHT
, N_("Change line breaks to:")),
426 QUICK_INPUT (3, DLG_WIDTH
, DLG_HEIGHT
- 11, DLG_HEIGHT
, filename
, DLG_WIDTH
- 6, 0,
427 "save-as", &filename_res
),
428 QUICK_LABEL (3, DLG_WIDTH
, DLG_HEIGHT
- 12, DLG_HEIGHT
, N_("Enter file name:")),
432 QuickDialog Quick_options
= {
433 DLG_WIDTH
, DLG_HEIGHT
, -1, -1,
434 N_("Save As"), "[Save File As]",
435 quick_widgets
, NULL
, NULL
, FALSE
438 if (quick_dialog (&Quick_options
) != B_CANCEL
)
441 vfs_path_t
*ret_vpath
;
444 fname
= tilde_expand (filename_res
);
445 g_free (filename_res
);
446 ret_vpath
= vfs_path_from_str (fname
);
458 /* --------------------------------------------------------------------------------------------- */
460 /** returns 1 on success */
463 edit_save_cmd (WEdit
* edit
)
465 int res
, save_lock
= 0;
467 if (!edit
->locked
&& !edit
->delete_file
)
468 save_lock
= lock_file (edit
->filename_vpath
);
469 res
= edit_save_file (edit
, edit
->filename_vpath
);
471 /* Maintain modify (not save) lock on failure */
472 if ((res
> 0 && edit
->locked
) || save_lock
)
473 edit
->locked
= unlock_file (edit
->filename_vpath
);
475 /* On failure try 'save as', it does locking on its own */
477 return edit_save_as_cmd (edit
);
478 edit
->force
|= REDRAW_COMPLETELY
;
481 edit
->delete_file
= 0;
488 /* --------------------------------------------------------------------------------------------- */
492 * @param edit widget object
493 * @param exp_vpath vfs file path
494 * @return TRUE if file content was successfully loaded, FALSE otherwise
498 edit_load_file_from_filename (WEdit
* edit
, const vfs_path_t
* exp_vpath
)
500 int prev_locked
= edit
->locked
;
501 vfs_path_t
*prev_filename
;
504 prev_filename
= vfs_path_clone (edit
->filename_vpath
);
505 if (!edit_reload (edit
, exp_vpath
))
507 else if (prev_locked
)
508 unlock_file (prev_filename
);
510 vfs_path_free (prev_filename
);
514 /* --------------------------------------------------------------------------------------------- */
517 edit_load_syntax_file (WEdit
* edit
)
519 vfs_path_t
*extdir_vpath
;
524 dir
= query_dialog (_("Syntax file edit"),
525 _("Which syntax file you want to edit?"), D_NORMAL
, 2,
526 _("&User"), _("&System Wide"));
530 vfs_path_build_filename (mc_global
.sysconfig_dir
, "syntax", "Syntax", (char *) NULL
);
531 if (!exist_file (vfs_path_get_last_path_str (extdir_vpath
)))
533 vfs_path_free (extdir_vpath
);
535 vfs_path_build_filename (mc_global
.share_data_dir
, "syntax", "Syntax", (char *) NULL
);
540 vfs_path_t
*user_syntax_file_vpath
;
542 user_syntax_file_vpath
= mc_config_get_full_vpath (EDIT_SYNTAX_FILE
);
543 check_for_default (extdir_vpath
, user_syntax_file_vpath
);
544 edit_load_file_from_filename (edit
, user_syntax_file_vpath
);
545 vfs_path_free (user_syntax_file_vpath
);
548 edit_load_file_from_filename (edit
, extdir_vpath
);
550 vfs_path_free (extdir_vpath
);
553 /* --------------------------------------------------------------------------------------------- */
556 edit_load_menu_file (WEdit
* edit
)
558 vfs_path_t
*buffer_vpath
;
559 vfs_path_t
*menufile_vpath
;
562 dir
= query_dialog (_("Menu edit"),
563 _("Which menu file do you want to edit?"), D_NORMAL
,
564 geteuid () != 0 ? 2 : 3, _("&Local"), _("&User"), _("&System Wide"));
566 menufile_vpath
= vfs_path_build_filename (mc_global
.sysconfig_dir
, EDIT_GLOBAL_MENU
, NULL
);
568 if (!exist_file (vfs_path_get_last_path_str (menufile_vpath
)))
570 vfs_path_free (menufile_vpath
);
571 menufile_vpath
= vfs_path_build_filename (mc_global
.share_data_dir
, EDIT_GLOBAL_MENU
, NULL
);
577 buffer_vpath
= vfs_path_from_str (EDIT_LOCAL_MENU
);
578 check_for_default (menufile_vpath
, buffer_vpath
);
579 chmod (vfs_path_get_last_path_str (buffer_vpath
), 0600);
583 buffer_vpath
= mc_config_get_full_vpath (EDIT_HOME_MENU
);
584 check_for_default (menufile_vpath
, buffer_vpath
);
588 buffer_vpath
= vfs_path_build_filename (mc_global
.sysconfig_dir
, EDIT_GLOBAL_MENU
, NULL
);
589 if (!exist_file (vfs_path_get_last_path_str (buffer_vpath
)))
591 vfs_path_free (buffer_vpath
);
593 vfs_path_build_filename (mc_global
.share_data_dir
, EDIT_GLOBAL_MENU
, NULL
);
598 vfs_path_free (menufile_vpath
);
602 edit_load_file_from_filename (edit
, buffer_vpath
);
604 vfs_path_free (buffer_vpath
);
605 vfs_path_free (menufile_vpath
);
608 /* --------------------------------------------------------------------------------------------- */
611 edit_delete_column_of_text (WEdit
* edit
)
613 long p
, q
, r
, m1
, m2
;
616 eval_marks (edit
, &m1
, &m2
);
617 n
= edit_move_forward (edit
, m1
, 0, m2
) + 1;
618 c
= edit_move_forward3 (edit
, edit_bol (edit
, m1
), 0, m1
);
619 d
= edit_move_forward3 (edit
, edit_bol (edit
, m2
), 0, m2
);
620 b
= max (min (c
, d
), min (edit
->column1
, edit
->column2
));
621 c
= max (c
, max (edit
->column1
, edit
->column2
));
625 r
= edit_bol (edit
, edit
->curs1
);
626 p
= edit_move_forward3 (edit
, r
, b
, 0);
627 q
= edit_move_forward3 (edit
, r
, c
, 0);
632 edit_cursor_move (edit
, p
- edit
->curs1
);
635 /* delete line between margins */
636 if (edit_get_byte (edit
, edit
->curs1
) != '\n')
637 edit_delete (edit
, 1);
641 /* move to next line except on the last delete */
642 edit_cursor_move (edit
, edit_move_forward (edit
, edit
->curs1
, 1, 0) - edit
->curs1
);
646 /* --------------------------------------------------------------------------------------------- */
647 /** if success return 0 */
650 edit_block_delete (WEdit
* edit
)
653 long start_mark
, end_mark
;
654 int curs_pos
, line_width
;
655 long curs_line
, c1
, c2
;
657 if (eval_marks (edit
, &start_mark
, &end_mark
))
659 if (edit
->column_highlight
&& edit
->mark2
< 0)
660 edit_mark_cmd (edit
, 0);
661 if ((end_mark
- start_mark
) > option_max_undo
/ 2)
663 /* Warning message with a query to continue or cancel the operation */
664 if (edit_query_dialog2
667 ("Block is large, you may not be able to undo this action"),
668 _("C&ontinue"), _("&Cancel")))
673 c1
= min (edit
->column1
, edit
->column2
);
674 c2
= max (edit
->column1
, edit
->column2
);
678 edit_push_markers (edit
);
680 curs_line
= edit
->curs_line
;
682 /* calculate line width and cursor position before cut */
683 line_width
= edit_move_forward3 (edit
, edit_bol (edit
, edit
->curs1
), 0,
684 edit_eol (edit
, edit
->curs1
));
685 curs_pos
= edit
->curs_col
+ edit
->over_col
;
687 /* move cursor to start of selection */
688 edit_cursor_move (edit
, start_mark
- edit
->curs1
);
689 edit_scroll_screen_over_cursor (edit
);
691 if (start_mark
< end_mark
)
693 if (edit
->column_highlight
)
696 edit_mark_cmd (edit
, 0);
697 edit_delete_column_of_text (edit
);
698 /* move cursor to the saved position */
699 edit_move_to_line (edit
, curs_line
);
700 /* calculate line width after cut */
701 line_width
= edit_move_forward3 (edit
, edit_bol (edit
, edit
->curs1
), 0,
702 edit_eol (edit
, edit
->curs1
));
703 if (option_cursor_beyond_eol
&& curs_pos
> line_width
)
704 edit
->over_col
= curs_pos
- line_width
;
708 while (count
< end_mark
)
710 edit_delete (edit
, 1);
715 edit_set_markers (edit
, 0, 0, 0, 0);
716 edit
->force
|= REDRAW_PAGE
;
720 /* --------------------------------------------------------------------------------------------- */
722 * Get EOL symbol for searching.
724 * @param edit editor object
729 edit_search_get_current_end_line_char (const WEdit
* edit
)
740 /* --------------------------------------------------------------------------------------------- */
742 * Checking if search condition have BOL(^) or EOL ($) regexp special characters.
744 * @param search search object
745 * @return result of checks.
748 static edit_search_line_t
749 edit_get_search_line_type (mc_search_t
* search
)
751 edit_search_line_t search_line_type
= 0;
753 if (search
->search_type
!= MC_SEARCH_T_REGEX
)
754 return search_line_type
;
756 if (*search
->original
== '^')
757 search_line_type
|= AT_START_LINE
;
759 if (search
->original
[search
->original_len
- 1] == '$')
760 search_line_type
|= AT_END_LINE
;
761 return search_line_type
;
764 /* --------------------------------------------------------------------------------------------- */
766 * Calculating the start position of next line.
768 * @param edit editor object
769 * @param current_pos current position
770 * @param max_pos max position
771 * @param end_string_symbol end of line symbol
772 * @return start position of next line
776 edit_calculate_start_of_next_line (WEdit
* edit
, off_t current_pos
, off_t max_pos
,
777 char end_string_symbol
)
781 for (i
= current_pos
; i
< max_pos
; i
++)
784 if (edit_get_byte (edit
, i
) == end_string_symbol
)
791 /* --------------------------------------------------------------------------------------------- */
793 * Calculating the end position of previous line.
795 * @param edit editor object
796 * @param current_pos current position
797 * @param end_string_symbol end of line symbol
798 * @return end position of previous line
802 edit_calculate_end_of_previous_line (WEdit
* edit
, off_t current_pos
, char end_string_symbol
)
806 for (i
= current_pos
- 1; i
>= 0; i
--)
807 if (edit_get_byte (edit
, i
) == end_string_symbol
)
813 /* --------------------------------------------------------------------------------------------- */
815 * Calculating the start position of previous line.
817 * @param edit editor object
818 * @param current_pos current position
819 * @param end_string_symbol end of line symbol
820 * @return start position of previous line
824 edit_calculate_start_of_previous_line (WEdit
* edit
, off_t current_pos
, char end_string_symbol
)
826 current_pos
= edit_calculate_end_of_previous_line (edit
, current_pos
, end_string_symbol
);
827 current_pos
= edit_calculate_end_of_previous_line (edit
, current_pos
, end_string_symbol
);
829 return (current_pos
+ 1);
832 /* --------------------------------------------------------------------------------------------- */
834 * Calculating the start position of current line.
836 * @param edit editor object
837 * @param current_pos current position
838 * @param end_string_symbol end of line symbol
839 * @return start position of current line
843 edit_calculate_start_of_current_line (WEdit
* edit
, off_t current_pos
, char end_string_symbol
)
845 current_pos
= edit_calculate_end_of_previous_line (edit
, current_pos
, end_string_symbol
);
847 return (current_pos
+ 1);
850 /* --------------------------------------------------------------------------------------------- */
852 * Fixing (if needed) search start position if 'only in selection' option present.
854 * @param edit editor object
858 edit_search_fix_search_start_if_selection (WEdit
* edit
)
863 if (!edit_search_options
.only_in_selection
)
866 if (eval_marks (edit
, &start_mark
, &end_mark
) != 0)
869 if (edit_search_options
.backwards
)
871 if (edit
->search_start
> end_mark
|| edit
->search_start
<= start_mark
)
872 edit
->search_start
= end_mark
;
876 if (edit
->search_start
< start_mark
|| edit
->search_start
>= end_mark
)
877 edit
->search_start
= start_mark
;
881 /* --------------------------------------------------------------------------------------------- */
884 editcmd_find (WEdit
* edit
, gsize
* len
)
886 off_t search_start
= edit
->search_start
;
889 long end_mark
= edit
->last_byte
;
891 char end_string_symbol
;
893 end_string_symbol
= edit_search_get_current_end_line_char (edit
);
895 /* prepare for search */
896 if (edit_search_options
.only_in_selection
)
898 mark_res
= eval_marks (edit
, &start_mark
, &end_mark
);
901 edit
->search
->error
= MC_SEARCH_E_NOTFOUND
;
902 edit
->search
->error_str
= g_strdup (_("Search string not found"));
906 /* fix the start and the end of search block positions */
907 if ((edit
->search_line_type
& AT_START_LINE
) != 0
908 && (start_mark
!= 0 || edit_get_byte (edit
, start_mark
- 1) != end_string_symbol
))
911 edit_calculate_start_of_next_line (edit
, start_mark
, edit
->last_byte
,
914 if ((edit
->search_line_type
& AT_END_LINE
) != 0
915 && (end_mark
- 1 != edit
->last_byte
916 || edit_get_byte (edit
, end_mark
) != end_string_symbol
))
918 end_mark
= edit_calculate_end_of_previous_line (edit
, end_mark
, end_string_symbol
);
920 if (start_mark
>= end_mark
)
922 edit
->search
->error
= MC_SEARCH_E_NOTFOUND
;
923 edit
->search
->error_str
= g_strdup (_("Search string not found"));
929 if (edit_search_options
.backwards
)
930 end_mark
= max (1, edit
->curs1
) - 1;
934 if (edit_search_options
.backwards
)
936 /* backward search */
937 search_end
= end_mark
;
939 if ((edit
->search_line_type
& AT_START_LINE
) != 0)
941 edit_calculate_start_of_current_line (edit
, search_start
, end_string_symbol
);
943 while ((int) search_start
>= start_mark
)
945 if (search_end
> (off_t
) (search_start
+ edit
->search
->original_len
)
946 && mc_search_is_fixed_search_str (edit
->search
))
948 search_end
= search_start
+ edit
->search
->original_len
;
950 if (mc_search_run (edit
->search
, (void *) edit
, search_start
, search_end
, len
)
951 && edit
->search
->normal_offset
== search_start
)
957 if ((edit
->search_line_type
& AT_START_LINE
) != 0)
959 edit_calculate_start_of_previous_line (edit
, search_start
, end_string_symbol
);
963 edit
->search
->error_str
= g_strdup (_("Search string not found"));
968 if ((edit
->search_line_type
& AT_START_LINE
) != 0 && search_start
!= start_mark
)
970 edit_calculate_start_of_next_line (edit
, search_start
, end_mark
, end_string_symbol
);
971 return mc_search_run (edit
->search
, (void *) edit
, search_start
, end_mark
, len
);
976 /* --------------------------------------------------------------------------------------------- */
979 edit_replace_cmd__conv_to_display (char *str
)
984 tmp
= str_convert_to_display (str
);
988 return g_string_free (tmp
, FALSE
);
989 g_string_free (tmp
, TRUE
);
992 return g_strdup (str
);
995 /* --------------------------------------------------------------------------------------------- */
998 edit_replace_cmd__conv_to_input (char *str
)
1003 tmp
= str_convert_to_input (str
);
1007 return g_string_free (tmp
, FALSE
);
1008 g_string_free (tmp
, TRUE
);
1011 return g_strdup (str
);
1014 /* --------------------------------------------------------------------------------------------- */
1017 edit_do_search (WEdit
* edit
)
1021 if (edit
->search
== NULL
)
1022 edit
->search_start
= edit
->curs1
;
1024 edit_push_undo_action (edit
, KEY_PRESS
+ edit
->start_display
);
1026 if (search_create_bookmark
)
1028 int found
= 0, books
= 0;
1029 long l
= 0, l_last
= -1;
1032 search_create_bookmark
= FALSE
;
1033 book_mark_flush (edit
, -1);
1037 if (!mc_search_run (edit
->search
, (void *) edit
, q
, edit
->last_byte
, &len
))
1040 edit
->search_start
= edit
->search
->normal_offset
;
1042 l
+= edit_count_lines (edit
, q
, edit
->search
->normal_offset
);
1045 book_mark_insert (edit
, l
, BOOK_MARK_FOUND_COLOR
);
1049 q
= edit
->search
->normal_offset
+ 1;
1053 edit_error_dialog (_("Search"), _("Search string not found"));
1055 edit_cursor_move (edit
, edit
->search_start
- edit
->curs1
);
1059 if (edit
->found_len
!= 0 && edit
->search_start
== edit
->found_start
+ 1
1060 && edit_search_options
.backwards
)
1061 edit
->search_start
--;
1063 if (edit
->found_len
!= 0 && edit
->search_start
== edit
->found_start
- 1
1064 && !edit_search_options
.backwards
)
1065 edit
->search_start
++;
1067 if (editcmd_find (edit
, &len
))
1069 edit
->found_start
= edit
->search_start
= edit
->search
->normal_offset
;
1070 edit
->found_len
= len
;
1072 edit_cursor_move (edit
, edit
->search_start
- edit
->curs1
);
1073 edit_scroll_screen_over_cursor (edit
);
1074 if (edit_search_options
.backwards
)
1075 edit
->search_start
--;
1077 edit
->search_start
++;
1081 edit
->search_start
= edit
->curs1
;
1082 if (edit
->search
->error_str
!= NULL
)
1083 edit_error_dialog (_("Search"), edit
->search
->error_str
);
1087 edit
->force
|= REDRAW_COMPLETELY
;
1088 edit_scroll_screen_over_cursor (edit
);
1091 /* --------------------------------------------------------------------------------------------- */
1094 edit_search (WEdit
* edit
)
1096 if (editcmd_dialog_search_show (edit
))
1098 edit
->search_line_type
= edit_get_search_line_type (edit
->search
);
1099 edit_search_fix_search_start_if_selection (edit
);
1100 edit_do_search (edit
);
1104 /* --------------------------------------------------------------------------------------------- */
1105 /** Return a null terminated length of text. Result must be g_free'd */
1107 static unsigned char *
1108 edit_get_block (WEdit
* edit
, long start
, long finish
, int *l
)
1110 unsigned char *s
, *r
;
1111 r
= s
= g_malloc0 (finish
- start
+ 1);
1112 if (edit
->column_highlight
)
1115 /* copy from buffer, excluding chars that are out of the column 'margins' */
1116 while (start
< finish
)
1120 x
= edit_move_forward3 (edit
, edit_bol (edit
, start
), 0, start
);
1121 c
= edit_get_byte (edit
, start
);
1122 if ((x
>= edit
->column1
&& x
< edit
->column2
)
1123 || (x
>= edit
->column2
&& x
< edit
->column1
) || c
== '\n')
1133 *l
= finish
- start
;
1134 while (start
< finish
)
1135 *s
++ = edit_get_byte (edit
, start
++);
1141 /* --------------------------------------------------------------------------------------------- */
1142 /** copies a block to clipboard file */
1145 edit_save_block_to_clip_file (WEdit
* edit
, long start
, long finish
)
1149 tmp
= mc_config_get_full_path (EDIT_CLIP_FILE
);
1150 ret
= edit_save_block (edit
, tmp
, start
, finish
);
1155 /* --------------------------------------------------------------------------------------------- */
1158 pipe_mail (WEdit
* edit
, char *to
, char *subject
, char *cc
)
1163 to
= name_quote (to
, 0);
1164 subject
= name_quote (subject
, 0);
1165 cc
= name_quote (cc
, 0);
1166 s
= g_strconcat ("mail -s ", subject
, *cc
? " -c " : "", cc
, " ", to
, (char *) NULL
);
1180 for (i
= 0; i
< edit
->last_byte
; i
++)
1181 fputc (edit_get_byte (edit
, i
), p
);
1186 /* --------------------------------------------------------------------------------------------- */
1189 is_break_char (char c
)
1191 return (isspace (c
) || strchr ("{}[]()<>=|/\\!?~-+`'\",.;:#$%^&*", c
));
1194 /* --------------------------------------------------------------------------------------------- */
1195 /** find first character of current word */
1198 edit_find_word_start (WEdit
* edit
, long *word_start
, gsize
* word_len
)
1203 /* return if at begin of file */
1204 if (edit
->curs1
<= 0)
1207 c
= (unsigned char) edit_get_byte (edit
, edit
->curs1
- 1);
1208 /* return if not at end or in word */
1209 if (is_break_char (c
))
1212 /* search start of word to be completed */
1215 /* return if at begin of file */
1216 if ((gsize
) edit
->curs1
< i
)
1220 c
= (unsigned char) edit_get_byte (edit
, edit
->curs1
- i
);
1222 if (is_break_char (c
))
1224 /* return if word starts with digit */
1228 *word_start
= edit
->curs1
- (i
- 1); /* start found */
1237 /* --------------------------------------------------------------------------------------------- */
1239 * Get current word under cursor
1241 * @param edit editor object
1242 * @param srch mc_search object
1243 * @param word_start start word position
1245 * @return newly allocated string or NULL if no any words under cursor
1249 edit_collect_completions_get_current_word (WEdit
* edit
, mc_search_t
* srch
, long word_start
)
1254 if (!mc_search_run (srch
, (void *) edit
, word_start
, edit
->last_byte
, &len
))
1257 temp
= g_string_sized_new (len
);
1259 for (i
= 0; i
< len
; i
++)
1263 chr
= edit_get_byte (edit
, word_start
+ i
);
1265 g_string_append_c (temp
, chr
);
1268 return g_string_free (temp
, temp
->len
== 0);
1271 /* --------------------------------------------------------------------------------------------- */
1272 /** collect the possible completions */
1275 edit_collect_completions (WEdit
* edit
, long word_start
, gsize word_len
,
1276 char *match_expr
, struct selection
*compl, gsize
* num
)
1284 long last_byte
, start
= -1;
1287 srch
= mc_search_new (match_expr
, -1);
1291 if (mc_config_get_bool
1292 (mc_main_config
, CONFIG_APP_SECTION
, "editor_wordcompletion_collect_entire_file", 0))
1294 last_byte
= edit
->last_byte
;
1298 last_byte
= word_start
;
1301 srch
->search_type
= MC_SEARCH_T_REGEX
;
1302 srch
->is_case_sensitive
= TRUE
;
1303 srch
->search_fn
= edit_search_cmd_callback
;
1305 current_word
= edit_collect_completions_get_current_word (edit
, srch
, word_start
);
1307 temp
= g_string_new ("");
1309 /* collect max MAX_WORD_COMPLETIONS completions */
1310 while (mc_search_run (srch
, (void *) edit
, start
+ 1, last_byte
, &len
))
1312 g_string_set_size (temp
, 0);
1313 start
= srch
->normal_offset
;
1315 /* add matched completion if not yet added */
1316 for (i
= 0; i
< len
; i
++)
1318 skip
= edit_get_byte (edit
, start
+ i
);
1322 /* skip current word */
1323 if (start
+ (long) i
== word_start
)
1326 g_string_append_c (temp
, skip
);
1332 if (current_word
!= NULL
&& strcmp (current_word
, temp
->str
) == 0)
1337 for (i
= 0; i
< *num
; i
++)
1340 ((char *) &compl[i
].text
[word_len
],
1341 (char *) &temp
->str
[word_len
], max (len
, compl[i
].len
) - word_len
) == 0)
1343 struct selection
this = compl[i
];
1344 for (++i
; i
< *num
; i
++)
1346 compl[i
- 1] = compl[i
];
1348 compl[*num
- 1] = this;
1350 break; /* skip it, already added */
1356 if (*num
== MAX_WORD_COMPLETIONS
)
1358 g_free (compl[0].text
);
1359 for (i
= 1; i
< *num
; i
++)
1361 compl[i
- 1] = compl[i
];
1368 recoded
= str_convert_to_display (temp
->str
);
1370 if (recoded
&& recoded
->len
)
1371 g_string_assign (temp
, recoded
->str
);
1373 g_string_free (recoded
, TRUE
);
1376 compl[*num
].text
= g_strdup (temp
->str
);
1377 compl[*num
].len
= temp
->len
;
1381 /* note the maximal length needed for the completion dialog */
1386 mc_search_free (srch
);
1387 g_string_free (temp
, TRUE
);
1388 g_free (current_word
);
1393 /* --------------------------------------------------------------------------------------------- */
1396 edit_insert_column_of_text (WEdit
* edit
, unsigned char *data
, int size
, int width
,
1397 long *start_pos
, long *end_pos
, int *col1
, int *col2
)
1402 cursor
= edit
->curs1
;
1403 col
= edit_get_col (edit
);
1405 for (i
= 0; i
< size
; i
++)
1407 if (data
[i
] != '\n')
1408 edit_insert (edit
, data
[i
]);
1410 { /* fill in and move to next line */
1414 if (edit_get_byte (edit
, edit
->curs1
) != '\n')
1416 l
= width
- (edit_get_col (edit
) - col
);
1419 edit_insert (edit
, ' ');
1423 for (p
= edit
->curs1
;; p
++)
1425 if (p
== edit
->last_byte
)
1427 edit_cursor_move (edit
, edit
->last_byte
- edit
->curs1
);
1428 edit_insert_ahead (edit
, '\n');
1432 if (edit_get_byte (edit
, p
) == '\n')
1438 edit_cursor_move (edit
, edit_move_forward3 (edit
, p
, col
, 0) - edit
->curs1
);
1439 l
= col
- edit_get_col (edit
);
1440 while (l
>= space_width
)
1442 edit_insert (edit
, ' ');
1449 *col2
= col
+ width
;
1450 *start_pos
= cursor
;
1451 *end_pos
= edit
->curs1
;
1452 edit_cursor_move (edit
, cursor
- edit
->curs1
);
1455 /* --------------------------------------------------------------------------------------------- */
1458 edit_macro_comparator (gconstpointer
* macro1
, gconstpointer
* macro2
)
1460 const macros_t
*m1
= (const macros_t
*) macro1
;
1461 const macros_t
*m2
= (const macros_t
*) macro2
;
1463 return m1
->hotkey
- m2
->hotkey
;
1466 /* --------------------------------------------------------------------------------------------- */
1469 edit_macro_sort_by_hotkey (void)
1471 if (macros_list
!= NULL
&& macros_list
->len
!= 0)
1472 g_array_sort (macros_list
, (GCompareFunc
) edit_macro_comparator
);
1475 /* --------------------------------------------------------------------------------------------- */
1478 edit_get_macro (WEdit
* edit
, int hotkey
, const macros_t
** macros
, guint
* indx
)
1480 const macros_t
*array_start
= &g_array_index (macros_list
, struct macros_t
, 0);
1482 macros_t search_macro
;
1486 search_macro
.hotkey
= hotkey
;
1487 result
= bsearch (&search_macro
, macros_list
->data
, macros_list
->len
,
1488 sizeof (macros_t
), (GCompareFunc
) edit_macro_comparator
);
1490 if (result
!= NULL
&& result
->macro
!= NULL
)
1492 *indx
= (result
- array_start
);
1500 /* --------------------------------------------------------------------------------------------- */
1501 /** returns FALSE on error */
1504 edit_delete_macro (WEdit
* edit
, int hotkey
)
1506 mc_config_t
*macros_config
= NULL
;
1507 const char *section_name
= "editor";
1508 gchar
*macros_fname
;
1511 const macros_t
*macros
= NULL
;
1513 /* clear array of actions for current hotkey */
1514 while (edit_get_macro (edit
, hotkey
, ¯os
, &indx
))
1516 if (macros
->macro
!= NULL
)
1517 g_array_free (macros
->macro
, TRUE
);
1519 g_array_remove_index (macros_list
, indx
);
1520 edit_macro_sort_by_hotkey ();
1523 macros_fname
= mc_config_get_full_path (MC_MACRO_FILE
);
1524 macros_config
= mc_config_init (macros_fname
);
1525 g_free (macros_fname
);
1527 if (macros_config
== NULL
)
1530 skeyname
= lookup_key_by_code (hotkey
);
1531 while (mc_config_del_key (macros_config
, section_name
, skeyname
))
1534 mc_config_save_file (macros_config
, NULL
);
1535 mc_config_deinit (macros_config
);
1540 /* --------------------------------------------------------------------------------------------- */
1541 /*** public functions ****************************************************************************/
1542 /* --------------------------------------------------------------------------------------------- */
1545 edit_refresh_cmd (WEdit
* edit
)
1550 edit_get_syntax_color (edit
, -1, &color
);
1551 tty_touch_screen ();
1558 #endif /* !HAVE_SLANG */
1562 /* --------------------------------------------------------------------------------------------- */
1565 edit_save_mode_cmd (void)
1568 const int DLG_X
= 38;
1569 const int DLG_Y
= 13;
1573 const char *str
[] = {
1576 N_("&Do backups with following extension:")
1579 QuickWidget widgets
[] = {
1581 QUICK_BUTTON (18, DLG_X
, DLG_Y
- 3, DLG_Y
, N_("&Cancel"), B_CANCEL
, NULL
),
1583 QUICK_BUTTON (6, DLG_X
, DLG_Y
- 3, DLG_Y
, N_("&OK"), B_ENTER
, NULL
),
1585 QUICK_CHECKBOX (4, DLG_X
, 8, DLG_Y
, N_("Check &POSIX new line"), &option_check_nl_at_eof
),
1587 QUICK_INPUT (8, DLG_X
, 6, DLG_Y
, option_backup_ext
, 9, 0, "edit-backup-ext", &str_result
),
1589 QUICK_RADIO (4, DLG_X
, 3, DLG_Y
, 3, str
, &option_save_mode
),
1593 QuickDialog dialog
= {
1594 DLG_X
, DLG_Y
, -1, -1, N_("Edit Save Mode"),
1595 "[Edit Save Mode]", widgets
, NULL
, NULL
, FALSE
1600 size_t w0
, w1
, b_len
, w3
;
1602 #ifdef HAVE_ASSERT_H
1603 assert (option_backup_ext
!= NULL
);
1606 /* OK/Cancel buttons */
1607 w0
= str_term_width1 (_(widgets
[0].u
.button
.text
)) + 3;
1608 w1
= str_term_width1 (_(widgets
[1].u
.button
.text
)) + 5; /* default button */
1609 b_len
= w0
+ w1
+ 3;
1611 maxlen
= max (b_len
, (size_t) str_term_width1 (_(dialog
.title
)) + 2);
1614 for (i
= 0; i
< 3; i
++)
1619 w3
= max (w3
, (size_t) str_term_width1 (str
[i
]));
1622 maxlen
= max (maxlen
, w3
+ 4);
1624 dialog
.xlen
= min ((size_t) COLS
, maxlen
+ 8);
1626 widgets
[3].u
.input
.len
= w3
;
1627 widgets
[1].relative_x
= (dialog
.xlen
- b_len
) / 2;
1628 widgets
[0].relative_x
= widgets
[1].relative_x
+ w0
+ 2;
1630 for (i
= 0; i
< sizeof (widgets
) / sizeof (widgets
[0]); i
++)
1631 widgets
[i
].x_divisions
= dialog
.xlen
;
1633 if (quick_dialog (&dialog
) != B_CANCEL
)
1635 g_free (option_backup_ext
);
1636 option_backup_ext
= str_result
;
1640 /* --------------------------------------------------------------------------------------------- */
1643 edit_set_filename (WEdit
* edit
, const vfs_path_t
* name_vpath
)
1645 vfs_path_free (edit
->filename_vpath
);
1646 edit
->filename_vpath
= vfs_path_clone (name_vpath
);
1648 if (edit
->dir_vpath
== NULL
)
1649 edit
->dir_vpath
= vfs_path_clone (vfs_get_raw_current_dir ());
1652 /* --------------------------------------------------------------------------------------------- */
1653 /* Here we want to warn the users of overwriting an existing file,
1654 but only if they have made a change to the filename */
1655 /* returns 1 on success */
1657 edit_save_as_cmd (WEdit
* edit
)
1659 /* This heads the 'Save As' dialog box */
1660 vfs_path_t
*exp_vpath
;
1662 int different_filename
= 0;
1664 if (!edit_check_newline (edit
))
1667 exp_vpath
= edit_get_save_file_as (edit
);
1668 edit_push_undo_action (edit
, KEY_PRESS
+ edit
->start_display
);
1670 if (exp_vpath
!= NULL
)
1672 if (vfs_path_len (exp_vpath
) == 0)
1678 if (vfs_path_cmp (edit
->filename_vpath
, exp_vpath
) != 0)
1683 if (mc_stat (exp_vpath
, &sb
) == 0 && !S_ISREG (sb
.st_mode
))
1685 edit_error_dialog (_("Save as"),
1687 ("Cannot save: destination is not a regular file")));
1691 different_filename
= 1;
1692 file
= mc_open (exp_vpath
, O_RDONLY
| O_BINARY
);
1696 /* the file exists */
1698 /* Overwrite the current file or cancel the operation */
1699 if (edit_query_dialog2
1701 _("A file already exists with this name"), _("&Overwrite"), _("&Cancel")))
1706 edit
->stat1
.st_mode
|= S_IWUSR
;
1708 save_lock
= lock_file (exp_vpath
);
1712 /* filenames equal, check if already locked */
1713 if (!edit
->locked
&& !edit
->delete_file
)
1714 save_lock
= lock_file (exp_vpath
);
1717 if (different_filename
)
1720 * Allow user to write into saved (under another name) file
1721 * even if original file had r/o user permissions.
1723 edit
->stat1
.st_mode
|= S_IWRITE
;
1726 rv
= edit_save_file (edit
, exp_vpath
);
1730 /* Succesful, so unlock both files */
1731 if (different_filename
)
1734 unlock_file (exp_vpath
);
1736 edit
->locked
= unlock_file (edit
->filename_vpath
);
1740 if (edit
->locked
|| save_lock
)
1741 edit
->locked
= unlock_file (edit
->filename_vpath
);
1744 edit_set_filename (edit
, exp_vpath
);
1745 if (edit
->lb
!= LB_ASIS
)
1746 edit_reload (edit
, exp_vpath
);
1748 edit
->delete_file
= 0;
1749 if (different_filename
)
1750 edit_load_syntax (edit
, NULL
, edit
->syntax_type
);
1751 vfs_path_free (exp_vpath
);
1752 edit
->force
|= REDRAW_COMPLETELY
;
1755 edit_error_dialog (_("Save as"), get_sys_error (_("Cannot save file")));
1758 /* Failed, so maintain modify (not save) lock */
1760 unlock_file (exp_vpath
);
1767 vfs_path_free (exp_vpath
);
1768 edit
->force
|= REDRAW_COMPLETELY
;
1772 /* {{{ Macro stuff starts here */
1773 /* --------------------------------------------------------------------------------------------- */
1776 edit_delete_macro_cmd (WEdit
* edit
)
1780 hotkey
= editcmd_dialog_raw_key_query (_("Delete macro"), _("Press macro hotkey:"), 1);
1782 if (hotkey
!= 0 && !edit_delete_macro (edit
, hotkey
))
1783 message (D_ERROR
, _("Delete macro"), _("Macro not deleted"));
1786 /* --------------------------------------------------------------------------------------------- */
1788 /** returns FALSE on error */
1790 edit_execute_macro (WEdit
* edit
, int hotkey
)
1792 gboolean res
= FALSE
;
1796 const macros_t
*macros
;
1799 if (edit_get_macro (edit
, hotkey
, ¯os
, &indx
) &&
1800 macros
->macro
!= NULL
&& macros
->macro
->len
!= 0)
1804 edit
->force
|= REDRAW_PAGE
;
1806 for (i
= 0; i
< macros
->macro
->len
; i
++)
1808 const macro_action_t
*m_act
;
1810 m_act
= &g_array_index (macros
->macro
, struct macro_action_t
, i
);
1811 edit_execute_cmd (edit
, m_act
->action
, m_act
->ch
);
1820 /* --------------------------------------------------------------------------------------------- */
1822 /** returns FALSE on error */
1824 edit_store_macro_cmd (WEdit
* edit
)
1828 GString
*marcros_string
;
1829 mc_config_t
*macros_config
= NULL
;
1830 const char *section_name
= "editor";
1831 gchar
*macros_fname
;
1832 GArray
*macros
; /* current macro */
1834 gboolean have_macro
= FALSE
;
1835 char *skeyname
= NULL
;
1837 hotkey
= editcmd_dialog_raw_key_query (_("Save macro"), _("Press the macro's new hotkey:"), 1);
1838 if (hotkey
== ESC_CHAR
)
1841 tmp_act
= keybind_lookup_keymap_command (editor_map
, hotkey
);
1843 /* return FALSE if try assign macro into restricted hotkeys */
1844 if (tmp_act
== CK_MacroStartRecord
1845 || tmp_act
== CK_MacroStopRecord
|| tmp_act
== CK_MacroStartStopRecord
)
1848 edit_delete_macro (edit
, hotkey
);
1850 macros_fname
= mc_config_get_full_path (MC_MACRO_FILE
);
1851 macros_config
= mc_config_init (macros_fname
);
1852 g_free (macros_fname
);
1854 if (macros_config
== NULL
)
1857 edit_push_undo_action (edit
, KEY_PRESS
+ edit
->start_display
);
1859 marcros_string
= g_string_sized_new (250);
1860 macros
= g_array_new (TRUE
, FALSE
, sizeof (macro_action_t
));
1862 skeyname
= lookup_key_by_code (hotkey
);
1864 for (i
= 0; i
< macro_index
; i
++)
1866 macro_action_t m_act
;
1867 const char *action_name
;
1869 action_name
= keybind_lookup_actionname (record_macro_buf
[i
].action
);
1871 if (action_name
== NULL
)
1874 m_act
.action
= record_macro_buf
[i
].action
;
1875 m_act
.ch
= record_macro_buf
[i
].ch
;
1876 g_array_append_val (macros
, m_act
);
1878 g_string_append_printf (marcros_string
, "%s:%i;", action_name
,
1879 (int) record_macro_buf
[i
].ch
);
1884 macro
.hotkey
= hotkey
;
1885 macro
.macro
= macros
;
1886 g_array_append_val (macros_list
, macro
);
1887 mc_config_set_string (macros_config
, section_name
, skeyname
, marcros_string
->str
);
1890 mc_config_del_key (macros_config
, section_name
, skeyname
);
1893 edit_macro_sort_by_hotkey ();
1895 g_string_free (marcros_string
, TRUE
);
1896 mc_config_save_file (macros_config
, NULL
);
1897 mc_config_deinit (macros_config
);
1901 /* --------------------------------------------------------------------------------------------- */
1904 edit_repeat_macro_cmd (WEdit
* edit
)
1911 f
= input_dialog (_("Repeat last commands"), _("Repeat times:"), MC_HISTORY_EDIT_REPEAT
, NULL
);
1912 if (f
== NULL
|| *f
== '\0')
1918 count_repeat
= strtol (f
, &error
, 0);
1928 edit_push_undo_action (edit
, KEY_PRESS
+ edit
->start_display
);
1929 edit
->force
|= REDRAW_PAGE
;
1931 for (j
= 0; j
< count_repeat
; j
++)
1932 for (i
= 0; i
< macro_index
; i
++)
1933 edit_execute_cmd (edit
, record_macro_buf
[i
].action
, record_macro_buf
[i
].ch
);
1934 edit_update_screen (edit
);
1938 /* --------------------------------------------------------------------------------------------- */
1939 /** return FALSE on error */
1942 edit_load_macro_cmd (WEdit
* edit
)
1944 mc_config_t
*macros_config
= NULL
;
1945 gchar
**profile_keys
, **keys
;
1946 gchar
**values
, **curr_values
;
1947 gsize len
, values_len
;
1948 const char *section_name
= "editor";
1949 gchar
*macros_fname
;
1954 macros_fname
= mc_config_get_full_path (MC_MACRO_FILE
);
1955 macros_config
= mc_config_init (macros_fname
);
1956 g_free (macros_fname
);
1958 if (macros_config
== NULL
)
1961 profile_keys
= keys
= mc_config_get_keys (macros_config
, section_name
, &len
);
1962 while (*profile_keys
!= NULL
)
1964 gboolean have_macro
;
1968 macros
= g_array_new (TRUE
, FALSE
, sizeof (macro_action_t
));
1970 curr_values
= values
= mc_config_get_string_list (macros_config
, section_name
,
1971 *profile_keys
, &values_len
);
1972 hotkey
= lookup_key (*profile_keys
, NULL
);
1975 while (*curr_values
!= NULL
&& *curr_values
[0] != '\0')
1977 char **macro_pair
= NULL
;
1979 macro_pair
= g_strsplit (*curr_values
, ":", 2);
1981 if (macro_pair
!= NULL
)
1983 macro_action_t m_act
;
1984 if (macro_pair
[0] == NULL
|| macro_pair
[0][0] == '\0')
1988 m_act
.action
= keybind_lookup_action (macro_pair
[0]);
1989 g_free (macro_pair
[0]);
1990 macro_pair
[0] = NULL
;
1992 if (macro_pair
[1] == NULL
|| macro_pair
[1][0] == '\0')
1996 m_act
.ch
= strtol (macro_pair
[1], NULL
, 0);
1997 g_free (macro_pair
[1]);
1998 macro_pair
[1] = NULL
;
2000 if (m_act
.action
!= 0)
2002 /* a shell command */
2003 if ((m_act
.action
/ CK_PipeBlock (0)) == 1)
2005 m_act
.action
= CK_PipeBlock (0) + (m_act
.ch
> 0 ? m_act
.ch
: 0);
2008 g_array_append_val (macros
, m_act
);
2011 g_strfreev (macro_pair
);
2018 macro
.hotkey
= hotkey
;
2019 macro
.macro
= macros
;
2020 g_array_append_val (macros_list
, macro
);
2023 g_strfreev (values
);
2026 mc_config_deinit (macros_config
);
2027 edit_macro_sort_by_hotkey ();
2031 /* }}} Macro stuff end here */
2033 /* --------------------------------------------------------------------------------------------- */
2034 /** returns 1 on success */
2037 edit_save_confirm_cmd (WEdit
* edit
)
2041 if (edit
->filename_vpath
== NULL
)
2042 return edit_save_as_cmd (edit
);
2044 if (!edit_check_newline (edit
))
2047 if (edit_confirm_save
)
2052 filename
= vfs_path_to_str (edit
->filename_vpath
);
2053 f
= g_strdup_printf (_("Confirm save file: \"%s\""), filename
);
2055 ok
= (edit_query_dialog2 (_("Save file"), f
, _("&Save"), _("&Cancel")) == 0);
2060 return edit_save_cmd (edit
);
2063 /* --------------------------------------------------------------------------------------------- */
2065 /* returns TRUE on success */
2067 edit_new_cmd (WEdit
* edit
)
2070 && edit_query_dialog2 (_("Warning"),
2071 _("Current text was modified without a file save.\n"
2072 "Continue discards these changes"),
2073 _("C&ontinue"), _("&Cancel")) == 1)
2075 edit
->force
|= REDRAW_COMPLETELY
;
2079 edit
->force
|= REDRAW_COMPLETELY
;
2081 return edit_renew (edit
); /* if this gives an error, something has really screwed up */
2084 /* --------------------------------------------------------------------------------------------- */
2087 edit_load_cmd (WEdit
* edit
, edit_current_file_t what
)
2089 gboolean ret
= TRUE
;
2092 && edit_query_dialog2 (_("Warning"),
2093 _("Current text was modified without a file save.\n"
2094 "Continue discards these changes"), _("C&ontinue"),
2097 edit
->force
|= REDRAW_COMPLETELY
;
2103 case EDIT_FILE_COMMON
:
2105 char *filename
, *exp
;
2107 filename
= vfs_path_to_str (edit
->filename_vpath
);
2108 exp
= input_expand_dialog (_("Load"), _("Enter file name:"),
2109 MC_HISTORY_EDIT_LOAD
, filename
);
2112 if (exp
!= NULL
&& *exp
!= '\0')
2114 vfs_path_t
*exp_vpath
;
2116 exp_vpath
= vfs_path_from_str (exp
);
2117 ret
= edit_load_file_from_filename (edit
, exp_vpath
);
2118 vfs_path_free (exp_vpath
);
2125 case EDIT_FILE_SYNTAX
:
2126 edit_load_syntax_file (edit
);
2129 case EDIT_FILE_MENU
:
2130 edit_load_menu_file (edit
);
2137 edit
->force
|= REDRAW_COMPLETELY
;
2141 /* --------------------------------------------------------------------------------------------- */
2143 * Close window with opened file.
2145 * @returns TRUE if file was closed.
2149 edit_close_cmd (WEdit
* edit
)
2153 ret
= (edit
!= NULL
) && edit_ok_to_exit (edit
);
2157 Dlg_head
*h
= ((Widget
*) edit
)->owner
;
2159 if (edit
->locked
!= 0)
2160 unlock_file (edit
->filename_vpath
);
2164 if (edit_widget_is_editor ((Widget
*) h
->current
->data
))
2165 edit
= (WEdit
*) h
->current
->data
;
2168 edit
= find_editor (h
);
2170 dlg_set_top_widget (edit
);
2175 edit
->force
|= REDRAW_COMPLETELY
;
2180 /* --------------------------------------------------------------------------------------------- */
2182 if mark2 is -1 then marking is from mark1 to the cursor.
2183 Otherwise its between the markers. This handles this.
2184 Returns 1 if no text is marked.
2188 eval_marks (WEdit
* edit
, long *start_mark
, long *end_mark
)
2190 if (edit
->mark1
!= edit
->mark2
)
2192 long start_bol
, start_eol
;
2193 long end_bol
, end_eol
;
2198 if (edit
->end_mark_curs
< 0)
2199 end_mark_curs
= edit
->curs1
;
2201 end_mark_curs
= edit
->end_mark_curs
;
2203 if (edit
->mark2
>= 0)
2205 *start_mark
= min (edit
->mark1
, edit
->mark2
);
2206 *end_mark
= max (edit
->mark1
, edit
->mark2
);
2210 *start_mark
= min (edit
->mark1
, end_mark_curs
);
2211 *end_mark
= max (edit
->mark1
, end_mark_curs
);
2212 edit
->column2
= edit
->curs_col
+ edit
->over_col
;
2215 if (edit
->column_highlight
2216 && (((edit
->mark1
> end_mark_curs
) && (edit
->column1
< edit
->column2
))
2217 || ((edit
->mark1
< end_mark_curs
) && (edit
->column1
> edit
->column2
))))
2219 start_bol
= edit_bol (edit
, *start_mark
);
2220 start_eol
= edit_eol (edit
, start_bol
- 1) + 1;
2221 end_bol
= edit_bol (edit
, *end_mark
);
2222 end_eol
= edit_eol (edit
, *end_mark
);
2223 col1
= min (edit
->column1
, edit
->column2
);
2224 col2
= max (edit
->column1
, edit
->column2
);
2227 edit_move_forward3 (edit
, start_bol
, col2
, 0) - edit_move_forward3 (edit
, start_bol
,
2230 edit_move_forward3 (edit
, end_bol
, col2
, 0) - edit_move_forward3 (edit
, end_bol
,
2233 *start_mark
-= diff1
;
2235 *start_mark
= max (*start_mark
, start_eol
);
2236 *end_mark
= min (*end_mark
, end_eol
);
2242 *start_mark
= *end_mark
= 0;
2243 edit
->column2
= edit
->column1
= 0;
2248 /* --------------------------------------------------------------------------------------------- */
2251 edit_insert_over (WEdit
* edit
)
2255 for (i
= 0; i
< edit
->over_col
; i
++)
2257 edit_insert (edit
, ' ');
2262 /* --------------------------------------------------------------------------------------------- */
2265 edit_insert_column_of_text_from_file (WEdit
* edit
, int file
,
2266 long *start_pos
, long *end_pos
, int *col1
, int *col2
)
2270 int blocklen
= -1, width
= 0;
2271 unsigned char *data
;
2273 cursor
= edit
->curs1
;
2274 col
= edit_get_col (edit
);
2275 data
= g_malloc0 (TEMP_BUF_LEN
);
2277 while ((blocklen
= mc_read (file
, (char *) data
, TEMP_BUF_LEN
)) > 0)
2280 for (width
= 0; width
< blocklen
; width
++)
2282 if (data
[width
] == '\n')
2285 for (i
= 0; i
< blocklen
; i
++)
2287 if (data
[i
] == '\n')
2288 { /* fill in and move to next line */
2291 if (edit_get_byte (edit
, edit
->curs1
) != '\n')
2293 l
= width
- (edit_get_col (edit
) - col
);
2296 edit_insert (edit
, ' ');
2300 for (p
= edit
->curs1
;; p
++)
2302 if (p
== edit
->last_byte
)
2304 edit_cursor_move (edit
, edit
->last_byte
- edit
->curs1
);
2305 edit_insert_ahead (edit
, '\n');
2309 if (edit_get_byte (edit
, p
) == '\n')
2315 edit_cursor_move (edit
, edit_move_forward3 (edit
, p
, col
, 0) - edit
->curs1
);
2316 l
= col
- edit_get_col (edit
);
2317 while (l
>= space_width
)
2319 edit_insert (edit
, ' ');
2324 edit_insert (edit
, data
[i
]);
2328 *col2
= col
+ width
;
2329 *start_pos
= cursor
;
2330 *end_pos
= edit
->curs1
;
2331 edit_cursor_move (edit
, cursor
- edit
->curs1
);
2337 /* --------------------------------------------------------------------------------------------- */
2340 edit_block_copy_cmd (WEdit
* edit
)
2342 long start_mark
, end_mark
, current
= edit
->curs1
;
2347 unsigned char *copy_buf
;
2349 edit_update_curs_col (edit
);
2350 if (eval_marks (edit
, &start_mark
, &end_mark
))
2353 copy_buf
= edit_get_block (edit
, start_mark
, end_mark
, &size
);
2355 /* all that gets pushed are deletes hence little space is used on the stack */
2357 edit_push_markers (edit
);
2359 if (edit
->column_highlight
)
2361 col_delta
= abs (edit
->column2
- edit
->column1
);
2362 edit_insert_column_of_text (edit
, copy_buf
, size
, col_delta
, &mark1
, &mark2
, &c1
, &c2
);
2367 edit_insert_ahead (edit
, copy_buf
[size
]);
2371 edit_scroll_screen_over_cursor (edit
);
2373 if (edit
->column_highlight
)
2374 edit_set_markers (edit
, edit
->curs1
, mark2
, c1
, c2
);
2375 else if (start_mark
< current
&& end_mark
> current
)
2376 edit_set_markers (edit
, start_mark
, end_mark
+ end_mark
- start_mark
, 0, 0);
2378 edit
->force
|= REDRAW_PAGE
;
2382 /* --------------------------------------------------------------------------------------------- */
2385 edit_block_move_cmd (WEdit
* edit
)
2388 unsigned char *copy_buf
= NULL
;
2389 long start_mark
, end_mark
;
2392 if (eval_marks (edit
, &start_mark
, &end_mark
))
2395 line
= edit
->curs_line
;
2396 if (edit
->mark2
< 0)
2397 edit_mark_cmd (edit
, 0);
2398 edit_push_markers (edit
);
2400 if (edit
->column_highlight
)
2408 c1
= min (edit
->column1
, edit
->column2
);
2409 c2
= max (edit
->column1
, edit
->column2
);
2410 b_width
= (c2
- c1
);
2412 edit_update_curs_col (edit
);
2415 x2
= x
+ edit
->over_col
;
2417 /* do nothing when cursor inside first line of selected area */
2418 if ((edit_eol (edit
, edit
->curs1
) == edit_eol (edit
, start_mark
)) && (x2
> c1
&& x2
<= c2
))
2421 if (edit
->curs1
> start_mark
&& edit
->curs1
< edit_eol (edit
, end_mark
))
2425 else if (x
> c1
&& x
<= c2
)
2428 /* save current selection into buffer */
2429 copy_buf
= edit_get_block (edit
, start_mark
, end_mark
, &size
);
2431 /* remove current selection */
2432 edit_block_delete_cmd (edit
);
2434 edit
->over_col
= max (0, edit
->over_col
- b_width
);
2435 /* calculate the cursor pos after delete block */
2436 current
= edit_move_forward3 (edit
, edit_bol (edit
, edit
->curs1
), x
, 0);
2437 edit_cursor_move (edit
, current
- edit
->curs1
);
2438 edit_scroll_screen_over_cursor (edit
);
2440 /* add TWS if need before block insertion */
2441 if (option_cursor_beyond_eol
&& edit
->over_col
> 0)
2442 edit_insert_over (edit
);
2444 edit_insert_column_of_text (edit
, copy_buf
, size
, b_width
, &mark1
, &mark2
, &c1
, &c2
);
2445 edit_set_markers (edit
, mark1
, mark2
, c1
, c2
);
2451 current
= edit
->curs1
;
2452 copy_buf
= g_malloc0 (end_mark
- start_mark
);
2453 edit_cursor_move (edit
, start_mark
- edit
->curs1
);
2454 edit_scroll_screen_over_cursor (edit
);
2456 while (count
< end_mark
)
2458 copy_buf
[end_mark
- count
- 1] = edit_delete (edit
, 1);
2461 edit_scroll_screen_over_cursor (edit
);
2462 edit_cursor_move (edit
,
2463 current
- edit
->curs1
-
2464 (((current
- edit
->curs1
) > 0) ? end_mark
- start_mark
: 0));
2465 edit_scroll_screen_over_cursor (edit
);
2466 while (count
-- > start_mark
)
2467 edit_insert_ahead (edit
, copy_buf
[end_mark
- count
- 1]);
2468 edit_set_markers (edit
, edit
->curs1
, edit
->curs1
+ end_mark
- start_mark
, 0, 0);
2471 edit_scroll_screen_over_cursor (edit
);
2473 edit
->force
|= REDRAW_PAGE
;
2476 /* --------------------------------------------------------------------------------------------- */
2477 /** returns 1 if canceelled by user */
2480 edit_block_delete_cmd (WEdit
* edit
)
2482 long start_mark
, end_mark
;
2483 if (eval_marks (edit
, &start_mark
, &end_mark
))
2485 edit_delete_line (edit
);
2488 return edit_block_delete (edit
);
2491 /* --------------------------------------------------------------------------------------------- */
2492 /** call with edit = 0 before shutdown to close memory leaks */
2495 edit_replace_cmd (WEdit
* edit
, int again
)
2497 /* 1 = search string, 2 = replace with */
2498 static char *saved1
= NULL
; /* saved default[123] */
2499 static char *saved2
= NULL
;
2500 char *input1
= NULL
; /* user input from the dialog */
2501 char *input2
= NULL
;
2502 GString
*input2_str
= NULL
;
2505 long times_replaced
= 0;
2506 gboolean once_found
= FALSE
;
2510 g_free (saved1
), saved1
= NULL
;
2511 g_free (saved2
), saved2
= NULL
;
2515 edit
->force
|= REDRAW_COMPLETELY
;
2517 if (again
&& !saved1
&& !saved2
)
2522 input1
= g_strdup (saved1
? saved1
: "");
2523 input2
= g_strdup (saved2
? saved2
: "");
2527 char *tmp_inp1
, *tmp_inp2
;
2529 disp1
= edit_replace_cmd__conv_to_display (saved1
? saved1
: (char *) "");
2530 disp2
= edit_replace_cmd__conv_to_display (saved2
? saved2
: (char *) "");
2532 edit_push_undo_action (edit
, KEY_PRESS
+ edit
->start_display
);
2534 editcmd_dialog_replace_show (edit
, disp1
, disp2
, &input1
, &input2
);
2539 if (input1
== NULL
|| *input1
== '\0')
2541 edit
->force
= REDRAW_COMPLETELY
;
2547 input1
= edit_replace_cmd__conv_to_input (input1
);
2548 input2
= edit_replace_cmd__conv_to_input (input2
);
2552 g_free (saved1
), saved1
= g_strdup (input1
);
2553 g_free (saved2
), saved2
= g_strdup (input2
);
2555 mc_search_free (edit
->search
);
2556 edit
->search
= NULL
;
2559 input2_str
= g_string_new (input2
);
2563 edit
->search
= mc_search_new (input1
, -1);
2564 if (edit
->search
== NULL
)
2566 edit
->search_start
= edit
->curs1
;
2569 edit
->search
->search_type
= edit_search_options
.type
;
2570 edit
->search
->is_all_charsets
= edit_search_options
.all_codepages
;
2571 edit
->search
->is_case_sensitive
= edit_search_options
.case_sens
;
2572 edit
->search
->whole_words
= edit_search_options
.whole_words
;
2573 edit
->search
->search_fn
= edit_search_cmd_callback
;
2574 edit
->search_line_type
= edit_get_search_line_type (edit
->search
);
2575 edit_search_fix_search_start_if_selection (edit
);
2578 if (edit
->found_len
&& edit
->search_start
== edit
->found_start
+ 1
2579 && edit_search_options
.backwards
)
2580 edit
->search_start
--;
2582 if (edit
->found_len
&& edit
->search_start
== edit
->found_start
- 1
2583 && !edit_search_options
.backwards
)
2584 edit
->search_start
++;
2590 if (!editcmd_find (edit
, &len
))
2592 if (!(edit
->search
->error
== MC_SEARCH_E_OK
||
2593 (once_found
&& edit
->search
->error
== MC_SEARCH_E_NOTFOUND
)))
2595 edit_error_dialog (_("Search"), edit
->search
->error_str
);
2601 edit
->search_start
= edit
->search
->normal_offset
;
2602 /*returns negative on not found or error in pattern */
2604 if ((edit
->search_start
>= 0) && (edit
->search_start
< edit
->last_byte
))
2609 edit
->found_start
= edit
->search_start
;
2610 i
= edit
->found_len
= len
;
2612 edit_cursor_move (edit
, edit
->search_start
- edit
->curs1
);
2613 edit_scroll_screen_over_cursor (edit
);
2615 if (edit
->replace_mode
== 0)
2620 l
= edit
->curs_row
- edit
->widget
.lines
/ 3;
2622 edit_scroll_downward (edit
, l
);
2624 edit_scroll_upward (edit
, -l
);
2626 edit_scroll_screen_over_cursor (edit
);
2627 edit
->force
|= REDRAW_PAGE
;
2628 edit_render_keypress (edit
);
2630 /*so that undo stops at each query */
2631 edit_push_key_press (edit
);
2632 /* and prompt 2/3 down */
2633 disp1
= edit_replace_cmd__conv_to_display (saved1
);
2634 disp2
= edit_replace_cmd__conv_to_display (saved2
);
2635 prompt
= editcmd_dialog_replace_prompt_show (edit
, disp1
, disp2
, -1, -1);
2639 if (prompt
== B_REPLACE_ALL
)
2640 edit
->replace_mode
= 1;
2641 else if (prompt
== B_SKIP_REPLACE
)
2643 if (edit_search_options
.backwards
)
2644 edit
->search_start
--;
2646 edit
->search_start
++;
2647 continue; /* loop */
2649 else if (prompt
== B_CANCEL
)
2651 edit
->replace_mode
= -1;
2656 repl_str
= mc_search_prepare_replace_str (edit
->search
, input2_str
);
2658 if (edit
->search
->error
!= MC_SEARCH_E_OK
)
2660 edit_error_dialog (_("Replace"), edit
->search
->error_str
);
2661 g_string_free (repl_str
, TRUE
);
2665 /* delete then insert new */
2666 for (i
= 0; i
< len
; i
++)
2667 edit_delete (edit
, 1);
2669 for (i
= 0; i
< repl_str
->len
; i
++)
2670 edit_insert (edit
, repl_str
->str
[i
]);
2672 edit
->found_len
= repl_str
->len
;
2673 g_string_free (repl_str
, TRUE
);
2676 /* so that we don't find the same string again */
2677 if (edit_search_options
.backwards
)
2679 edit
->search_start
--;
2683 edit
->search_start
+= edit
->found_len
+ (len
== 0 ? 1 : 0);
2685 if (edit
->search_start
>= edit
->last_byte
)
2689 edit_scroll_screen_over_cursor (edit
);
2693 /* try and find from right here for next search */
2694 edit
->search_start
= edit
->curs1
;
2695 edit_update_curs_col (edit
);
2697 edit
->force
|= REDRAW_PAGE
;
2698 edit_render_keypress (edit
);
2700 if (times_replaced
== 0)
2701 query_dialog (_("Replace"), _("Search string not found"), D_NORMAL
, 1, _("&OK"));
2705 while (edit
->replace_mode
>= 0);
2707 edit_scroll_screen_over_cursor (edit
);
2708 edit
->force
|= REDRAW_COMPLETELY
;
2709 edit_render_keypress (edit
);
2711 if ((edit
->replace_mode
== 1) && (times_replaced
!= 0))
2712 message (D_NORMAL
, _("Replace"), _("%ld replacements made"), times_replaced
);
2717 if (input2_str
!= NULL
)
2718 g_string_free (input2_str
, TRUE
);
2721 /* --------------------------------------------------------------------------------------------- */
2724 edit_search_cmd_callback (const void *user_data
, gsize char_offset
, int *current_char
)
2726 *current_char
= edit_get_byte ((WEdit
*) user_data
, (long) char_offset
);
2727 return MC_SEARCH_CB_OK
;
2730 /* --------------------------------------------------------------------------------------------- */
2733 edit_search_cmd (WEdit
* edit
, gboolean again
)
2741 else if (edit
->last_search_string
!= NULL
)
2742 edit_do_search (edit
);
2745 /* find last search string in history */
2748 history
= history_get (MC_HISTORY_SHARED_SEARCH
);
2749 if (history
!= NULL
&& history
->data
!= NULL
)
2751 edit
->last_search_string
= (char *) history
->data
;
2752 history
->data
= NULL
;
2753 history
= g_list_first (history
);
2754 g_list_foreach (history
, (GFunc
) g_free
, NULL
);
2755 g_list_free (history
);
2757 edit
->search
= mc_search_new (edit
->last_search_string
, -1);
2758 if (edit
->search
== NULL
)
2760 /* if not... then ask for an expression */
2761 g_free (edit
->last_search_string
);
2762 edit
->last_search_string
= NULL
;
2767 edit
->search
->search_type
= edit_search_options
.type
;
2768 edit
->search
->is_all_charsets
= edit_search_options
.all_codepages
;
2769 edit
->search
->is_case_sensitive
= edit_search_options
.case_sens
;
2770 edit
->search
->whole_words
= edit_search_options
.whole_words
;
2771 edit
->search
->search_fn
= edit_search_cmd_callback
;
2772 edit
->search_line_type
= edit_get_search_line_type (edit
->search
);
2773 edit_do_search (edit
);
2778 /* if not... then ask for an expression */
2779 g_free (edit
->last_search_string
);
2780 edit
->last_search_string
= NULL
;
2787 /* --------------------------------------------------------------------------------------------- */
2789 * Check if it's OK to close the editor. If there are unsaved changes,
2790 * ask user. Return 1 if it's OK to exit, 0 to continue editing.
2794 edit_ok_to_exit (WEdit
* edit
)
2798 if (!edit
->modified
)
2801 if (!mc_global
.midnight_shutdown
)
2803 if (!edit_check_newline (edit
))
2807 act
= edit_query_dialog3 (_("Quit"), _("File was modified. Save with exit?"),
2808 _("&Yes"), _("&No"), _("&Cancel quit"));
2813 edit_query_dialog2 (_("Quit"),
2814 _("Midnight Commander is being shut down.\nSave modified file?"),
2815 _("&Yes"), _("&No"));
2825 edit_push_markers (edit
);
2826 edit_set_markers (edit
, 0, 0, 0, 0);
2827 if (!edit_save_cmd (edit
) || mc_global
.midnight_shutdown
)
2828 return mc_global
.midnight_shutdown
;
2832 case 2: /* Cancel quit */
2840 /* --------------------------------------------------------------------------------------------- */
2841 /** save block, returns 1 on success */
2844 edit_save_block (WEdit
* edit
, const char *filename
, long start
, long finish
)
2849 vpath
= vfs_path_from_str (filename
);
2850 file
= mc_open (vpath
, O_CREAT
| O_WRONLY
| O_TRUNC
,
2851 S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IROTH
| O_BINARY
);
2852 vfs_path_free (vpath
);
2856 if (edit
->column_highlight
)
2860 r
= mc_write (file
, VERTICAL_MAGIC
, sizeof (VERTICAL_MAGIC
));
2863 unsigned char *block
, *p
;
2865 p
= block
= edit_get_block (edit
, start
, finish
, &len
);
2868 r
= mc_write (file
, p
, len
);
2882 len
= finish
- start
;
2883 buf
= g_malloc0 (TEMP_BUF_LEN
);
2884 while (start
!= finish
)
2886 end
= min (finish
, start
+ TEMP_BUF_LEN
);
2887 for (; i
< end
; i
++)
2888 buf
[i
- start
] = edit_get_byte (edit
, i
);
2889 len
-= mc_write (file
, (char *) buf
, end
- start
);
2900 /* --------------------------------------------------------------------------------------------- */
2903 edit_paste_from_history (WEdit
* edit
)
2906 edit_error_dialog (_("Error"), _("This function is not implemented"));
2909 /* --------------------------------------------------------------------------------------------- */
2912 edit_copy_to_X_buf_cmd (WEdit
* edit
)
2914 long start_mark
, end_mark
;
2915 if (eval_marks (edit
, &start_mark
, &end_mark
))
2917 if (!edit_save_block_to_clip_file (edit
, start_mark
, end_mark
))
2919 edit_error_dialog (_("Copy to clipboard"), get_sys_error (_("Unable to save to file")));
2922 /* try use external clipboard utility */
2923 mc_event_raise (MCEVENT_GROUP_CORE
, "clipboard_file_to_ext_clip", NULL
);
2928 /* --------------------------------------------------------------------------------------------- */
2931 edit_cut_to_X_buf_cmd (WEdit
* edit
)
2933 long start_mark
, end_mark
;
2934 if (eval_marks (edit
, &start_mark
, &end_mark
))
2936 if (!edit_save_block_to_clip_file (edit
, start_mark
, end_mark
))
2938 edit_error_dialog (_("Cut to clipboard"), _("Unable to save to file"));
2941 /* try use external clipboard utility */
2942 mc_event_raise (MCEVENT_GROUP_CORE
, "clipboard_file_to_ext_clip", NULL
);
2944 edit_block_delete_cmd (edit
);
2945 edit_mark_cmd (edit
, 1);
2949 /* --------------------------------------------------------------------------------------------- */
2952 edit_paste_from_X_buf_cmd (WEdit
* edit
)
2956 /* try use external clipboard utility */
2957 mc_event_raise (MCEVENT_GROUP_CORE
, "clipboard_file_from_ext_clip", NULL
);
2958 tmp
= mc_config_get_full_vpath (EDIT_CLIP_FILE
);
2959 edit_insert_file (edit
, tmp
);
2960 vfs_path_free (tmp
);
2964 /* --------------------------------------------------------------------------------------------- */
2966 * Ask user for the line and go to that line.
2967 * Negative numbers mean line from the end (i.e. -1 is the last line).
2971 edit_goto_cmd (WEdit
* edit
)
2974 static long line
= 0; /* line as typed, saved as default */
2979 g_snprintf (s
, sizeof (s
), "%ld", line
);
2980 f
= input_dialog (_("Goto line"), _("Enter line:"), MC_HISTORY_EDIT_GOTO_LINE
, line
? s
: "");
2990 l
= strtol (f
, &error
, 0);
2999 l
= edit
->total_lines
+ l
+ 2;
3000 edit_move_display (edit
, l
- edit
->widget
.lines
/ 2 - 1);
3001 edit_move_to_line (edit
, l
- 1);
3002 edit
->force
|= REDRAW_COMPLETELY
;
3007 /* --------------------------------------------------------------------------------------------- */
3008 /** Return 1 on success */
3011 edit_save_block_cmd (WEdit
* edit
)
3013 long start_mark
, end_mark
;
3016 if (eval_marks (edit
, &start_mark
, &end_mark
))
3019 tmp
= mc_config_get_full_path (EDIT_CLIP_FILE
);
3021 input_expand_dialog (_("Save block"), _("Enter file name:"),
3022 MC_HISTORY_EDIT_SAVE_BLOCK
, tmp
);
3024 edit_push_undo_action (edit
, KEY_PRESS
+ edit
->start_display
);
3034 if (edit_save_block (edit
, exp
, start_mark
, end_mark
))
3037 edit
->force
|= REDRAW_COMPLETELY
;
3043 edit_error_dialog (_("Save block"), get_sys_error (_("Cannot save file")));
3047 edit
->force
|= REDRAW_COMPLETELY
;
3052 /* --------------------------------------------------------------------------------------------- */
3054 /** returns TRUE on success */
3056 edit_insert_file_cmd (WEdit
* edit
)
3060 gboolean ret
= FALSE
;
3062 tmp
= mc_config_get_full_path (EDIT_CLIP_FILE
);
3063 exp
= input_expand_dialog (_("Insert file"), _("Enter file name:"),
3064 MC_HISTORY_EDIT_INSERT_FILE
, tmp
);
3067 edit_push_undo_action (edit
, KEY_PRESS
+ edit
->start_display
);
3069 if (exp
!= NULL
&& *exp
!= '\0')
3071 vfs_path_t
*exp_vpath
;
3073 exp_vpath
= vfs_path_from_str (exp
);
3074 ret
= (edit_insert_file (edit
, exp_vpath
) >= 0);
3075 vfs_path_free (exp_vpath
);
3078 edit_error_dialog (_("Insert file"), get_sys_error (_("Cannot insert file")));
3083 edit
->force
|= REDRAW_COMPLETELY
;
3087 /* --------------------------------------------------------------------------------------------- */
3088 /** sorts a block, returns -1 on system fail, 1 on cancel and 0 on success */
3091 edit_sort_cmd (WEdit
* edit
)
3093 static char *old
= 0;
3094 char *exp
, *tmp
, *tmp_edit_block_name
, *tmp_edit_temp_name
;
3095 long start_mark
, end_mark
;
3098 if (eval_marks (edit
, &start_mark
, &end_mark
))
3100 edit_error_dialog (_("Sort block"), _("You must first highlight a block of text"));
3104 tmp
= mc_config_get_full_path (EDIT_BLOCK_FILE
);
3105 edit_save_block (edit
, tmp
, start_mark
, end_mark
);
3108 exp
= input_dialog (_("Run sort"),
3109 _("Enter sort options (see manpage) separated by whitespace:"),
3110 MC_HISTORY_EDIT_SORT
, (old
!= NULL
) ? old
: "");
3116 tmp_edit_block_name
= mc_config_get_full_path (EDIT_BLOCK_FILE
);
3117 tmp_edit_temp_name
= mc_config_get_full_path (EDIT_TEMP_FILE
);
3119 g_strconcat (" sort ", exp
, " ", tmp_edit_block_name
,
3120 " > ", tmp_edit_temp_name
, (char *) NULL
);
3121 g_free (tmp_edit_temp_name
);
3122 g_free (tmp_edit_block_name
);
3128 if (e
== -1 || e
== 127)
3130 edit_error_dialog (_("Sort"), get_sys_error (_("Cannot execute sort command")));
3135 sprintf (q
, "%d ", e
);
3136 tmp
= g_strdup_printf (_("Sort returned non-zero: %s"), q
);
3137 edit_error_dialog (_("Sort"), tmp
);
3143 edit
->force
|= REDRAW_COMPLETELY
;
3145 if (edit_block_delete_cmd (edit
))
3149 vfs_path_t
*tmp_vpath
;
3151 tmp_vpath
= mc_config_get_full_vpath (EDIT_TEMP_FILE
);
3152 edit_insert_file (edit
, tmp_vpath
);
3153 vfs_path_free (tmp_vpath
);
3158 /* --------------------------------------------------------------------------------------------- */
3160 * Ask user for a command, execute it and paste its output back to the
3165 edit_ext_cmd (WEdit
* edit
)
3167 char *exp
, *tmp
, *tmp_edit_temp_file
;
3171 input_dialog (_("Paste output of external command"),
3172 _("Enter shell command(s):"), MC_HISTORY_EDIT_PASTE_EXTCMD
, NULL
);
3177 tmp_edit_temp_file
= mc_config_get_full_path (EDIT_TEMP_FILE
);
3178 tmp
= g_strconcat (exp
, " > ", tmp_edit_temp_file
, (char *) NULL
);
3179 g_free (tmp_edit_temp_file
);
3186 edit_error_dialog (_("External command"), get_sys_error (_("Cannot execute command")));
3190 edit
->force
|= REDRAW_COMPLETELY
;
3193 vfs_path_t
*tmp_vpath
;
3195 tmp_vpath
= mc_config_get_full_vpath (EDIT_TEMP_FILE
);
3196 edit_insert_file (edit
, tmp_vpath
);
3197 vfs_path_free (tmp_vpath
);
3202 /* --------------------------------------------------------------------------------------------- */
3203 /** if block is 1, a block must be highlighted and the shell command
3204 processes it. If block is 0 the shell command is a straight system
3205 command, that just produces some output which is to be inserted */
3208 edit_block_process_cmd (WEdit
* edit
, int macro_number
)
3211 char *macros_fname
= NULL
;
3213 fname
= g_strdup_printf ("%s.%i.sh", MC_EXTMACRO_FILE
, macro_number
);
3214 macros_fname
= g_build_filename (mc_config_get_data_path (), fname
, (char *) NULL
);
3215 user_menu (edit
, macros_fname
, 0);
3217 g_free (macros_fname
);
3218 edit
->force
|= REDRAW_COMPLETELY
;
3221 /* --------------------------------------------------------------------------------------------- */
3224 edit_mail_dialog (WEdit
* edit
)
3227 char *tmail_subject
;
3230 static char *mail_cc_last
= 0;
3231 static char *mail_subject_last
= 0;
3232 static char *mail_to_last
= 0;
3234 QuickWidget quick_widgets
[] = {
3235 /* 0 */ QUICK_BUTTON (6, 10, 9, MAIL_DLG_HEIGHT
, N_("&Cancel"), B_CANCEL
, NULL
),
3236 /* 1 */ QUICK_BUTTON (2, 10, 9, MAIL_DLG_HEIGHT
, N_("&OK"), B_ENTER
, NULL
),
3237 /* 2 */ QUICK_INPUT (3, 50, 8, MAIL_DLG_HEIGHT
, "", 44, 0, "mail-dlg-input", &tmail_cc
),
3238 /* 3 */ QUICK_LABEL (3, 50, 7, MAIL_DLG_HEIGHT
, N_("Copies to")),
3239 /* 4 */ QUICK_INPUT (3, 50, 6, MAIL_DLG_HEIGHT
, "", 44, 0, "mail-dlg-input-2",
3241 /* 5 */ QUICK_LABEL (3, 50, 5, MAIL_DLG_HEIGHT
, N_("Subject")),
3242 /* 6 */ QUICK_INPUT (3, 50, 4, MAIL_DLG_HEIGHT
, "", 44, 0, "mail-dlg-input-3", &tmail_to
),
3243 /* 7 */ QUICK_LABEL (3, 50, 3, MAIL_DLG_HEIGHT
, N_("To")),
3244 /* 8 */ QUICK_LABEL (3, 50, 2, MAIL_DLG_HEIGHT
, N_("mail -s <subject> -c <cc> <to>")),
3248 QuickDialog Quick_input
= {
3249 50, MAIL_DLG_HEIGHT
, -1, -1, N_("Mail"),
3250 "[Input Line Keys]", quick_widgets
, NULL
, NULL
, FALSE
3253 quick_widgets
[2].u
.input
.text
= mail_cc_last
? mail_cc_last
: "";
3254 quick_widgets
[4].u
.input
.text
= mail_subject_last
? mail_subject_last
: "";
3255 quick_widgets
[6].u
.input
.text
= mail_to_last
? mail_to_last
: "";
3257 if (quick_dialog (&Quick_input
) != B_CANCEL
)
3259 g_free (mail_cc_last
);
3260 g_free (mail_subject_last
);
3261 g_free (mail_to_last
);
3262 mail_cc_last
= tmail_cc
;
3263 mail_subject_last
= tmail_subject
;
3264 mail_to_last
= tmail_to
;
3265 pipe_mail (edit
, mail_to_last
, mail_subject_last
, mail_cc_last
);
3270 /*******************/
3271 /* Word Completion */
3272 /*******************/
3274 /* --------------------------------------------------------------------------------------------- */
3276 * Complete current word using regular expression search
3277 * backwards beginning at the current cursor position.
3281 edit_complete_word_cmd (WEdit
* edit
)
3283 gsize i
, max_len
, word_len
= 0, num_compl
= 0;
3284 long word_start
= 0;
3285 unsigned char *bufpos
;
3287 struct selection
compl[MAX_WORD_COMPLETIONS
]; /* completions */
3289 /* search start of word to be completed */
3290 if (!edit_find_word_start (edit
, &word_start
, &word_len
))
3293 /* prepare match expression */
3294 bufpos
= &edit
->buffers1
[word_start
>> S_EDIT_BUF_SIZE
][word_start
& M_EDIT_BUF_SIZE
];
3296 /* match_expr = g_strdup_printf ("\\b%.*s[a-zA-Z_0-9]+", word_len, bufpos); */
3299 ("(^|\\s+|\\b)%.*s[^\\s\\.=\\+\\[\\]\\(\\)\\,\\;\\:\\\"\\'\\-\\?\\/\\|\\\\\\{\\}\\*\\&\\^\\%%\\$#@\\!]+",
3300 (int) word_len
, bufpos
);
3302 /* collect the possible completions */
3303 /* start search from begin to end of file */
3305 edit_collect_completions (edit
, word_start
, word_len
, match_expr
,
3306 (struct selection
*) &compl, &num_compl
);
3310 /* insert completed word if there is only one match */
3313 for (i
= word_len
; i
< compl[0].len
; i
++)
3314 edit_insert (edit
, *(compl[0].text
+ i
));
3316 /* more than one possible completion => ask the user */
3319 /* !!! usually only a beep is expected and when <ALT-TAB> is !!! */
3320 /* !!! pressed again the selection dialog pops up, but that !!! */
3321 /* !!! seems to require a further internal state !!! */
3324 /* let the user select the preferred completion */
3325 editcmd_dialog_completion_show (edit
, max_len
, word_len
,
3326 (struct selection
*) &compl, num_compl
);
3330 g_free (match_expr
);
3331 /* release memory before return */
3332 for (i
= 0; i
< num_compl
; i
++)
3333 g_free (compl[i
].text
);
3336 /* --------------------------------------------------------------------------------------------- */
3339 edit_select_codepage_cmd (WEdit
* edit
)
3342 if (do_select_codepage ())
3343 edit_set_codeset (edit
);
3345 edit
->force
= REDRAW_COMPLETELY
;
3346 edit_refresh_cmd (edit
);
3352 /* --------------------------------------------------------------------------------------------- */
3355 edit_insert_literal_cmd (WEdit
* edit
)
3357 int char_for_insertion
= editcmd_dialog_raw_key_query (_("Insert literal"),
3358 _("Press any key:"), 0);
3359 edit_execute_key_command (edit
, -1, ascii_alpha_to_cntrl (char_for_insertion
));
3362 /* --------------------------------------------------------------------------------------------- */
3365 edit_begin_end_macro_cmd (WEdit
* edit
)
3367 /* edit is a pointer to the widget */
3370 unsigned long command
= macro_index
< 0 ? CK_MacroStartRecord
: CK_MacroStopRecord
;
3371 edit_execute_key_command (edit
, command
, -1);
3375 /* --------------------------------------------------------------------------------------------- */
3378 edit_begin_end_repeat_cmd (WEdit
* edit
)
3380 /* edit is a pointer to the widget */
3383 unsigned long command
= macro_index
< 0 ? CK_RepeatStartRecord
: CK_RepeatStopRecord
;
3384 edit_execute_key_command (edit
, command
, -1);
3388 /* --------------------------------------------------------------------------------------------- */
3391 edit_load_forward_cmd (WEdit
* edit
)
3394 && edit_query_dialog2 (_("Warning"),
3395 _("Current text was modified without a file save.\n"
3396 "Continue discards these changes"), _("C&ontinue"),
3399 edit
->force
|= REDRAW_COMPLETELY
;
3403 if (edit_stack_iterator
+ 1 >= MAX_HISTORY_MOVETO
)
3406 if (edit_history_moveto
[edit_stack_iterator
+ 1].line
< 1)
3409 edit_stack_iterator
++;
3410 if (edit_history_moveto
[edit_stack_iterator
].filename_vpath
!= NULL
)
3411 return edit_reload_line (edit
, edit_history_moveto
[edit_stack_iterator
].filename_vpath
,
3412 edit_history_moveto
[edit_stack_iterator
].line
);
3417 /* --------------------------------------------------------------------------------------------- */
3420 edit_load_back_cmd (WEdit
* edit
)
3423 && edit_query_dialog2 (_("Warning"),
3424 _("Current text was modified without a file save.\n"
3425 "Continue discards these changes"), _("C&ontinue"),
3428 edit
->force
|= REDRAW_COMPLETELY
;
3432 if (edit_stack_iterator
< 0)
3435 edit_stack_iterator
--;
3436 if (edit_history_moveto
[edit_stack_iterator
].filename_vpath
!= NULL
)
3437 return edit_reload_line (edit
, edit_history_moveto
[edit_stack_iterator
].filename_vpath
,
3438 edit_history_moveto
[edit_stack_iterator
].line
);
3443 /* --------------------------------------------------------------------------------------------- */
3446 edit_get_match_keyword_cmd (WEdit
* edit
)
3448 gsize word_len
= 0, max_len
= 0;
3451 long word_start
= 0;
3452 unsigned char *bufpos
;
3456 char *tagfile
= NULL
;
3458 etags_hash_t def_hash
[MAX_DEFINITIONS
];
3460 for (i
= 0; i
< MAX_DEFINITIONS
; i
++)
3462 def_hash
[i
].filename
= NULL
;
3465 /* search start of word to be completed */
3466 if (!edit_find_word_start (edit
, &word_start
, &word_len
))
3469 /* prepare match expression */
3470 bufpos
= &edit
->buffers1
[word_start
>> S_EDIT_BUF_SIZE
][word_start
& M_EDIT_BUF_SIZE
];
3471 match_expr
= g_strdup_printf ("%.*s", (int) word_len
, bufpos
);
3473 ptr
= g_get_current_dir ();
3474 path
= g_strconcat (ptr
, G_DIR_SEPARATOR_S
, (char *) NULL
);
3477 /* Recursive search file 'TAGS' in parent dirs */
3480 ptr
= g_path_get_dirname (path
);
3484 tagfile
= mc_build_filename (path
, TAGS_NAME
, (char *) NULL
);
3485 if (exist_file (tagfile
))
3488 while (strcmp (path
, G_DIR_SEPARATOR_S
) != 0);
3493 etags_set_definition_hash (tagfile
, path
, match_expr
, (etags_hash_t
*) & def_hash
);
3498 max_len
= MAX_WIDTH_DEF_DIALOG
;
3502 editcmd_dialog_select_definition_show (edit
, match_expr
, max_len
, word_len
,
3503 (etags_hash_t
*) & def_hash
, num_def
);
3505 g_free (match_expr
);
3508 /* --------------------------------------------------------------------------------------------- */