2 Editor high level editing commands
4 Copyright (C) 1996, 1997, 1998, 2001, 2002, 2003, 2004, 2005, 2006,
6 The Free Software Foundation, Inc.
11 This file is part of the Midnight Commander.
13 The Midnight Commander is free software: you can redistribute it
14 and/or modify it under the terms of the GNU General Public License as
15 published by the Free Software Foundation, either version 3 of the License,
16 or (at your option) any later version.
18 The Midnight Commander is distributed in the hope that it will be useful,
19 but WITHOUT ANY WARRANTY; without even the implied warranty of
20 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 GNU General Public License for more details.
23 You should have received a copy of the GNU General Public License
24 along with this program. If not, see <http://www.gnu.org/licenses/>.
28 * \brief Source: editor high level editing commands
33 /* #define PIPE_BLOCKS_SO_READ_BYTE_BY_BYTE */
44 #include <sys/types.h>
52 #include "lib/global.h"
53 #include "lib/tty/tty.h"
54 #include "lib/tty/key.h" /* XCTRL */
55 #include "lib/mcconfig.h"
57 #include "lib/strutil.h" /* utf string functions */
59 #include "lib/util.h" /* tilde_expand() */
60 #include "lib/vfs/vfs.h"
61 #include "lib/widget.h"
62 #include "lib/charsets.h"
63 #include "lib/event.h" /* mc_event_raise() */
65 #include "src/history.h"
66 #include "src/setup.h" /* option_tab_spacing */
67 #include "src/main.h" /* mactos_t */
68 #include "src/selcodepage.h"
69 #include "src/keybind-defaults.h"
70 #include "src/util.h" /* check_for_default() */
71 #include "src/filemanager/layout.h" /* mc_refresh() */
73 #include "edit-impl.h"
74 #include "edit-widget.h"
75 #include "editcmd_dialogs.h"
78 /*** global variables ****************************************************************************/
80 /* search and replace: */
81 int search_create_bookmark
= FALSE
;
83 /* queries on a save */
84 int edit_confirm_save
= 1;
86 /*** file scope macro definitions ****************************************************************/
90 #define TEMP_BUF_LEN 1024
94 /* thanks to Liviu Daia <daia@stoilow.imar.ro> for getting this
95 (and the above) routines to work properly - paul */
97 #define is_digit(x) ((x) >= '0' && (x) <= '9')
99 #define MAIL_DLG_HEIGHT 12
101 #define MAX_WORD_COMPLETIONS 100 /* in listbox */
103 /*** file scope type declarations ****************************************************************/
105 /*** file scope variables ************************************************************************/
107 /*** file scope functions ************************************************************************/
108 /* --------------------------------------------------------------------------------------------- */
110 /* If 0 (quick save) then a) create/truncate <filename> file,
111 b) save to <filename>;
112 if 1 (safe save) then a) save to <tempnam>,
113 b) rename <tempnam> to <filename>;
114 if 2 (do backups) then a) save to <tempnam>,
115 b) rename <filename> to <filename.backup_ext>,
116 c) rename <tempnam> to <filename>. */
118 /* returns 0 on error, -1 on abort */
121 edit_save_file (WEdit
* edit
, const char *filename
)
126 gchar
*real_filename
;
127 int this_save_mode
, fd
= -1;
128 vfs_path_t
*real_filename_vpath
;
129 vfs_path_t
*savename_vpath
= NULL
;
136 if (*filename
!= PATH_SEP
&& edit
->dir
)
138 real_filename
= concat_dir_and_file (edit
->dir
, filename
);
142 real_filename
= g_strdup (filename
);
144 real_filename_vpath
= vfs_path_from_str (real_filename
);
146 this_save_mode
= option_save_mode
;
147 if (this_save_mode
!= EDIT_QUICK_SAVE
)
149 if (!vfs_file_is_local (real_filename_vpath
)
150 || (fd
= mc_open (real_filename_vpath
, O_RDONLY
| O_BINARY
)) == -1)
153 * The file does not exists yet, so no safe save or
154 * backup are necessary.
156 this_save_mode
= EDIT_QUICK_SAVE
;
162 if (this_save_mode
== EDIT_QUICK_SAVE
&& !edit
->skip_detach_prompt
)
167 rv
= mc_stat (real_filename_vpath
, &sb
);
168 if (rv
== 0 && sb
.st_nlink
> 1)
170 rv
= edit_query_dialog3 (_("Warning"),
171 _("File has hard-links. Detach before saving?"),
172 _("&Yes"), _("&No"), _("&Cancel"));
176 this_save_mode
= EDIT_SAFE_SAVE
;
179 edit
->skip_detach_prompt
= 1;
182 g_free (real_filename
);
183 vfs_path_free (real_filename_vpath
);
188 /* Prevent overwriting changes from other editor sessions. */
189 if (rv
== 0 && edit
->stat1
.st_mtime
!= 0 && edit
->stat1
.st_mtime
!= sb
.st_mtime
)
192 /* The default action is "Cancel". */
195 rv
= edit_query_dialog2 (_("Warning"),
196 _("The file has been modified in the meantime. Save anyway?"),
197 _("&Yes"), _("&Cancel"));
200 g_free (real_filename
);
201 vfs_path_free (real_filename_vpath
);
207 if (this_save_mode
!= EDIT_QUICK_SAVE
)
209 char *savedir
, *saveprefix
;
210 const char *slashpos
;
211 slashpos
= strrchr (real_filename
, PATH_SEP
);
214 savedir
= g_strdup (real_filename
);
215 savedir
[slashpos
- real_filename
+ 1] = '\0';
218 savedir
= g_strdup (".");
219 saveprefix
= mc_build_filename (savedir
, "cooledit", NULL
);
221 fd
= mc_mkstemps (&savename_vpath
, saveprefix
, NULL
);
223 if (savename_vpath
== NULL
)
225 g_free (real_filename
);
226 vfs_path_free (real_filename_vpath
);
230 * Close for now because mc_mkstemps use pure open system call
231 * to create temporary file and it needs to be reopened by
232 * VFS-aware mc_open().
237 savename_vpath
= vfs_path_from_str (real_filename
);
239 (void) mc_chown (savename_vpath
, edit
->stat1
.st_uid
, edit
->stat1
.st_gid
);
240 (void) mc_chmod (savename_vpath
, edit
->stat1
.st_mode
);
242 fd
= mc_open (savename_vpath
, O_CREAT
| O_WRONLY
| O_TRUNC
| O_BINARY
, edit
->stat1
.st_mode
);
247 p
= edit_get_write_filter (savename_vpath
, real_filename
);
253 file
= (FILE *) popen (p
, "w");
257 filelen
= edit_write_stream (edit
, file
);
261 if (pclose (file
) != 0)
263 tmp
= g_strdup_printf (_("Error writing to pipe: %s"), p
);
264 edit_error_dialog (_("Error"), tmp
);
273 tmp
= g_strdup_printf (_("Cannot open pipe for writing: %s"), p
);
274 edit_error_dialog (_("Error"), get_sys_error (tmp
));
281 else if (edit
->lb
== LB_ASIS
)
282 { /* do not change line breaks */
285 filelen
= edit
->last_byte
;
286 while (buf
<= (edit
->curs1
>> S_EDIT_BUF_SIZE
) - 1)
288 if (mc_write (fd
, (char *) edit
->buffers1
[buf
], EDIT_BUF_SIZE
) != EDIT_BUF_SIZE
)
296 (fd
, (char *) edit
->buffers1
[buf
],
297 edit
->curs1
& M_EDIT_BUF_SIZE
) != (edit
->curs1
& M_EDIT_BUF_SIZE
))
301 else if (edit
->curs2
)
304 buf
= (edit
->curs2
>> S_EDIT_BUF_SIZE
);
307 (char *) edit
->buffers2
[buf
] + EDIT_BUF_SIZE
-
308 (edit
->curs2
& M_EDIT_BUF_SIZE
) - 1,
309 1 + (edit
->curs2
& M_EDIT_BUF_SIZE
)) != 1 + (edit
->curs2
& M_EDIT_BUF_SIZE
))
317 if (mc_write (fd
, (char *) edit
->buffers2
[buf
], EDIT_BUF_SIZE
) != EDIT_BUF_SIZE
)
329 /* Update the file information, especially the mtime. */
330 if (mc_stat (savename_vpath
, &edit
->stat1
) == -1)
334 { /* change line breaks */
336 vfs_path_element_t
*path_element
;
340 path_element
= vfs_path_get_by_index (savename_vpath
, -1);
341 file
= (FILE *) fopen (path_element
->path
, "w");
344 filelen
= edit_write_stream (edit
, file
);
351 msg
= g_strdup_printf (_("Cannot open file for writing: %s"), path_element
->path
);
352 edit_error_dialog (_("Error"), msg
);
358 if (filelen
!= edit
->last_byte
)
361 if (this_save_mode
== EDIT_DO_BACKUP
)
363 vfs_path_t
*tmp_vpath
;
367 assert (option_backup_ext
!= NULL
);
369 tmp_vpath
= vfs_path_append_new (real_filename_vpath
, option_backup_ext
, (char *) NULL
);
370 ok
= (mc_rename (real_filename_vpath
, tmp_vpath
) != -1);
371 vfs_path_free (tmp_vpath
);
376 if (this_save_mode
!= EDIT_QUICK_SAVE
)
377 if (mc_rename (savename_vpath
, real_filename_vpath
) == -1)
380 g_free (real_filename
);
381 vfs_path_free (real_filename_vpath
);
382 vfs_path_free (savename_vpath
);
385 /* FIXME: Is this safe ?
386 * if (this_save_mode != EDIT_QUICK_SAVE)
387 * mc_unlink (savename);
389 g_free (real_filename
);
390 vfs_path_free (real_filename_vpath
);
391 vfs_path_free (savename_vpath
);
395 /* --------------------------------------------------------------------------------------------- */
398 edit_check_newline (WEdit
* edit
)
400 return !(option_check_nl_at_eof
&& edit
->last_byte
> 0
401 && edit_get_byte (edit
, edit
->last_byte
- 1) != '\n'
402 && edit_query_dialog2 (_("Warning"),
403 _("The file you are saving is not finished with a newline"),
404 _("C&ontinue"), _("&Cancel")));
407 /* --------------------------------------------------------------------------------------------- */
410 edit_get_save_file_as (WEdit
* edit
)
413 #define DLG_HEIGHT 14
415 static LineBreaks cur_lb
= LB_ASIS
;
417 char *filename
= edit
->filename
;
419 const char *lb_names
[LB_NAMES
] = {
420 N_("&Do not change"),
421 N_("&Unix format (LF)"),
422 N_("&Windows/DOS format (CR LF)"),
423 N_("&Macintosh format (CR)")
426 QuickWidget quick_widgets
[] = {
427 QUICK_BUTTON (6, 10, DLG_HEIGHT
- 3, DLG_HEIGHT
, N_("&Cancel"), B_CANCEL
, NULL
),
428 QUICK_BUTTON (2, 10, DLG_HEIGHT
- 3, DLG_HEIGHT
, N_("&OK"), B_ENTER
, NULL
),
429 QUICK_RADIO (5, DLG_WIDTH
, DLG_HEIGHT
- 8, DLG_HEIGHT
, LB_NAMES
, lb_names
, (int *) &cur_lb
),
430 QUICK_LABEL (3, DLG_WIDTH
, DLG_HEIGHT
- 9, DLG_HEIGHT
, N_("Change line breaks to:")),
431 QUICK_INPUT (3, DLG_WIDTH
, DLG_HEIGHT
- 11, DLG_HEIGHT
, filename
, DLG_WIDTH
- 6, 0,
432 "save-as", &filename
),
433 QUICK_LABEL (3, DLG_WIDTH
, DLG_HEIGHT
- 12, DLG_HEIGHT
, N_("Enter file name:")),
437 QuickDialog Quick_options
= {
438 DLG_WIDTH
, DLG_HEIGHT
, -1, -1,
439 N_("Save As"), "[Save File As]",
440 quick_widgets
, NULL
, FALSE
443 if (quick_dialog (&Quick_options
) != B_CANCEL
)
448 fname
= tilde_expand (filename
);
459 /** returns 1 on success */
462 edit_save_cmd (WEdit
* edit
)
464 int res
, save_lock
= 0;
466 if (!edit
->locked
&& !edit
->delete_file
)
467 save_lock
= edit_lock_file (edit
);
468 res
= edit_save_file (edit
, edit
->filename
);
470 /* Maintain modify (not save) lock on failure */
471 if ((res
> 0 && edit
->locked
) || save_lock
)
472 edit
->locked
= edit_unlock_file (edit
);
474 /* On failure try 'save as', it does locking on its own */
476 return edit_save_as_cmd (edit
);
477 edit
->force
|= REDRAW_COMPLETELY
;
480 edit
->delete_file
= 0;
487 /* --------------------------------------------------------------------------------------------- */
488 /** returns 1 on error */
491 edit_load_file_from_filename (WEdit
* edit
, char *exp
)
493 int prev_locked
= edit
->locked
;
494 char *prev_filename
= g_strdup (edit
->filename
);
496 if (!edit_reload (edit
, exp
))
498 g_free (prev_filename
);
506 fullpath
= mc_build_filename (edit
->dir
, prev_filename
, (char *) NULL
);
507 unlock_file (fullpath
);
510 g_free (prev_filename
);
514 /* --------------------------------------------------------------------------------------------- */
517 edit_load_syntax_file (WEdit
* edit
)
524 dir
= query_dialog (_("Syntax file edit"),
525 _("Which syntax file you want to edit?"), D_NORMAL
, 2,
526 _("&User"), _("&System Wide"));
529 extdir
= g_build_filename (mc_global
.sysconfig_dir
, "syntax", "Syntax", (char *) NULL
);
530 if (!exist_file (extdir
))
533 extdir
= g_build_filename (mc_global
.share_data_dir
, "syntax", "Syntax", (char *) NULL
);
540 buffer
= mc_config_get_full_path (EDIT_SYNTAX_FILE
);
541 check_for_default (extdir
, buffer
);
542 edit_load_file_from_filename (edit
, buffer
);
546 edit_load_file_from_filename (edit
, extdir
);
551 /* --------------------------------------------------------------------------------------------- */
554 edit_load_menu_file (WEdit
* edit
)
560 dir
= query_dialog (_("Menu edit"),
561 _("Which menu file do you want to edit?"), D_NORMAL
,
562 geteuid () != 0 ? 2 : 3, _("&Local"), _("&User"), _("&System Wide"));
564 menufile
= concat_dir_and_file (mc_global
.sysconfig_dir
, EDIT_GLOBAL_MENU
);
566 if (!exist_file (menufile
))
569 menufile
= concat_dir_and_file (mc_global
.share_data_dir
, EDIT_GLOBAL_MENU
);
575 buffer
= g_strdup (EDIT_LOCAL_MENU
);
576 check_for_default (menufile
, buffer
);
577 chmod (buffer
, 0600);
581 buffer
= mc_config_get_full_path (EDIT_HOME_MENU
);
582 check_for_default (menufile
, buffer
);
586 buffer
= concat_dir_and_file (mc_global
.sysconfig_dir
, EDIT_GLOBAL_MENU
);
587 if (!exist_file (buffer
))
590 buffer
= concat_dir_and_file (mc_global
.share_data_dir
, EDIT_GLOBAL_MENU
);
599 edit_load_file_from_filename (edit
, buffer
);
605 /* --------------------------------------------------------------------------------------------- */
608 edit_delete_column_of_text (WEdit
* edit
)
610 long p
, q
, r
, m1
, m2
;
613 eval_marks (edit
, &m1
, &m2
);
614 n
= edit_move_forward (edit
, m1
, 0, m2
) + 1;
615 c
= edit_move_forward3 (edit
, edit_bol (edit
, m1
), 0, m1
);
616 d
= edit_move_forward3 (edit
, edit_bol (edit
, m2
), 0, m2
);
617 b
= max (min (c
, d
), min (edit
->column1
, edit
->column2
));
618 c
= max (c
, max (edit
->column1
, edit
->column2
));
622 r
= edit_bol (edit
, edit
->curs1
);
623 p
= edit_move_forward3 (edit
, r
, b
, 0);
624 q
= edit_move_forward3 (edit
, r
, c
, 0);
629 edit_cursor_move (edit
, p
- edit
->curs1
);
632 /* delete line between margins */
633 if (edit_get_byte (edit
, edit
->curs1
) != '\n')
634 edit_delete (edit
, 1);
638 /* move to next line except on the last delete */
639 edit_cursor_move (edit
, edit_move_forward (edit
, edit
->curs1
, 1, 0) - edit
->curs1
);
643 /* --------------------------------------------------------------------------------------------- */
644 /** if success return 0 */
647 edit_block_delete (WEdit
* edit
)
650 long start_mark
, end_mark
;
651 int curs_pos
, line_width
;
652 long curs_line
, c1
, c2
;
654 if (eval_marks (edit
, &start_mark
, &end_mark
))
656 if (edit
->column_highlight
&& edit
->mark2
< 0)
657 edit_mark_cmd (edit
, 0);
658 if ((end_mark
- start_mark
) > option_max_undo
/ 2)
660 /* Warning message with a query to continue or cancel the operation */
661 if (edit_query_dialog2
664 ("Block is large, you may not be able to undo this action"),
665 _("C&ontinue"), _("&Cancel")))
670 c1
= min (edit
->column1
, edit
->column2
);
671 c2
= max (edit
->column1
, edit
->column2
);
675 edit_push_markers (edit
);
677 curs_line
= edit
->curs_line
;
679 /* calculate line width and cursor position before cut */
680 line_width
= edit_move_forward3 (edit
, edit_bol (edit
, edit
->curs1
), 0,
681 edit_eol (edit
, edit
->curs1
));
682 curs_pos
= edit
->curs_col
+ edit
->over_col
;
684 /* move cursor to start of selection */
685 edit_cursor_move (edit
, start_mark
- edit
->curs1
);
686 edit_scroll_screen_over_cursor (edit
);
688 if (start_mark
< end_mark
)
690 if (edit
->column_highlight
)
693 edit_mark_cmd (edit
, 0);
694 edit_delete_column_of_text (edit
);
695 /* move cursor to the saved position */
696 edit_move_to_line (edit
, curs_line
);
697 /* calculate line width after cut */
698 line_width
= edit_move_forward3 (edit
, edit_bol (edit
, edit
->curs1
), 0,
699 edit_eol (edit
, edit
->curs1
));
700 if (option_cursor_beyond_eol
&& curs_pos
> line_width
)
701 edit
->over_col
= curs_pos
- line_width
;
705 while (count
< end_mark
)
707 edit_delete (edit
, 1);
712 edit_set_markers (edit
, 0, 0, 0, 0);
713 edit
->force
|= REDRAW_PAGE
;
717 /* --------------------------------------------------------------------------------------------- */
719 * Get EOL symbol for searching.
721 * @param edit editor object
726 edit_search_get_current_end_line_char (const WEdit
* edit
)
737 /* --------------------------------------------------------------------------------------------- */
739 * Checking if search condition have BOL(^) or EOL ($) regexp special characters.
741 * @param search search object
742 * @return result of checks.
745 static edit_search_line_t
746 edit_get_search_line_type (mc_search_t
* search
)
748 edit_search_line_t search_line_type
= 0;
750 if (search
->search_type
!= MC_SEARCH_T_REGEX
)
751 return search_line_type
;
753 if (*search
->original
== '^')
754 search_line_type
|= AT_START_LINE
;
756 if (search
->original
[search
->original_len
- 1] == '$')
757 search_line_type
|= AT_END_LINE
;
758 return search_line_type
;
761 /* --------------------------------------------------------------------------------------------- */
763 * Calculating the start position of next line.
765 * @param edit editor object
766 * @param current_pos current position
767 * @param max_pos max position
768 * @param end_string_symbol end of line symbol
769 * @return start position of next line
773 edit_calculate_start_of_next_line (WEdit
* edit
, off_t current_pos
, off_t max_pos
,
774 char end_string_symbol
)
778 for (i
= current_pos
; i
< max_pos
; i
++)
781 if (edit_get_byte (edit
, i
) == end_string_symbol
)
788 /* --------------------------------------------------------------------------------------------- */
790 * Calculating the end position of previous line.
792 * @param edit editor object
793 * @param current_pos current position
794 * @param end_string_symbol end of line symbol
795 * @return end position of previous line
799 edit_calculate_end_of_previous_line (WEdit
* edit
, off_t current_pos
, char end_string_symbol
)
803 for (i
= current_pos
- 1; i
>= 0; i
--)
804 if (edit_get_byte (edit
, i
) == end_string_symbol
)
810 /* --------------------------------------------------------------------------------------------- */
812 * Calculating the start position of previous line.
814 * @param edit editor object
815 * @param current_pos current position
816 * @param end_string_symbol end of line symbol
817 * @return start position of previous line
821 edit_calculate_start_of_previous_line (WEdit
* edit
, off_t current_pos
, char end_string_symbol
)
823 current_pos
= edit_calculate_end_of_previous_line (edit
, current_pos
, end_string_symbol
);
824 current_pos
= edit_calculate_end_of_previous_line (edit
, current_pos
, end_string_symbol
);
826 return (current_pos
+ 1);
829 /* --------------------------------------------------------------------------------------------- */
831 * Calculating the start position of current line.
833 * @param edit editor object
834 * @param current_pos current position
835 * @param end_string_symbol end of line symbol
836 * @return start position of current line
840 edit_calculate_start_of_current_line (WEdit
* edit
, off_t current_pos
, char end_string_symbol
)
842 current_pos
= edit_calculate_end_of_previous_line (edit
, current_pos
, end_string_symbol
);
844 return (current_pos
+ 1);
847 /* --------------------------------------------------------------------------------------------- */
849 * Fixing (if needed) search start position if 'only in selection' option present.
851 * @param edit editor object
855 edit_search_fix_search_start_if_selection (WEdit
* edit
)
860 if (!edit_search_options
.only_in_selection
)
863 if (eval_marks (edit
, &start_mark
, &end_mark
) != 0)
866 if (edit_search_options
.backwards
)
868 if (edit
->search_start
> end_mark
|| edit
->search_start
<= start_mark
)
869 edit
->search_start
= end_mark
;
873 if (edit
->search_start
< start_mark
|| edit
->search_start
>= end_mark
)
874 edit
->search_start
= start_mark
;
878 /* --------------------------------------------------------------------------------------------- */
881 editcmd_find (WEdit
* edit
, gsize
* len
)
883 off_t search_start
= edit
->search_start
;
886 long end_mark
= edit
->last_byte
;
888 char end_string_symbol
;
890 end_string_symbol
= edit_search_get_current_end_line_char (edit
);
892 /* prepare for search */
893 if (edit_search_options
.only_in_selection
)
895 mark_res
= eval_marks (edit
, &start_mark
, &end_mark
);
898 edit
->search
->error
= MC_SEARCH_E_NOTFOUND
;
899 edit
->search
->error_str
= g_strdup (_("Search string not found"));
903 /* fix the start and the end of search block positions */
904 if ((edit
->search_line_type
& AT_START_LINE
) != 0
905 && (start_mark
!= 0 || edit_get_byte (edit
, start_mark
- 1) != end_string_symbol
))
908 edit_calculate_start_of_next_line (edit
, start_mark
, edit
->last_byte
,
911 if ((edit
->search_line_type
& AT_END_LINE
) != 0
912 && (end_mark
- 1 != edit
->last_byte
913 || edit_get_byte (edit
, end_mark
) != end_string_symbol
))
915 end_mark
= edit_calculate_end_of_previous_line (edit
, end_mark
, end_string_symbol
);
917 if (start_mark
>= end_mark
)
919 edit
->search
->error
= MC_SEARCH_E_NOTFOUND
;
920 edit
->search
->error_str
= g_strdup (_("Search string not found"));
926 if (edit_search_options
.backwards
)
927 end_mark
= max (1, edit
->curs1
) - 1;
931 if (edit_search_options
.backwards
)
933 /* backward search */
934 search_end
= end_mark
;
936 if ((edit
->search_line_type
& AT_START_LINE
) != 0)
938 edit_calculate_start_of_current_line (edit
, search_start
, end_string_symbol
);
940 while ((int) search_start
>= start_mark
)
942 if (search_end
> (off_t
) (search_start
+ edit
->search
->original_len
)
943 && mc_search_is_fixed_search_str (edit
->search
))
945 search_end
= search_start
+ edit
->search
->original_len
;
947 if (mc_search_run (edit
->search
, (void *) edit
, search_start
, search_end
, len
)
948 && edit
->search
->normal_offset
== search_start
)
954 if ((edit
->search_line_type
& AT_START_LINE
) != 0)
956 edit_calculate_start_of_previous_line (edit
, search_start
, end_string_symbol
);
960 edit
->search
->error_str
= g_strdup (_("Search string not found"));
965 if ((edit
->search_line_type
& AT_START_LINE
) != 0 && search_start
!= start_mark
)
967 edit_calculate_start_of_next_line (edit
, search_start
, end_mark
, end_string_symbol
);
968 return mc_search_run (edit
->search
, (void *) edit
, search_start
, end_mark
, len
);
973 /* --------------------------------------------------------------------------------------------- */
976 edit_replace_cmd__conv_to_display (char *str
)
981 tmp
= str_convert_to_display (str
);
985 return g_string_free (tmp
, FALSE
);
986 g_string_free (tmp
, TRUE
);
989 return g_strdup (str
);
992 /* --------------------------------------------------------------------------------------------- */
995 edit_replace_cmd__conv_to_input (char *str
)
1000 tmp
= str_convert_to_input (str
);
1004 return g_string_free (tmp
, FALSE
);
1005 g_string_free (tmp
, TRUE
);
1008 return g_strdup (str
);
1011 /* --------------------------------------------------------------------------------------------- */
1014 edit_do_search (WEdit
* edit
)
1018 if (edit
->search
== NULL
)
1019 edit
->search_start
= edit
->curs1
;
1021 edit_push_undo_action (edit
, KEY_PRESS
+ edit
->start_display
);
1023 if (search_create_bookmark
)
1025 int found
= 0, books
= 0;
1026 long l
= 0, l_last
= -1;
1029 search_create_bookmark
= FALSE
;
1030 book_mark_flush (edit
, -1);
1034 if (!mc_search_run (edit
->search
, (void *) edit
, q
, edit
->last_byte
, &len
))
1037 edit
->search_start
= edit
->search
->normal_offset
;
1039 l
+= edit_count_lines (edit
, q
, edit
->search
->normal_offset
);
1042 book_mark_insert (edit
, l
, BOOK_MARK_FOUND_COLOR
);
1046 q
= edit
->search
->normal_offset
+ 1;
1050 edit_error_dialog (_("Search"), _("Search string not found"));
1052 edit_cursor_move (edit
, edit
->search_start
- edit
->curs1
);
1056 if (edit
->found_len
!= 0 && edit
->search_start
== edit
->found_start
+ 1
1057 && edit_search_options
.backwards
)
1058 edit
->search_start
--;
1060 if (edit
->found_len
!= 0 && edit
->search_start
== edit
->found_start
- 1
1061 && !edit_search_options
.backwards
)
1062 edit
->search_start
++;
1064 if (editcmd_find (edit
, &len
))
1066 edit
->found_start
= edit
->search_start
= edit
->search
->normal_offset
;
1067 edit
->found_len
= len
;
1069 edit_cursor_move (edit
, edit
->search_start
- edit
->curs1
);
1070 edit_scroll_screen_over_cursor (edit
);
1071 if (edit_search_options
.backwards
)
1072 edit
->search_start
--;
1074 edit
->search_start
++;
1078 edit
->search_start
= edit
->curs1
;
1079 if (edit
->search
->error_str
!= NULL
)
1080 edit_error_dialog (_("Search"), edit
->search
->error_str
);
1084 edit
->force
|= REDRAW_COMPLETELY
;
1085 edit_scroll_screen_over_cursor (edit
);
1088 /* --------------------------------------------------------------------------------------------- */
1091 edit_search (WEdit
* edit
)
1093 if (editcmd_dialog_search_show (edit
))
1095 edit
->search_line_type
= edit_get_search_line_type (edit
->search
);
1096 edit_search_fix_search_start_if_selection (edit
);
1097 edit_do_search (edit
);
1101 /* --------------------------------------------------------------------------------------------- */
1102 /** Return a null terminated length of text. Result must be g_free'd */
1104 static unsigned char *
1105 edit_get_block (WEdit
* edit
, long start
, long finish
, int *l
)
1107 unsigned char *s
, *r
;
1108 r
= s
= g_malloc0 (finish
- start
+ 1);
1109 if (edit
->column_highlight
)
1112 /* copy from buffer, excluding chars that are out of the column 'margins' */
1113 while (start
< finish
)
1117 x
= edit_move_forward3 (edit
, edit_bol (edit
, start
), 0, start
);
1118 c
= edit_get_byte (edit
, start
);
1119 if ((x
>= edit
->column1
&& x
< edit
->column2
)
1120 || (x
>= edit
->column2
&& x
< edit
->column1
) || c
== '\n')
1130 *l
= finish
- start
;
1131 while (start
< finish
)
1132 *s
++ = edit_get_byte (edit
, start
++);
1138 /* --------------------------------------------------------------------------------------------- */
1139 /** copies a block to clipboard file */
1142 edit_save_block_to_clip_file (WEdit
* edit
, long start
, long finish
)
1146 tmp
= mc_config_get_full_path (EDIT_CLIP_FILE
);
1147 ret
= edit_save_block (edit
, tmp
, start
, finish
);
1152 /* --------------------------------------------------------------------------------------------- */
1155 pipe_mail (WEdit
* edit
, char *to
, char *subject
, char *cc
)
1160 to
= name_quote (to
, 0);
1161 subject
= name_quote (subject
, 0);
1162 cc
= name_quote (cc
, 0);
1163 s
= g_strconcat ("mail -s ", subject
, *cc
? " -c " : "", cc
, " ", to
, (char *) NULL
);
1177 for (i
= 0; i
< edit
->last_byte
; i
++)
1178 fputc (edit_get_byte (edit
, i
), p
);
1183 /* --------------------------------------------------------------------------------------------- */
1186 is_break_char (char c
)
1188 return (isspace (c
) || strchr ("{}[]()<>=|/\\!?~-+`'\",.;:#$%^&*", c
));
1191 /* --------------------------------------------------------------------------------------------- */
1192 /** find first character of current word */
1195 edit_find_word_start (WEdit
* edit
, long *word_start
, gsize
* word_len
)
1200 /* return if at begin of file */
1201 if (edit
->curs1
<= 0)
1204 c
= (unsigned char) edit_get_byte (edit
, edit
->curs1
- 1);
1205 /* return if not at end or in word */
1206 if (is_break_char (c
))
1209 /* search start of word to be completed */
1212 /* return if at begin of file */
1213 if ((gsize
) edit
->curs1
< i
)
1217 c
= (unsigned char) edit_get_byte (edit
, edit
->curs1
- i
);
1219 if (is_break_char (c
))
1221 /* return if word starts with digit */
1225 *word_start
= edit
->curs1
- (i
- 1); /* start found */
1234 /* --------------------------------------------------------------------------------------------- */
1236 * Get current word under cursor
1238 * @param edit editor object
1239 * @param srch mc_search object
1240 * @param word_start start word position
1242 * @return newly allocated string or NULL if no any words under cursor
1246 edit_collect_completions_get_current_word (WEdit
* edit
, mc_search_t
* srch
, long word_start
)
1251 if (!mc_search_run (srch
, (void *) edit
, word_start
, edit
->last_byte
, &len
))
1254 temp
= g_string_sized_new (len
);
1256 for (i
= 0; i
< len
; i
++)
1260 chr
= edit_get_byte (edit
, word_start
+ i
);
1262 g_string_append_c (temp
, chr
);
1265 return g_string_free (temp
, temp
->len
== 0);
1268 /* --------------------------------------------------------------------------------------------- */
1269 /** collect the possible completions */
1272 edit_collect_completions (WEdit
* edit
, long word_start
, gsize word_len
,
1273 char *match_expr
, struct selection
*compl, gsize
* num
)
1281 long last_byte
, start
= -1;
1284 srch
= mc_search_new (match_expr
, -1);
1288 if (mc_config_get_bool
1289 (mc_main_config
, CONFIG_APP_SECTION
, "editor_wordcompletion_collect_entire_file", 0))
1291 last_byte
= edit
->last_byte
;
1295 last_byte
= word_start
;
1298 srch
->search_type
= MC_SEARCH_T_REGEX
;
1299 srch
->is_case_sensitive
= TRUE
;
1300 srch
->search_fn
= edit_search_cmd_callback
;
1302 current_word
= edit_collect_completions_get_current_word (edit
, srch
, word_start
);
1304 temp
= g_string_new ("");
1306 /* collect max MAX_WORD_COMPLETIONS completions */
1307 while (mc_search_run (srch
, (void *) edit
, start
+ 1, last_byte
, &len
))
1309 g_string_set_size (temp
, 0);
1310 start
= srch
->normal_offset
;
1312 /* add matched completion if not yet added */
1313 for (i
= 0; i
< len
; i
++)
1315 skip
= edit_get_byte (edit
, start
+ i
);
1319 /* skip current word */
1320 if (start
+ (long) i
== word_start
)
1323 g_string_append_c (temp
, skip
);
1329 if (current_word
!= NULL
&& strcmp (current_word
, temp
->str
) == 0)
1334 for (i
= 0; i
< *num
; i
++)
1337 ((char *) &compl[i
].text
[word_len
],
1338 (char *) &temp
->str
[word_len
], max (len
, compl[i
].len
) - word_len
) == 0)
1340 struct selection
this = compl[i
];
1341 for (++i
; i
< *num
; i
++)
1343 compl[i
- 1] = compl[i
];
1345 compl[*num
- 1] = this;
1347 break; /* skip it, already added */
1353 if (*num
== MAX_WORD_COMPLETIONS
)
1355 g_free (compl[0].text
);
1356 for (i
= 1; i
< *num
; i
++)
1358 compl[i
- 1] = compl[i
];
1365 recoded
= str_convert_to_display (temp
->str
);
1367 if (recoded
&& recoded
->len
)
1368 g_string_assign (temp
, recoded
->str
);
1370 g_string_free (recoded
, TRUE
);
1373 compl[*num
].text
= g_strdup (temp
->str
);
1374 compl[*num
].len
= temp
->len
;
1378 /* note the maximal length needed for the completion dialog */
1383 mc_search_free (srch
);
1384 g_string_free (temp
, TRUE
);
1385 g_free (current_word
);
1390 /* --------------------------------------------------------------------------------------------- */
1393 edit_insert_column_of_text (WEdit
* edit
, unsigned char *data
, int size
, int width
,
1394 long *start_pos
, long *end_pos
, int *col1
, int *col2
)
1399 cursor
= edit
->curs1
;
1400 col
= edit_get_col (edit
);
1402 for (i
= 0; i
< size
; i
++)
1404 if (data
[i
] != '\n')
1405 edit_insert (edit
, data
[i
]);
1407 { /* fill in and move to next line */
1411 if (edit_get_byte (edit
, edit
->curs1
) != '\n')
1413 l
= width
- (edit_get_col (edit
) - col
);
1416 edit_insert (edit
, ' ');
1420 for (p
= edit
->curs1
;; p
++)
1422 if (p
== edit
->last_byte
)
1424 edit_cursor_move (edit
, edit
->last_byte
- edit
->curs1
);
1425 edit_insert_ahead (edit
, '\n');
1429 if (edit_get_byte (edit
, p
) == '\n')
1435 edit_cursor_move (edit
, edit_move_forward3 (edit
, p
, col
, 0) - edit
->curs1
);
1436 l
= col
- edit_get_col (edit
);
1437 while (l
>= space_width
)
1439 edit_insert (edit
, ' ');
1446 *col2
= col
+ width
;
1447 *start_pos
= cursor
;
1448 *end_pos
= edit
->curs1
;
1449 edit_cursor_move (edit
, cursor
- edit
->curs1
);
1452 /* --------------------------------------------------------------------------------------------- */
1455 edit_macro_comparator (gconstpointer
* macro1
, gconstpointer
* macro2
)
1457 const macros_t
*m1
= (const macros_t
*) macro1
;
1458 const macros_t
*m2
= (const macros_t
*) macro2
;
1460 return m1
->hotkey
- m2
->hotkey
;
1463 /* --------------------------------------------------------------------------------------------- */
1466 edit_macro_sort_by_hotkey (void)
1468 if (macros_list
!= NULL
&& macros_list
->len
!= 0)
1469 g_array_sort (macros_list
, (GCompareFunc
) edit_macro_comparator
);
1472 /* --------------------------------------------------------------------------------------------- */
1475 edit_get_macro (WEdit
* edit
, int hotkey
, const macros_t
** macros
, guint
* indx
)
1477 const macros_t
*array_start
= &g_array_index (macros_list
, struct macros_t
, 0);
1479 macros_t search_macro
;
1483 search_macro
.hotkey
= hotkey
;
1484 result
= bsearch (&search_macro
, macros_list
->data
, macros_list
->len
,
1485 sizeof (macros_t
), (GCompareFunc
) edit_macro_comparator
);
1487 if (result
!= NULL
&& result
->macro
!= NULL
)
1489 *indx
= (result
- array_start
);
1497 /* --------------------------------------------------------------------------------------------- */
1498 /** returns FALSE on error */
1501 edit_delete_macro (WEdit
* edit
, int hotkey
)
1503 mc_config_t
*macros_config
= NULL
;
1504 const char *section_name
= "editor";
1505 gchar
*macros_fname
;
1508 const macros_t
*macros
= NULL
;
1510 /* clear array of actions for current hotkey */
1511 while (edit_get_macro (edit
, hotkey
, ¯os
, &indx
))
1513 if (macros
->macro
!= NULL
)
1514 g_array_free (macros
->macro
, TRUE
);
1516 g_array_remove_index (macros_list
, indx
);
1517 edit_macro_sort_by_hotkey ();
1520 macros_fname
= mc_config_get_full_path (MC_MACRO_FILE
);
1521 macros_config
= mc_config_init (macros_fname
);
1522 g_free (macros_fname
);
1524 if (macros_config
== NULL
)
1527 skeyname
= lookup_key_by_code (hotkey
);
1528 while (mc_config_del_key (macros_config
, section_name
, skeyname
))
1531 mc_config_save_file (macros_config
, NULL
);
1532 mc_config_deinit (macros_config
);
1537 /* --------------------------------------------------------------------------------------------- */
1538 /*** public functions ****************************************************************************/
1539 /* --------------------------------------------------------------------------------------------- */
1542 edit_help_cmd (WEdit
* edit
)
1544 ev_help_t event_data
= { NULL
, "[Internal File Editor]" };
1545 mc_event_raise (MCEVENT_GROUP_CORE
, "help", &event_data
);
1547 edit
->force
|= REDRAW_COMPLETELY
;
1550 /* --------------------------------------------------------------------------------------------- */
1553 edit_refresh_cmd (WEdit
* edit
)
1558 edit_get_syntax_color (edit
, -1, &color
);
1559 tty_touch_screen ();
1566 #endif /* !HAVE_SLANG */
1570 /* --------------------------------------------------------------------------------------------- */
1573 menu_save_mode_cmd (void)
1576 const int DLG_X
= 38;
1577 const int DLG_Y
= 13;
1581 const char *str
[] = {
1584 N_("&Do backups with following extension:")
1587 QuickWidget widgets
[] = {
1589 QUICK_BUTTON (18, DLG_X
, DLG_Y
- 3, DLG_Y
, N_("&Cancel"), B_CANCEL
, NULL
),
1591 QUICK_BUTTON (6, DLG_X
, DLG_Y
- 3, DLG_Y
, N_("&OK"), B_ENTER
, NULL
),
1593 QUICK_CHECKBOX (4, DLG_X
, 8, DLG_Y
, N_("Check &POSIX new line"), &option_check_nl_at_eof
),
1595 QUICK_INPUT (8, DLG_X
, 6, DLG_Y
, option_backup_ext
, 9, 0, "edit-backup-ext", &str_result
),
1597 QUICK_RADIO (4, DLG_X
, 3, DLG_Y
, 3, str
, &option_save_mode
),
1601 QuickDialog dialog
= {
1602 DLG_X
, DLG_Y
, -1, -1, N_("Edit Save Mode"),
1603 "[Edit Save Mode]", widgets
, NULL
, FALSE
1608 size_t w0
, w1
, b_len
, w3
;
1610 #ifdef HAVE_ASSERT_H
1611 assert (option_backup_ext
!= NULL
);
1614 /* OK/Cancel buttons */
1615 w0
= str_term_width1 (_(widgets
[0].u
.button
.text
)) + 3;
1616 w1
= str_term_width1 (_(widgets
[1].u
.button
.text
)) + 5; /* default button */
1617 b_len
= w0
+ w1
+ 3;
1619 maxlen
= max (b_len
, (size_t) str_term_width1 (_(dialog
.title
)) + 2);
1622 for (i
= 0; i
< 3; i
++)
1627 w3
= max (w3
, (size_t) str_term_width1 (str
[i
]));
1630 maxlen
= max (maxlen
, w3
+ 4);
1632 dialog
.xlen
= min ((size_t) COLS
, maxlen
+ 8);
1634 widgets
[3].u
.input
.len
= w3
;
1635 widgets
[1].relative_x
= (dialog
.xlen
- b_len
) / 2;
1636 widgets
[0].relative_x
= widgets
[1].relative_x
+ w0
+ 2;
1638 for (i
= 0; i
< sizeof (widgets
) / sizeof (widgets
[0]); i
++)
1639 widgets
[i
].x_divisions
= dialog
.xlen
;
1641 if (quick_dialog (&dialog
) != B_CANCEL
)
1643 g_free (option_backup_ext
);
1644 option_backup_ext
= str_result
;
1648 /* --------------------------------------------------------------------------------------------- */
1651 edit_set_filename (WEdit
* edit
, const char *name
)
1653 g_free (edit
->filename
);
1658 edit
->filename
= tilde_expand (name
);
1659 if (edit
->dir
== NULL
&& !g_path_is_absolute (name
))
1660 edit
->dir
= vfs_get_current_dir ();
1663 /* --------------------------------------------------------------------------------------------- */
1664 /* Here we want to warn the users of overwriting an existing file,
1665 but only if they have made a change to the filename */
1666 /* returns 1 on success */
1668 edit_save_as_cmd (WEdit
* edit
)
1670 /* This heads the 'Save As' dialog box */
1673 int different_filename
= 0;
1675 if (!edit_check_newline (edit
))
1678 exp
= edit_get_save_file_as (edit
);
1679 edit_push_undo_action (edit
, KEY_PRESS
+ edit
->start_display
);
1686 edit
->force
|= REDRAW_COMPLETELY
;
1693 if (strcmp (edit
->filename
, exp
))
1696 vfs_path_t
*tmp_vpath
;
1699 tmp_vpath
= vfs_path_from_str (exp
);
1700 if (mc_stat (tmp_vpath
, &sb
) == 0 && !S_ISREG (sb
.st_mode
))
1702 edit_error_dialog (_("Save as"),
1704 ("Cannot save: destination is not a regular file")));
1706 edit
->force
|= REDRAW_COMPLETELY
;
1707 vfs_path_free (tmp_vpath
);
1711 different_filename
= 1;
1712 file
= mc_open (tmp_vpath
, O_RDONLY
| O_BINARY
);
1713 vfs_path_free (tmp_vpath
);
1716 /* the file exists */
1718 /* Overwrite the current file or cancel the operation */
1719 if (edit_query_dialog2
1721 _("A file already exists with this name"), _("&Overwrite"), _("&Cancel")))
1723 edit
->force
|= REDRAW_COMPLETELY
;
1730 edit
->stat1
.st_mode
|= S_IWUSR
;
1732 save_lock
= lock_file (exp
);
1736 /* filenames equal, check if already locked */
1737 if (!edit
->locked
&& !edit
->delete_file
)
1738 save_lock
= lock_file (exp
);
1741 if (different_filename
)
1744 * Allow user to write into saved (under another name) file
1745 * even if original file had r/o user permissions.
1747 edit
->stat1
.st_mode
|= S_IWRITE
;
1750 rv
= edit_save_file (edit
, exp
);
1754 /* Succesful, so unlock both files */
1755 if (different_filename
)
1760 edit
->locked
= edit_unlock_file (edit
);
1764 if (edit
->locked
|| save_lock
)
1765 edit
->locked
= edit_unlock_file (edit
);
1768 edit_set_filename (edit
, exp
);
1769 if (edit
->lb
!= LB_ASIS
)
1770 edit_reload (edit
, exp
);
1773 edit
->delete_file
= 0;
1774 if (different_filename
)
1775 edit_load_syntax (edit
, NULL
, edit
->syntax_type
);
1776 edit
->force
|= REDRAW_COMPLETELY
;
1779 edit_error_dialog (_("Save as"), get_sys_error (_("Cannot save file")));
1782 /* Failed, so maintain modify (not save) lock */
1786 edit
->force
|= REDRAW_COMPLETELY
;
1791 edit
->force
|= REDRAW_COMPLETELY
;
1795 /* {{{ Macro stuff starts here */
1796 /* --------------------------------------------------------------------------------------------- */
1799 edit_delete_macro_cmd (WEdit
* edit
)
1803 hotkey
= editcmd_dialog_raw_key_query (_("Delete macro"), _("Press macro hotkey:"), 1);
1805 if (hotkey
!= 0 && !edit_delete_macro (edit
, hotkey
))
1806 message (D_ERROR
, _("Delete macro"), _("Macro not deleted"));
1809 /* --------------------------------------------------------------------------------------------- */
1811 /** returns FALSE on error */
1813 edit_execute_macro (WEdit
* edit
, int hotkey
)
1815 gboolean res
= FALSE
;
1819 const macros_t
*macros
;
1822 if (edit_get_macro (edit
, hotkey
, ¯os
, &indx
) &&
1823 macros
->macro
!= NULL
&& macros
->macro
->len
!= 0)
1827 edit
->force
|= REDRAW_PAGE
;
1829 for (i
= 0; i
< macros
->macro
->len
; i
++)
1831 const macro_action_t
*m_act
;
1833 m_act
= &g_array_index (macros
->macro
, struct macro_action_t
, i
);
1834 edit_execute_cmd (edit
, m_act
->action
, m_act
->ch
);
1843 /* --------------------------------------------------------------------------------------------- */
1845 /** returns FALSE on error */
1847 edit_store_macro_cmd (WEdit
* edit
)
1851 GString
*marcros_string
;
1852 mc_config_t
*macros_config
= NULL
;
1853 const char *section_name
= "editor";
1854 gchar
*macros_fname
;
1855 GArray
*macros
; /* current macro */
1857 gboolean have_macro
= FALSE
;
1858 char *skeyname
= NULL
;
1860 hotkey
= editcmd_dialog_raw_key_query (_("Save macro"), _("Press the macro's new hotkey:"), 1);
1861 if (hotkey
== ESC_CHAR
)
1864 tmp_act
= keybind_lookup_keymap_command (editor_map
, hotkey
);
1866 /* return FALSE if try assign macro into restricted hotkeys */
1867 if (tmp_act
== CK_MacroStartRecord
1868 || tmp_act
== CK_MacroStopRecord
|| tmp_act
== CK_MacroStartStopRecord
)
1871 edit_delete_macro (edit
, hotkey
);
1873 macros_fname
= mc_config_get_full_path (MC_MACRO_FILE
);
1874 macros_config
= mc_config_init (macros_fname
);
1875 g_free (macros_fname
);
1877 if (macros_config
== NULL
)
1880 edit_push_undo_action (edit
, KEY_PRESS
+ edit
->start_display
);
1882 marcros_string
= g_string_sized_new (250);
1883 macros
= g_array_new (TRUE
, FALSE
, sizeof (macro_action_t
));
1885 skeyname
= lookup_key_by_code (hotkey
);
1887 for (i
= 0; i
< macro_index
; i
++)
1889 macro_action_t m_act
;
1890 const char *action_name
;
1892 action_name
= keybind_lookup_actionname (record_macro_buf
[i
].action
);
1894 if (action_name
== NULL
)
1897 m_act
.action
= record_macro_buf
[i
].action
;
1898 m_act
.ch
= record_macro_buf
[i
].ch
;
1899 g_array_append_val (macros
, m_act
);
1901 g_string_append_printf (marcros_string
, "%s:%i;", action_name
,
1902 (int) record_macro_buf
[i
].ch
);
1907 macro
.hotkey
= hotkey
;
1908 macro
.macro
= macros
;
1909 g_array_append_val (macros_list
, macro
);
1910 mc_config_set_string (macros_config
, section_name
, skeyname
, marcros_string
->str
);
1913 mc_config_del_key (macros_config
, section_name
, skeyname
);
1916 edit_macro_sort_by_hotkey ();
1918 g_string_free (marcros_string
, TRUE
);
1919 mc_config_save_file (macros_config
, NULL
);
1920 mc_config_deinit (macros_config
);
1924 /* --------------------------------------------------------------------------------------------- */
1927 edit_repeat_macro_cmd (WEdit
* edit
)
1934 f
= input_dialog (_("Repeat last commands"), _("Repeat times:"), MC_HISTORY_EDIT_REPEAT
, NULL
);
1935 if (f
== NULL
|| *f
== '\0')
1941 count_repeat
= strtol (f
, &error
, 0);
1951 edit_push_undo_action (edit
, KEY_PRESS
+ edit
->start_display
);
1952 edit
->force
|= REDRAW_PAGE
;
1954 for (j
= 0; j
< count_repeat
; j
++)
1955 for (i
= 0; i
< macro_index
; i
++)
1956 edit_execute_cmd (edit
, record_macro_buf
[i
].action
, record_macro_buf
[i
].ch
);
1957 edit_update_screen (edit
);
1961 /* --------------------------------------------------------------------------------------------- */
1962 /** return FALSE on error */
1965 edit_load_macro_cmd (WEdit
* edit
)
1967 mc_config_t
*macros_config
= NULL
;
1968 gchar
**profile_keys
, **keys
;
1969 gchar
**values
, **curr_values
;
1970 gsize len
, values_len
;
1971 const char *section_name
= "editor";
1972 gchar
*macros_fname
;
1977 macros_fname
= mc_config_get_full_path (MC_MACRO_FILE
);
1978 macros_config
= mc_config_init (macros_fname
);
1979 g_free (macros_fname
);
1981 if (macros_config
== NULL
)
1984 profile_keys
= keys
= mc_config_get_keys (macros_config
, section_name
, &len
);
1985 while (*profile_keys
!= NULL
)
1987 gboolean have_macro
;
1991 macros
= g_array_new (TRUE
, FALSE
, sizeof (macro_action_t
));
1993 curr_values
= values
= mc_config_get_string_list (macros_config
, section_name
,
1994 *profile_keys
, &values_len
);
1995 hotkey
= lookup_key (*profile_keys
, NULL
);
1998 while (*curr_values
!= NULL
&& *curr_values
[0] != '\0')
2000 char **macro_pair
= NULL
;
2002 macro_pair
= g_strsplit (*curr_values
, ":", 2);
2004 if (macro_pair
!= NULL
)
2006 macro_action_t m_act
;
2007 if (macro_pair
[0] == NULL
|| macro_pair
[0][0] == '\0')
2011 m_act
.action
= keybind_lookup_action (macro_pair
[0]);
2012 g_free (macro_pair
[0]);
2013 macro_pair
[0] = NULL
;
2015 if (macro_pair
[1] == NULL
|| macro_pair
[1][0] == '\0')
2019 m_act
.ch
= strtol (macro_pair
[1], NULL
, 0);
2020 g_free (macro_pair
[1]);
2021 macro_pair
[1] = NULL
;
2023 if (m_act
.action
!= 0)
2025 /* a shell command */
2026 if ((m_act
.action
/ CK_PipeBlock (0)) == 1)
2028 m_act
.action
= CK_PipeBlock (0) + (m_act
.ch
> 0 ? m_act
.ch
: 0);
2031 g_array_append_val (macros
, m_act
);
2034 g_strfreev (macro_pair
);
2041 macro
.hotkey
= hotkey
;
2042 macro
.macro
= macros
;
2043 g_array_append_val (macros_list
, macro
);
2046 g_strfreev (values
);
2049 mc_config_deinit (macros_config
);
2050 edit_macro_sort_by_hotkey ();
2054 /* }}} Macro stuff end here */
2056 /* --------------------------------------------------------------------------------------------- */
2057 /** returns 1 on success */
2060 edit_save_confirm_cmd (WEdit
* edit
)
2064 if (!edit_check_newline (edit
))
2067 if (edit_confirm_save
)
2069 f
= g_strdup_printf (_("Confirm save file: \"%s\""), edit
->filename
);
2070 if (edit_query_dialog2 (_("Save file"), f
, _("&Save"), _("&Cancel")))
2077 return edit_save_cmd (edit
);
2080 /* --------------------------------------------------------------------------------------------- */
2081 /** returns 1 on success */
2084 edit_new_cmd (WEdit
* edit
)
2088 if (edit_query_dialog2
2091 ("Current text was modified without a file save.\nContinue discards these changes"),
2092 _("C&ontinue"), _("&Cancel")))
2094 edit
->force
|= REDRAW_COMPLETELY
;
2098 edit
->force
|= REDRAW_COMPLETELY
;
2100 return edit_renew (edit
); /* if this gives an error, something has really screwed up */
2103 /* --------------------------------------------------------------------------------------------- */
2106 edit_load_cmd (WEdit
* edit
, edit_current_file_t what
)
2111 && (edit_query_dialog2
2113 _("Current text was modified without a file save.\n"
2114 "Continue discards these changes"), _("C&ontinue"), _("&Cancel")) == 1))
2116 edit
->force
|= REDRAW_COMPLETELY
;
2122 case EDIT_FILE_COMMON
:
2123 exp
= input_expand_dialog (_("Load"), _("Enter file name:"),
2124 MC_HISTORY_EDIT_LOAD
, edit
->filename
);
2129 edit_load_file_from_filename (edit
, exp
);
2134 case EDIT_FILE_SYNTAX
:
2135 edit_load_syntax_file (edit
);
2138 case EDIT_FILE_MENU
:
2139 edit_load_menu_file (edit
);
2146 edit
->force
|= REDRAW_COMPLETELY
;
2150 /* --------------------------------------------------------------------------------------------- */
2152 if mark2 is -1 then marking is from mark1 to the cursor.
2153 Otherwise its between the markers. This handles this.
2154 Returns 1 if no text is marked.
2158 eval_marks (WEdit
* edit
, long *start_mark
, long *end_mark
)
2160 if (edit
->mark1
!= edit
->mark2
)
2162 long start_bol
, start_eol
;
2163 long end_bol
, end_eol
;
2168 if (edit
->end_mark_curs
< 0)
2169 end_mark_curs
= edit
->curs1
;
2171 end_mark_curs
= edit
->end_mark_curs
;
2173 if (edit
->mark2
>= 0)
2175 *start_mark
= min (edit
->mark1
, edit
->mark2
);
2176 *end_mark
= max (edit
->mark1
, edit
->mark2
);
2180 *start_mark
= min (edit
->mark1
, end_mark_curs
);
2181 *end_mark
= max (edit
->mark1
, end_mark_curs
);
2182 edit
->column2
= edit
->curs_col
+ edit
->over_col
;
2185 if (edit
->column_highlight
2186 && (((edit
->mark1
> end_mark_curs
) && (edit
->column1
< edit
->column2
))
2187 || ((edit
->mark1
< end_mark_curs
) && (edit
->column1
> edit
->column2
))))
2189 start_bol
= edit_bol (edit
, *start_mark
);
2190 start_eol
= edit_eol (edit
, start_bol
- 1) + 1;
2191 end_bol
= edit_bol (edit
, *end_mark
);
2192 end_eol
= edit_eol (edit
, *end_mark
);
2193 col1
= min (edit
->column1
, edit
->column2
);
2194 col2
= max (edit
->column1
, edit
->column2
);
2197 edit_move_forward3 (edit
, start_bol
, col2
, 0) - edit_move_forward3 (edit
, start_bol
,
2200 edit_move_forward3 (edit
, end_bol
, col2
, 0) - edit_move_forward3 (edit
, end_bol
,
2203 *start_mark
-= diff1
;
2205 *start_mark
= max (*start_mark
, start_eol
);
2206 *end_mark
= min (*end_mark
, end_eol
);
2212 *start_mark
= *end_mark
= 0;
2213 edit
->column2
= edit
->column1
= 0;
2218 /* --------------------------------------------------------------------------------------------- */
2221 edit_insert_over (WEdit
* edit
)
2225 for (i
= 0; i
< edit
->over_col
; i
++)
2227 edit_insert (edit
, ' ');
2232 /* --------------------------------------------------------------------------------------------- */
2235 edit_insert_column_of_text_from_file (WEdit
* edit
, int file
,
2236 long *start_pos
, long *end_pos
, int *col1
, int *col2
)
2240 int blocklen
= -1, width
= 0;
2241 unsigned char *data
;
2243 cursor
= edit
->curs1
;
2244 col
= edit_get_col (edit
);
2245 data
= g_malloc0 (TEMP_BUF_LEN
);
2247 while ((blocklen
= mc_read (file
, (char *) data
, TEMP_BUF_LEN
)) > 0)
2250 for (width
= 0; width
< blocklen
; width
++)
2252 if (data
[width
] == '\n')
2255 for (i
= 0; i
< blocklen
; i
++)
2257 if (data
[i
] == '\n')
2258 { /* fill in and move to next line */
2261 if (edit_get_byte (edit
, edit
->curs1
) != '\n')
2263 l
= width
- (edit_get_col (edit
) - col
);
2266 edit_insert (edit
, ' ');
2270 for (p
= edit
->curs1
;; p
++)
2272 if (p
== edit
->last_byte
)
2274 edit_cursor_move (edit
, edit
->last_byte
- edit
->curs1
);
2275 edit_insert_ahead (edit
, '\n');
2279 if (edit_get_byte (edit
, p
) == '\n')
2285 edit_cursor_move (edit
, edit_move_forward3 (edit
, p
, col
, 0) - edit
->curs1
);
2286 l
= col
- edit_get_col (edit
);
2287 while (l
>= space_width
)
2289 edit_insert (edit
, ' ');
2294 edit_insert (edit
, data
[i
]);
2298 *col2
= col
+ width
;
2299 *start_pos
= cursor
;
2300 *end_pos
= edit
->curs1
;
2301 edit_cursor_move (edit
, cursor
- edit
->curs1
);
2307 /* --------------------------------------------------------------------------------------------- */
2310 edit_block_copy_cmd (WEdit
* edit
)
2312 long start_mark
, end_mark
, current
= edit
->curs1
;
2317 unsigned char *copy_buf
;
2319 edit_update_curs_col (edit
);
2320 if (eval_marks (edit
, &start_mark
, &end_mark
))
2323 copy_buf
= edit_get_block (edit
, start_mark
, end_mark
, &size
);
2325 /* all that gets pushed are deletes hence little space is used on the stack */
2327 edit_push_markers (edit
);
2329 if (edit
->column_highlight
)
2331 col_delta
= abs (edit
->column2
- edit
->column1
);
2332 edit_insert_column_of_text (edit
, copy_buf
, size
, col_delta
, &mark1
, &mark2
, &c1
, &c2
);
2337 edit_insert_ahead (edit
, copy_buf
[size
]);
2341 edit_scroll_screen_over_cursor (edit
);
2343 if (edit
->column_highlight
)
2344 edit_set_markers (edit
, edit
->curs1
, mark2
, c1
, c2
);
2345 else if (start_mark
< current
&& end_mark
> current
)
2346 edit_set_markers (edit
, start_mark
, end_mark
+ end_mark
- start_mark
, 0, 0);
2348 edit
->force
|= REDRAW_PAGE
;
2352 /* --------------------------------------------------------------------------------------------- */
2355 edit_block_move_cmd (WEdit
* edit
)
2358 unsigned char *copy_buf
= NULL
;
2359 long start_mark
, end_mark
;
2362 if (eval_marks (edit
, &start_mark
, &end_mark
))
2365 line
= edit
->curs_line
;
2366 if (edit
->mark2
< 0)
2367 edit_mark_cmd (edit
, 0);
2368 edit_push_markers (edit
);
2370 if (edit
->column_highlight
)
2378 c1
= min (edit
->column1
, edit
->column2
);
2379 c2
= max (edit
->column1
, edit
->column2
);
2380 b_width
= (c2
- c1
);
2382 edit_update_curs_col (edit
);
2385 x2
= x
+ edit
->over_col
;
2387 /* do nothing when cursor inside first line of selected area */
2388 if ((edit_eol (edit
, edit
->curs1
) == edit_eol (edit
, start_mark
)) && (x2
> c1
&& x2
<= c2
))
2391 if (edit
->curs1
> start_mark
&& edit
->curs1
< edit_eol (edit
, end_mark
))
2395 else if (x
> c1
&& x
<= c2
)
2398 /* save current selection into buffer */
2399 copy_buf
= edit_get_block (edit
, start_mark
, end_mark
, &size
);
2401 /* remove current selection */
2402 edit_block_delete_cmd (edit
);
2404 edit
->over_col
= max (0, edit
->over_col
- b_width
);
2405 /* calculate the cursor pos after delete block */
2406 current
= edit_move_forward3 (edit
, edit_bol (edit
, edit
->curs1
), x
, 0);
2407 edit_cursor_move (edit
, current
- edit
->curs1
);
2408 edit_scroll_screen_over_cursor (edit
);
2410 /* add TWS if need before block insertion */
2411 if (option_cursor_beyond_eol
&& edit
->over_col
> 0)
2412 edit_insert_over (edit
);
2414 edit_insert_column_of_text (edit
, copy_buf
, size
, b_width
, &mark1
, &mark2
, &c1
, &c2
);
2415 edit_set_markers (edit
, mark1
, mark2
, c1
, c2
);
2421 current
= edit
->curs1
;
2422 copy_buf
= g_malloc0 (end_mark
- start_mark
);
2423 edit_cursor_move (edit
, start_mark
- edit
->curs1
);
2424 edit_scroll_screen_over_cursor (edit
);
2426 while (count
< end_mark
)
2428 copy_buf
[end_mark
- count
- 1] = edit_delete (edit
, 1);
2431 edit_scroll_screen_over_cursor (edit
);
2432 edit_cursor_move (edit
,
2433 current
- edit
->curs1
-
2434 (((current
- edit
->curs1
) > 0) ? end_mark
- start_mark
: 0));
2435 edit_scroll_screen_over_cursor (edit
);
2436 while (count
-- > start_mark
)
2437 edit_insert_ahead (edit
, copy_buf
[end_mark
- count
- 1]);
2438 edit_set_markers (edit
, edit
->curs1
, edit
->curs1
+ end_mark
- start_mark
, 0, 0);
2441 edit_scroll_screen_over_cursor (edit
);
2443 edit
->force
|= REDRAW_PAGE
;
2446 /* --------------------------------------------------------------------------------------------- */
2447 /** returns 1 if canceelled by user */
2450 edit_block_delete_cmd (WEdit
* edit
)
2452 long start_mark
, end_mark
;
2453 if (eval_marks (edit
, &start_mark
, &end_mark
))
2455 edit_delete_line (edit
);
2458 return edit_block_delete (edit
);
2461 /* --------------------------------------------------------------------------------------------- */
2462 /** call with edit = 0 before shutdown to close memory leaks */
2465 edit_replace_cmd (WEdit
* edit
, int again
)
2467 /* 1 = search string, 2 = replace with */
2468 static char *saved1
= NULL
; /* saved default[123] */
2469 static char *saved2
= NULL
;
2470 char *input1
= NULL
; /* user input from the dialog */
2471 char *input2
= NULL
;
2472 GString
*input2_str
= NULL
;
2475 long times_replaced
= 0;
2476 gboolean once_found
= FALSE
;
2480 g_free (saved1
), saved1
= NULL
;
2481 g_free (saved2
), saved2
= NULL
;
2485 edit
->force
|= REDRAW_COMPLETELY
;
2487 if (again
&& !saved1
&& !saved2
)
2492 input1
= g_strdup (saved1
? saved1
: "");
2493 input2
= g_strdup (saved2
? saved2
: "");
2497 char *tmp_inp1
, *tmp_inp2
;
2499 disp1
= edit_replace_cmd__conv_to_display (saved1
? saved1
: (char *) "");
2500 disp2
= edit_replace_cmd__conv_to_display (saved2
? saved2
: (char *) "");
2502 edit_push_undo_action (edit
, KEY_PRESS
+ edit
->start_display
);
2504 editcmd_dialog_replace_show (edit
, disp1
, disp2
, &input1
, &input2
);
2509 if (input1
== NULL
|| *input1
== '\0')
2511 edit
->force
= REDRAW_COMPLETELY
;
2517 input1
= edit_replace_cmd__conv_to_input (input1
);
2518 input2
= edit_replace_cmd__conv_to_input (input2
);
2522 g_free (saved1
), saved1
= g_strdup (input1
);
2523 g_free (saved2
), saved2
= g_strdup (input2
);
2525 mc_search_free (edit
->search
);
2526 edit
->search
= NULL
;
2529 input2_str
= g_string_new (input2
);
2533 edit
->search
= mc_search_new (input1
, -1);
2534 if (edit
->search
== NULL
)
2536 edit
->search_start
= edit
->curs1
;
2539 edit
->search
->search_type
= edit_search_options
.type
;
2540 edit
->search
->is_all_charsets
= edit_search_options
.all_codepages
;
2541 edit
->search
->is_case_sensitive
= edit_search_options
.case_sens
;
2542 edit
->search
->whole_words
= edit_search_options
.whole_words
;
2543 edit
->search
->search_fn
= edit_search_cmd_callback
;
2544 edit
->search_line_type
= edit_get_search_line_type (edit
->search
);
2545 edit_search_fix_search_start_if_selection (edit
);
2548 if (edit
->found_len
&& edit
->search_start
== edit
->found_start
+ 1
2549 && edit_search_options
.backwards
)
2550 edit
->search_start
--;
2552 if (edit
->found_len
&& edit
->search_start
== edit
->found_start
- 1
2553 && !edit_search_options
.backwards
)
2554 edit
->search_start
++;
2560 if (!editcmd_find (edit
, &len
))
2562 if (!(edit
->search
->error
== MC_SEARCH_E_OK
||
2563 (once_found
&& edit
->search
->error
== MC_SEARCH_E_NOTFOUND
)))
2565 edit_error_dialog (_("Search"), edit
->search
->error_str
);
2571 edit
->search_start
= edit
->search
->normal_offset
;
2572 /*returns negative on not found or error in pattern */
2574 if ((edit
->search_start
>= 0) && (edit
->search_start
< edit
->last_byte
))
2579 edit
->found_start
= edit
->search_start
;
2580 i
= edit
->found_len
= len
;
2582 edit_cursor_move (edit
, edit
->search_start
- edit
->curs1
);
2583 edit_scroll_screen_over_cursor (edit
);
2585 if (edit
->replace_mode
== 0)
2590 l
= edit
->curs_row
- edit
->widget
.lines
/ 3;
2592 edit_scroll_downward (edit
, l
);
2594 edit_scroll_upward (edit
, -l
);
2596 edit_scroll_screen_over_cursor (edit
);
2597 edit
->force
|= REDRAW_PAGE
;
2598 edit_render_keypress (edit
);
2600 /*so that undo stops at each query */
2601 edit_push_key_press (edit
);
2602 /* and prompt 2/3 down */
2603 disp1
= edit_replace_cmd__conv_to_display (saved1
);
2604 disp2
= edit_replace_cmd__conv_to_display (saved2
);
2605 prompt
= editcmd_dialog_replace_prompt_show (edit
, disp1
, disp2
, -1, -1);
2609 if (prompt
== B_REPLACE_ALL
)
2610 edit
->replace_mode
= 1;
2611 else if (prompt
== B_SKIP_REPLACE
)
2613 if (edit_search_options
.backwards
)
2614 edit
->search_start
--;
2616 edit
->search_start
++;
2617 continue; /* loop */
2619 else if (prompt
== B_CANCEL
)
2621 edit
->replace_mode
= -1;
2626 repl_str
= mc_search_prepare_replace_str (edit
->search
, input2_str
);
2628 if (edit
->search
->error
!= MC_SEARCH_E_OK
)
2630 edit_error_dialog (_("Replace"), edit
->search
->error_str
);
2631 g_string_free (repl_str
, TRUE
);
2635 /* delete then insert new */
2636 for (i
= 0; i
< len
; i
++)
2637 edit_delete (edit
, 1);
2639 for (i
= 0; i
< repl_str
->len
; i
++)
2640 edit_insert (edit
, repl_str
->str
[i
]);
2642 edit
->found_len
= repl_str
->len
;
2643 g_string_free (repl_str
, TRUE
);
2646 /* so that we don't find the same string again */
2647 if (edit_search_options
.backwards
)
2649 edit
->search_start
--;
2653 edit
->search_start
+= edit
->found_len
+ (len
== 0 ? 1 : 0);
2655 if (edit
->search_start
>= edit
->last_byte
)
2659 edit_scroll_screen_over_cursor (edit
);
2663 /* try and find from right here for next search */
2664 edit
->search_start
= edit
->curs1
;
2665 edit_update_curs_col (edit
);
2667 edit
->force
|= REDRAW_PAGE
;
2668 edit_render_keypress (edit
);
2670 if (times_replaced
== 0)
2671 query_dialog (_("Replace"), _("Search string not found"), D_NORMAL
, 1, _("&OK"));
2675 while (edit
->replace_mode
>= 0);
2677 edit_scroll_screen_over_cursor (edit
);
2678 edit
->force
|= REDRAW_COMPLETELY
;
2679 edit_render_keypress (edit
);
2681 if ((edit
->replace_mode
== 1) && (times_replaced
!= 0))
2682 message (D_NORMAL
, _("Replace"), _("%ld replacements made"), times_replaced
);
2687 if (input2_str
!= NULL
)
2688 g_string_free (input2_str
, TRUE
);
2691 /* --------------------------------------------------------------------------------------------- */
2694 edit_search_cmd_callback (const void *user_data
, gsize char_offset
)
2696 return edit_get_byte ((WEdit
*) user_data
, (long) char_offset
);
2699 /* --------------------------------------------------------------------------------------------- */
2702 edit_search_cmd (WEdit
* edit
, gboolean again
)
2710 else if (edit
->last_search_string
!= NULL
)
2711 edit_do_search (edit
);
2714 /* find last search string in history */
2717 history
= history_get (MC_HISTORY_SHARED_SEARCH
);
2718 if (history
!= NULL
&& history
->data
!= NULL
)
2720 edit
->last_search_string
= (char *) history
->data
;
2721 history
->data
= NULL
;
2722 history
= g_list_first (history
);
2723 g_list_foreach (history
, (GFunc
) g_free
, NULL
);
2724 g_list_free (history
);
2726 edit
->search
= mc_search_new (edit
->last_search_string
, -1);
2727 if (edit
->search
== NULL
)
2729 /* if not... then ask for an expression */
2730 g_free (edit
->last_search_string
);
2731 edit
->last_search_string
= NULL
;
2736 edit
->search
->search_type
= edit_search_options
.type
;
2737 edit
->search
->is_all_charsets
= edit_search_options
.all_codepages
;
2738 edit
->search
->is_case_sensitive
= edit_search_options
.case_sens
;
2739 edit
->search
->whole_words
= edit_search_options
.whole_words
;
2740 edit
->search
->search_fn
= edit_search_cmd_callback
;
2741 edit
->search_line_type
= edit_get_search_line_type (edit
->search
);
2742 edit_do_search (edit
);
2747 /* if not... then ask for an expression */
2748 g_free (edit
->last_search_string
);
2749 edit
->last_search_string
= NULL
;
2756 /* --------------------------------------------------------------------------------------------- */
2758 * Check if it's OK to close the editor. If there are unsaved changes,
2759 * ask user. Return 1 if it's OK to exit, 0 to continue editing.
2763 edit_ok_to_exit (WEdit
* edit
)
2767 if (!edit
->modified
)
2770 if (!mc_global
.midnight_shutdown
)
2772 if (!edit_check_newline (edit
))
2776 act
= edit_query_dialog3 (_("Quit"), _("File was modified. Save with exit?"),
2777 _("&Yes"), _("&No"), _("&Cancel quit"));
2782 edit_query_dialog2 (_("Quit"),
2783 _("Midnight Commander is being shut down.\nSave modified file?"),
2784 _("&Yes"), _("&No"));
2794 edit_push_markers (edit
);
2795 edit_set_markers (edit
, 0, 0, 0, 0);
2796 if (!edit_save_cmd (edit
) || mc_global
.midnight_shutdown
)
2797 return mc_global
.midnight_shutdown
;
2801 case 2: /* Cancel quit */
2809 /* --------------------------------------------------------------------------------------------- */
2810 /** save block, returns 1 on success */
2813 edit_save_block (WEdit
* edit
, const char *filename
, long start
, long finish
)
2818 vpath
= vfs_path_from_str (filename
);
2819 file
= mc_open (vpath
, O_CREAT
| O_WRONLY
| O_TRUNC
,
2820 S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IROTH
| O_BINARY
);
2821 vfs_path_free (vpath
);
2825 if (edit
->column_highlight
)
2829 r
= mc_write (file
, VERTICAL_MAGIC
, sizeof (VERTICAL_MAGIC
));
2832 unsigned char *block
, *p
;
2834 p
= block
= edit_get_block (edit
, start
, finish
, &len
);
2837 r
= mc_write (file
, p
, len
);
2851 len
= finish
- start
;
2852 buf
= g_malloc0 (TEMP_BUF_LEN
);
2853 while (start
!= finish
)
2855 end
= min (finish
, start
+ TEMP_BUF_LEN
);
2856 for (; i
< end
; i
++)
2857 buf
[i
- start
] = edit_get_byte (edit
, i
);
2858 len
-= mc_write (file
, (char *) buf
, end
- start
);
2869 /* --------------------------------------------------------------------------------------------- */
2872 edit_paste_from_history (WEdit
* edit
)
2875 edit_error_dialog (_("Error"), _("This function is not implemented"));
2878 /* --------------------------------------------------------------------------------------------- */
2881 edit_copy_to_X_buf_cmd (WEdit
* edit
)
2883 long start_mark
, end_mark
;
2884 if (eval_marks (edit
, &start_mark
, &end_mark
))
2886 if (!edit_save_block_to_clip_file (edit
, start_mark
, end_mark
))
2888 edit_error_dialog (_("Copy to clipboard"), get_sys_error (_("Unable to save to file")));
2891 /* try use external clipboard utility */
2892 mc_event_raise (MCEVENT_GROUP_CORE
, "clipboard_file_to_ext_clip", NULL
);
2897 /* --------------------------------------------------------------------------------------------- */
2900 edit_cut_to_X_buf_cmd (WEdit
* edit
)
2902 long start_mark
, end_mark
;
2903 if (eval_marks (edit
, &start_mark
, &end_mark
))
2905 if (!edit_save_block_to_clip_file (edit
, start_mark
, end_mark
))
2907 edit_error_dialog (_("Cut to clipboard"), _("Unable to save to file"));
2910 /* try use external clipboard utility */
2911 mc_event_raise (MCEVENT_GROUP_CORE
, "clipboard_file_to_ext_clip", NULL
);
2913 edit_block_delete_cmd (edit
);
2914 edit_mark_cmd (edit
, 1);
2918 /* --------------------------------------------------------------------------------------------- */
2921 edit_paste_from_X_buf_cmd (WEdit
* edit
)
2924 /* try use external clipboard utility */
2925 mc_event_raise (MCEVENT_GROUP_CORE
, "clipboard_file_from_ext_clip", NULL
);
2926 tmp
= mc_config_get_full_path (EDIT_CLIP_FILE
);
2927 edit_insert_file (edit
, tmp
);
2932 /* --------------------------------------------------------------------------------------------- */
2934 * Ask user for the line and go to that line.
2935 * Negative numbers mean line from the end (i.e. -1 is the last line).
2939 edit_goto_cmd (WEdit
* edit
)
2942 static long line
= 0; /* line as typed, saved as default */
2947 g_snprintf (s
, sizeof (s
), "%ld", line
);
2948 f
= input_dialog (_("Goto line"), _("Enter line:"), MC_HISTORY_EDIT_GOTO_LINE
, line
? s
: "");
2958 l
= strtol (f
, &error
, 0);
2967 l
= edit
->total_lines
+ l
+ 2;
2968 edit_move_display (edit
, l
- edit
->widget
.lines
/ 2 - 1);
2969 edit_move_to_line (edit
, l
- 1);
2970 edit
->force
|= REDRAW_COMPLETELY
;
2975 /* --------------------------------------------------------------------------------------------- */
2976 /** Return 1 on success */
2979 edit_save_block_cmd (WEdit
* edit
)
2981 long start_mark
, end_mark
;
2984 if (eval_marks (edit
, &start_mark
, &end_mark
))
2987 tmp
= mc_config_get_full_path (EDIT_CLIP_FILE
);
2989 input_expand_dialog (_("Save block"), _("Enter file name:"),
2990 MC_HISTORY_EDIT_SAVE_BLOCK
, tmp
);
2992 edit_push_undo_action (edit
, KEY_PRESS
+ edit
->start_display
);
3002 if (edit_save_block (edit
, exp
, start_mark
, end_mark
))
3005 edit
->force
|= REDRAW_COMPLETELY
;
3011 edit_error_dialog (_("Save block"), get_sys_error (_("Cannot save file")));
3015 edit
->force
|= REDRAW_COMPLETELY
;
3020 /* --------------------------------------------------------------------------------------------- */
3021 /** returns TRUE on success */
3024 edit_insert_file_cmd (WEdit
* edit
)
3028 gboolean ret
= FALSE
;
3030 tmp
= mc_config_get_full_path (EDIT_CLIP_FILE
);
3031 exp
= input_expand_dialog (_("Insert file"), _("Enter file name:"),
3032 MC_HISTORY_EDIT_INSERT_FILE
, tmp
);
3035 edit_push_undo_action (edit
, KEY_PRESS
+ edit
->start_display
);
3037 if (exp
!= NULL
&& *exp
!= '\0')
3039 ret
= (edit_insert_file (edit
, exp
) >= 0);
3041 edit_error_dialog (_("Insert file"), get_sys_error (_("Cannot insert file")));
3046 edit
->force
|= REDRAW_COMPLETELY
;
3050 /* --------------------------------------------------------------------------------------------- */
3051 /** sorts a block, returns -1 on system fail, 1 on cancel and 0 on success */
3054 edit_sort_cmd (WEdit
* edit
)
3056 static char *old
= 0;
3057 char *exp
, *tmp
, *tmp_edit_block_name
, *tmp_edit_temp_name
;
3058 long start_mark
, end_mark
;
3061 if (eval_marks (edit
, &start_mark
, &end_mark
))
3063 edit_error_dialog (_("Sort block"), _("You must first highlight a block of text"));
3067 tmp
= mc_config_get_full_path (EDIT_BLOCK_FILE
);
3068 edit_save_block (edit
, tmp
, start_mark
, end_mark
);
3071 exp
= input_dialog (_("Run sort"),
3072 _("Enter sort options (see manpage) separated by whitespace:"),
3073 MC_HISTORY_EDIT_SORT
, (old
!= NULL
) ? old
: "");
3079 tmp_edit_block_name
= mc_config_get_full_path (EDIT_BLOCK_FILE
);
3080 tmp_edit_temp_name
= mc_config_get_full_path (EDIT_TEMP_FILE
);
3082 g_strconcat (" sort ", exp
, " ", tmp_edit_block_name
,
3083 " > ", tmp_edit_temp_name
, (char *) NULL
);
3084 g_free (tmp_edit_temp_name
);
3085 g_free (tmp_edit_block_name
);
3091 if (e
== -1 || e
== 127)
3093 edit_error_dialog (_("Sort"), get_sys_error (_("Cannot execute sort command")));
3098 sprintf (q
, "%d ", e
);
3099 tmp
= g_strdup_printf (_("Sort returned non-zero: %s"), q
);
3100 edit_error_dialog (_("Sort"), tmp
);
3106 edit
->force
|= REDRAW_COMPLETELY
;
3108 if (edit_block_delete_cmd (edit
))
3110 tmp
= mc_config_get_full_path (EDIT_TEMP_FILE
);
3111 edit_insert_file (edit
, tmp
);
3116 /* --------------------------------------------------------------------------------------------- */
3118 * Ask user for a command, execute it and paste its output back to the
3123 edit_ext_cmd (WEdit
* edit
)
3125 char *exp
, *tmp
, *tmp_edit_temp_file
;
3129 input_dialog (_("Paste output of external command"),
3130 _("Enter shell command(s):"), MC_HISTORY_EDIT_PASTE_EXTCMD
, NULL
);
3135 tmp_edit_temp_file
= mc_config_get_full_path (EDIT_TEMP_FILE
);
3136 tmp
= g_strconcat (exp
, " > ", tmp_edit_temp_file
, (char *) NULL
);
3137 g_free (tmp_edit_temp_file
);
3144 edit_error_dialog (_("External command"), get_sys_error (_("Cannot execute command")));
3148 edit
->force
|= REDRAW_COMPLETELY
;
3149 tmp
= mc_config_get_full_path (EDIT_TEMP_FILE
);
3150 edit_insert_file (edit
, tmp
);
3155 /* --------------------------------------------------------------------------------------------- */
3156 /** if block is 1, a block must be highlighted and the shell command
3157 processes it. If block is 0 the shell command is a straight system
3158 command, that just produces some output which is to be inserted */
3161 edit_block_process_cmd (WEdit
* edit
, int macro_number
)
3164 char *macros_fname
= NULL
;
3166 fname
= g_strdup_printf ("%s.%i.sh", MC_EXTMACRO_FILE
, macro_number
);
3167 macros_fname
= g_build_filename (mc_config_get_data_path (), fname
, (char *) NULL
);
3168 user_menu (edit
, macros_fname
, 0);
3170 g_free (macros_fname
);
3171 edit
->force
|= REDRAW_COMPLETELY
;
3174 /* --------------------------------------------------------------------------------------------- */
3177 edit_mail_dialog (WEdit
* edit
)
3180 char *tmail_subject
;
3183 static char *mail_cc_last
= 0;
3184 static char *mail_subject_last
= 0;
3185 static char *mail_to_last
= 0;
3187 QuickWidget quick_widgets
[] = {
3188 /* 0 */ QUICK_BUTTON (6, 10, 9, MAIL_DLG_HEIGHT
, N_("&Cancel"), B_CANCEL
, NULL
),
3189 /* 1 */ QUICK_BUTTON (2, 10, 9, MAIL_DLG_HEIGHT
, N_("&OK"), B_ENTER
, NULL
),
3190 /* 2 */ QUICK_INPUT (3, 50, 8, MAIL_DLG_HEIGHT
, "", 44, 0, "mail-dlg-input", &tmail_cc
),
3191 /* 3 */ QUICK_LABEL (3, 50, 7, MAIL_DLG_HEIGHT
, N_("Copies to")),
3192 /* 4 */ QUICK_INPUT (3, 50, 6, MAIL_DLG_HEIGHT
, "", 44, 0, "mail-dlg-input-2",
3194 /* 5 */ QUICK_LABEL (3, 50, 5, MAIL_DLG_HEIGHT
, N_("Subject")),
3195 /* 6 */ QUICK_INPUT (3, 50, 4, MAIL_DLG_HEIGHT
, "", 44, 0, "mail-dlg-input-3", &tmail_to
),
3196 /* 7 */ QUICK_LABEL (3, 50, 3, MAIL_DLG_HEIGHT
, N_("To")),
3197 /* 8 */ QUICK_LABEL (3, 50, 2, MAIL_DLG_HEIGHT
, N_("mail -s <subject> -c <cc> <to>")),
3201 QuickDialog Quick_input
= {
3202 50, MAIL_DLG_HEIGHT
, -1, -1, N_("Mail"),
3203 "[Input Line Keys]", quick_widgets
, NULL
, FALSE
3206 quick_widgets
[2].u
.input
.text
= mail_cc_last
? mail_cc_last
: "";
3207 quick_widgets
[4].u
.input
.text
= mail_subject_last
? mail_subject_last
: "";
3208 quick_widgets
[6].u
.input
.text
= mail_to_last
? mail_to_last
: "";
3210 if (quick_dialog (&Quick_input
) != B_CANCEL
)
3212 g_free (mail_cc_last
);
3213 g_free (mail_subject_last
);
3214 g_free (mail_to_last
);
3215 mail_cc_last
= tmail_cc
;
3216 mail_subject_last
= tmail_subject
;
3217 mail_to_last
= tmail_to
;
3218 pipe_mail (edit
, mail_to_last
, mail_subject_last
, mail_cc_last
);
3223 /*******************/
3224 /* Word Completion */
3225 /*******************/
3227 /* --------------------------------------------------------------------------------------------- */
3229 * Complete current word using regular expression search
3230 * backwards beginning at the current cursor position.
3234 edit_complete_word_cmd (WEdit
* edit
)
3236 gsize i
, max_len
, word_len
= 0, num_compl
= 0;
3237 long word_start
= 0;
3238 unsigned char *bufpos
;
3240 struct selection
compl[MAX_WORD_COMPLETIONS
]; /* completions */
3242 /* search start of word to be completed */
3243 if (!edit_find_word_start (edit
, &word_start
, &word_len
))
3246 /* prepare match expression */
3247 bufpos
= &edit
->buffers1
[word_start
>> S_EDIT_BUF_SIZE
][word_start
& M_EDIT_BUF_SIZE
];
3249 /* match_expr = g_strdup_printf ("\\b%.*s[a-zA-Z_0-9]+", word_len, bufpos); */
3252 ("(^|\\s+|\\b)%.*s[^\\s\\.=\\+\\[\\]\\(\\)\\,\\;\\:\\\"\\'\\-\\?\\/\\|\\\\\\{\\}\\*\\&\\^\\%%\\$#@\\!]+",
3253 (int) word_len
, bufpos
);
3255 /* collect the possible completions */
3256 /* start search from begin to end of file */
3258 edit_collect_completions (edit
, word_start
, word_len
, match_expr
,
3259 (struct selection
*) &compl, &num_compl
);
3263 /* insert completed word if there is only one match */
3266 for (i
= word_len
; i
< compl[0].len
; i
++)
3267 edit_insert (edit
, *(compl[0].text
+ i
));
3269 /* more than one possible completion => ask the user */
3272 /* !!! usually only a beep is expected and when <ALT-TAB> is !!! */
3273 /* !!! pressed again the selection dialog pops up, but that !!! */
3274 /* !!! seems to require a further internal state !!! */
3277 /* let the user select the preferred completion */
3278 editcmd_dialog_completion_show (edit
, max_len
, word_len
,
3279 (struct selection
*) &compl, num_compl
);
3283 g_free (match_expr
);
3284 /* release memory before return */
3285 for (i
= 0; i
< num_compl
; i
++)
3286 g_free (compl[i
].text
);
3289 /* --------------------------------------------------------------------------------------------- */
3292 edit_select_codepage_cmd (WEdit
* edit
)
3295 if (do_select_codepage ())
3296 edit_set_codeset (edit
);
3298 edit
->force
= REDRAW_COMPLETELY
;
3299 edit_refresh_cmd (edit
);
3305 /* --------------------------------------------------------------------------------------------- */
3308 edit_insert_literal_cmd (WEdit
* edit
)
3310 int char_for_insertion
= editcmd_dialog_raw_key_query (_("Insert literal"),
3311 _("Press any key:"), 0);
3312 edit_execute_key_command (edit
, -1, ascii_alpha_to_cntrl (char_for_insertion
));
3315 /* --------------------------------------------------------------------------------------------- */
3318 edit_begin_end_macro_cmd (WEdit
* edit
)
3320 /* edit is a pointer to the widget */
3323 unsigned long command
= macro_index
< 0 ? CK_MacroStartRecord
: CK_MacroStopRecord
;
3324 edit_execute_key_command (edit
, command
, -1);
3328 /* --------------------------------------------------------------------------------------------- */
3331 edit_begin_end_repeat_cmd (WEdit
* edit
)
3333 /* edit is a pointer to the widget */
3336 unsigned long command
= macro_index
< 0 ? CK_RepeatStartRecord
: CK_RepeatStopRecord
;
3337 edit_execute_key_command (edit
, command
, -1);
3341 /* --------------------------------------------------------------------------------------------- */
3344 edit_load_forward_cmd (WEdit
* edit
)
3348 if (edit_query_dialog2
3350 _("Current text was modified without a file save\n"
3351 "Continue discards these changes"), _("C&ontinue"), _("&Cancel")))
3353 edit
->force
|= REDRAW_COMPLETELY
;
3357 if (edit_stack_iterator
+ 1 < MAX_HISTORY_MOVETO
)
3359 if (edit_history_moveto
[edit_stack_iterator
+ 1].line
< 1)
3363 edit_stack_iterator
++;
3364 if (edit_history_moveto
[edit_stack_iterator
].filename
)
3366 edit_reload_line (edit
, edit_history_moveto
[edit_stack_iterator
].filename
,
3367 edit_history_moveto
[edit_stack_iterator
].line
);
3381 /* --------------------------------------------------------------------------------------------- */
3384 edit_load_back_cmd (WEdit
* edit
)
3388 if (edit_query_dialog2
3390 _("Current text was modified without a file save\n"
3391 "Continue discards these changes"), _("C&ontinue"), _("&Cancel")))
3393 edit
->force
|= REDRAW_COMPLETELY
;
3397 if (edit_stack_iterator
> 0)
3399 edit_stack_iterator
--;
3400 if (edit_history_moveto
[edit_stack_iterator
].filename
)
3402 edit_reload_line (edit
, edit_history_moveto
[edit_stack_iterator
].filename
,
3403 edit_history_moveto
[edit_stack_iterator
].line
);
3417 /* --------------------------------------------------------------------------------------------- */
3420 edit_get_match_keyword_cmd (WEdit
* edit
)
3422 gsize word_len
= 0, max_len
= 0;
3425 long word_start
= 0;
3426 unsigned char *bufpos
;
3430 char *tagfile
= NULL
;
3432 etags_hash_t def_hash
[MAX_DEFINITIONS
];
3434 for (i
= 0; i
< MAX_DEFINITIONS
; i
++)
3436 def_hash
[i
].filename
= NULL
;
3439 /* search start of word to be completed */
3440 if (!edit_find_word_start (edit
, &word_start
, &word_len
))
3443 /* prepare match expression */
3444 bufpos
= &edit
->buffers1
[word_start
>> S_EDIT_BUF_SIZE
][word_start
& M_EDIT_BUF_SIZE
];
3445 match_expr
= g_strdup_printf ("%.*s", (int) word_len
, bufpos
);
3447 ptr
= g_get_current_dir ();
3448 path
= g_strconcat (ptr
, G_DIR_SEPARATOR_S
, (char *) NULL
);
3451 /* Recursive search file 'TAGS' in parent dirs */
3454 ptr
= g_path_get_dirname (path
);
3458 tagfile
= mc_build_filename (path
, TAGS_NAME
, (char *) NULL
);
3459 if (exist_file (tagfile
))
3462 while (strcmp (path
, G_DIR_SEPARATOR_S
) != 0);
3467 etags_set_definition_hash (tagfile
, path
, match_expr
, (etags_hash_t
*) & def_hash
);
3472 max_len
= MAX_WIDTH_DEF_DIALOG
;
3476 editcmd_dialog_select_definition_show (edit
, match_expr
, max_len
, word_len
,
3477 (etags_hash_t
*) & def_hash
, num_def
);
3479 g_free (match_expr
);
3482 /* --------------------------------------------------------------------------------------------- */