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
)
127 gchar
*real_filename
;
128 int this_save_mode
, fd
= -1;
135 if (*filename
!= PATH_SEP
&& edit
->dir
)
137 real_filename
= concat_dir_and_file (edit
->dir
, filename
);
141 real_filename
= g_strdup (filename
);
144 this_save_mode
= option_save_mode
;
145 if (this_save_mode
!= EDIT_QUICK_SAVE
)
147 vfs_path_t
*vpath
= vfs_path_from_str (real_filename
);
148 if (!vfs_file_is_local (vpath
) || (fd
= mc_open (real_filename
, O_RDONLY
| O_BINARY
)) == -1)
151 * The file does not exists yet, so no safe save or
152 * backup are necessary.
154 this_save_mode
= EDIT_QUICK_SAVE
;
156 vfs_path_free (vpath
);
161 if (this_save_mode
== EDIT_QUICK_SAVE
&& !edit
->skip_detach_prompt
)
166 rv
= mc_stat (real_filename
, &sb
);
167 if (rv
== 0 && sb
.st_nlink
> 1)
169 rv
= edit_query_dialog3 (_("Warning"),
170 _("File has hard-links. Detach before saving?"),
171 _("&Yes"), _("&No"), _("&Cancel"));
175 this_save_mode
= EDIT_SAFE_SAVE
;
178 edit
->skip_detach_prompt
= 1;
181 g_free (real_filename
);
186 /* Prevent overwriting changes from other editor sessions. */
187 if (rv
== 0 && edit
->stat1
.st_mtime
!= 0 && edit
->stat1
.st_mtime
!= sb
.st_mtime
)
190 /* The default action is "Cancel". */
193 rv
= edit_query_dialog2 (_("Warning"),
194 _("The file has been modified in the meantime. Save anyway?"),
195 _("&Yes"), _("&Cancel"));
198 g_free (real_filename
);
204 if (this_save_mode
!= EDIT_QUICK_SAVE
)
206 char *savedir
, *saveprefix
;
207 const char *slashpos
;
208 slashpos
= strrchr (real_filename
, PATH_SEP
);
211 savedir
= g_strdup (real_filename
);
212 savedir
[slashpos
- real_filename
+ 1] = '\0';
215 savedir
= g_strdup (".");
216 saveprefix
= concat_dir_and_file (savedir
, "cooledit");
218 fd
= mc_mkstemps (&savename
, saveprefix
, NULL
);
222 g_free (real_filename
);
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
= g_strdup (real_filename
);
236 ret
= mc_chown (savename
, edit
->stat1
.st_uid
, edit
->stat1
.st_gid
);
237 ret
= mc_chmod (savename
, edit
->stat1
.st_mode
);
240 fd
= mc_open (savename
, O_CREAT
| O_WRONLY
| O_TRUNC
| O_BINARY
, edit
->stat1
.st_mode
);
245 p
= edit_get_write_filter (savename
, real_filename
);
251 file
= (FILE *) popen (p
, "w");
255 filelen
= edit_write_stream (edit
, file
);
259 if (pclose (file
) != 0)
261 tmp
= g_strdup_printf (_("Error writing to pipe: %s"), p
);
262 edit_error_dialog (_("Error"), tmp
);
271 tmp
= g_strdup_printf (_("Cannot open pipe for writing: %s"), p
);
272 edit_error_dialog (_("Error"), get_sys_error (tmp
));
279 else if (edit
->lb
== LB_ASIS
)
280 { /* do not change line breaks */
283 filelen
= edit
->last_byte
;
284 while (buf
<= (edit
->curs1
>> S_EDIT_BUF_SIZE
) - 1)
286 if (mc_write (fd
, (char *) edit
->buffers1
[buf
], EDIT_BUF_SIZE
) != EDIT_BUF_SIZE
)
294 (fd
, (char *) edit
->buffers1
[buf
],
295 edit
->curs1
& M_EDIT_BUF_SIZE
) != (edit
->curs1
& M_EDIT_BUF_SIZE
))
299 else if (edit
->curs2
)
302 buf
= (edit
->curs2
>> S_EDIT_BUF_SIZE
);
305 (char *) edit
->buffers2
[buf
] + EDIT_BUF_SIZE
-
306 (edit
->curs2
& M_EDIT_BUF_SIZE
) - 1,
307 1 + (edit
->curs2
& M_EDIT_BUF_SIZE
)) != 1 + (edit
->curs2
& M_EDIT_BUF_SIZE
))
315 if (mc_write (fd
, (char *) edit
->buffers2
[buf
], EDIT_BUF_SIZE
) != EDIT_BUF_SIZE
)
327 /* Update the file information, especially the mtime. */
328 if (mc_stat (savename
, &edit
->stat1
) == -1)
332 { /* change line breaks */
337 file
= (FILE *) fopen (savename
, "w");
341 filelen
= edit_write_stream (edit
, file
);
348 msg
= g_strdup_printf (_("Cannot open file for writing: %s"), savename
);
349 edit_error_dialog (_("Error"), msg
);
355 if (filelen
!= edit
->last_byte
)
358 if (this_save_mode
== EDIT_DO_BACKUP
)
361 assert (option_backup_ext
!= NULL
);
363 tmp
= g_strconcat (real_filename
, option_backup_ext
, (char *) NULL
);
364 if (mc_rename (real_filename
, tmp
) == -1)
371 if (this_save_mode
!= EDIT_QUICK_SAVE
)
372 if (mc_rename (savename
, real_filename
) == -1)
375 g_free (real_filename
);
378 /* FIXME: Is this safe ?
379 * if (this_save_mode != EDIT_QUICK_SAVE)
380 * mc_unlink (savename);
382 g_free (real_filename
);
387 /* --------------------------------------------------------------------------------------------- */
390 edit_check_newline (WEdit
* edit
)
392 return !(option_check_nl_at_eof
&& edit
->last_byte
> 0
393 && edit_get_byte (edit
, edit
->last_byte
- 1) != '\n'
394 && edit_query_dialog2 (_("Warning"),
395 _("The file you are saving is not finished with a newline"),
396 _("C&ontinue"), _("&Cancel")));
399 /* --------------------------------------------------------------------------------------------- */
402 edit_get_save_file_as (WEdit
* edit
)
405 #define DLG_HEIGHT 14
407 static LineBreaks cur_lb
= LB_ASIS
;
409 char *filename
= edit
->filename
;
411 const char *lb_names
[LB_NAMES
] = {
412 N_("&Do not change"),
413 N_("&Unix format (LF)"),
414 N_("&Windows/DOS format (CR LF)"),
415 N_("&Macintosh format (CR)")
418 QuickWidget quick_widgets
[] = {
419 QUICK_BUTTON (6, 10, DLG_HEIGHT
- 3, DLG_HEIGHT
, N_("&Cancel"), B_CANCEL
, NULL
),
420 QUICK_BUTTON (2, 10, DLG_HEIGHT
- 3, DLG_HEIGHT
, N_("&OK"), B_ENTER
, NULL
),
421 QUICK_RADIO (5, DLG_WIDTH
, DLG_HEIGHT
- 8, DLG_HEIGHT
, LB_NAMES
, lb_names
, (int *) &cur_lb
),
422 QUICK_LABEL (3, DLG_WIDTH
, DLG_HEIGHT
- 9, DLG_HEIGHT
, N_("Change line breaks to:")),
423 QUICK_INPUT (3, DLG_WIDTH
, DLG_HEIGHT
- 11, DLG_HEIGHT
, filename
, DLG_WIDTH
- 6, 0,
424 "save-as", &filename
),
425 QUICK_LABEL (3, DLG_WIDTH
, DLG_HEIGHT
- 12, DLG_HEIGHT
, N_("Enter file name:")),
429 QuickDialog Quick_options
= {
430 DLG_WIDTH
, DLG_HEIGHT
, -1, -1,
431 N_("Save As"), "[Save File As]",
432 quick_widgets
, NULL
, FALSE
435 if (quick_dialog (&Quick_options
) != B_CANCEL
)
440 fname
= tilde_expand (filename
);
451 /** returns 1 on success */
454 edit_save_cmd (WEdit
* edit
)
456 int res
, save_lock
= 0;
458 if (!edit
->locked
&& !edit
->delete_file
)
459 save_lock
= edit_lock_file (edit
);
460 res
= edit_save_file (edit
, edit
->filename
);
462 /* Maintain modify (not save) lock on failure */
463 if ((res
> 0 && edit
->locked
) || save_lock
)
464 edit
->locked
= edit_unlock_file (edit
);
466 /* On failure try 'save as', it does locking on its own */
468 return edit_save_as_cmd (edit
);
469 edit
->force
|= REDRAW_COMPLETELY
;
472 edit
->delete_file
= 0;
479 /* --------------------------------------------------------------------------------------------- */
480 /** returns 1 on error */
483 edit_load_file_from_filename (WEdit
* edit
, char *exp
)
485 int prev_locked
= edit
->locked
;
486 char *prev_filename
= g_strdup (edit
->filename
);
488 if (!edit_reload (edit
, exp
))
490 g_free (prev_filename
);
498 fullpath
= mc_build_filename (edit
->dir
, prev_filename
, (char *) NULL
);
499 unlock_file (fullpath
);
502 g_free (prev_filename
);
506 /* --------------------------------------------------------------------------------------------- */
509 edit_load_syntax_file (WEdit
* edit
)
516 dir
= query_dialog (_("Syntax file edit"),
517 _("Which syntax file you want to edit?"), D_NORMAL
, 2,
518 _("&User"), _("&System Wide"));
521 extdir
= g_build_filename (mc_global
.sysconfig_dir
, "syntax", "Syntax", (char *) NULL
);
522 if (!exist_file (extdir
))
525 extdir
= g_build_filename (mc_global
.share_data_dir
, "syntax", "Syntax", (char *) NULL
);
532 buffer
= mc_config_get_full_path (EDIT_SYNTAX_FILE
);
533 check_for_default (extdir
, buffer
);
534 edit_load_file_from_filename (edit
, buffer
);
538 edit_load_file_from_filename (edit
, extdir
);
543 /* --------------------------------------------------------------------------------------------- */
546 edit_load_menu_file (WEdit
* edit
)
552 dir
= query_dialog (_("Menu edit"),
553 _("Which menu file do you want to edit?"), D_NORMAL
,
554 geteuid () != 0 ? 2 : 3, _("&Local"), _("&User"), _("&System Wide"));
556 menufile
= concat_dir_and_file (mc_global
.sysconfig_dir
, EDIT_GLOBAL_MENU
);
558 if (!exist_file (menufile
))
561 menufile
= concat_dir_and_file (mc_global
.share_data_dir
, EDIT_GLOBAL_MENU
);
567 buffer
= g_strdup (EDIT_LOCAL_MENU
);
568 check_for_default (menufile
, buffer
);
569 chmod (buffer
, 0600);
573 buffer
= mc_config_get_full_path (EDIT_HOME_MENU
);
574 check_for_default (menufile
, buffer
);
578 buffer
= concat_dir_and_file (mc_global
.sysconfig_dir
, EDIT_GLOBAL_MENU
);
579 if (!exist_file (buffer
))
582 buffer
= concat_dir_and_file (mc_global
.share_data_dir
, EDIT_GLOBAL_MENU
);
591 edit_load_file_from_filename (edit
, buffer
);
597 /* --------------------------------------------------------------------------------------------- */
600 edit_delete_column_of_text (WEdit
* edit
)
602 long p
, q
, r
, m1
, m2
;
605 eval_marks (edit
, &m1
, &m2
);
606 n
= edit_move_forward (edit
, m1
, 0, m2
) + 1;
607 c
= edit_move_forward3 (edit
, edit_bol (edit
, m1
), 0, m1
);
608 d
= edit_move_forward3 (edit
, edit_bol (edit
, m2
), 0, m2
);
609 b
= max (min (c
, d
), min (edit
->column1
, edit
->column2
));
610 c
= max (c
, max (edit
->column1
, edit
->column2
));
614 r
= edit_bol (edit
, edit
->curs1
);
615 p
= edit_move_forward3 (edit
, r
, b
, 0);
616 q
= edit_move_forward3 (edit
, r
, c
, 0);
621 edit_cursor_move (edit
, p
- edit
->curs1
);
624 /* delete line between margins */
625 if (edit_get_byte (edit
, edit
->curs1
) != '\n')
626 edit_delete (edit
, 1);
630 /* move to next line except on the last delete */
631 edit_cursor_move (edit
, edit_move_forward (edit
, edit
->curs1
, 1, 0) - edit
->curs1
);
635 /* --------------------------------------------------------------------------------------------- */
636 /** if success return 0 */
639 edit_block_delete (WEdit
* edit
)
642 long start_mark
, end_mark
;
643 int curs_pos
, line_width
;
644 long curs_line
, c1
, c2
;
646 if (eval_marks (edit
, &start_mark
, &end_mark
))
648 if (edit
->column_highlight
&& edit
->mark2
< 0)
649 edit_mark_cmd (edit
, 0);
650 if ((end_mark
- start_mark
) > option_max_undo
/ 2)
652 /* Warning message with a query to continue or cancel the operation */
653 if (edit_query_dialog2
656 ("Block is large, you may not be able to undo this action"),
657 _("C&ontinue"), _("&Cancel")))
662 c1
= min (edit
->column1
, edit
->column2
);
663 c2
= max (edit
->column1
, edit
->column2
);
667 edit_push_markers (edit
);
669 curs_line
= edit
->curs_line
;
671 /* calculate line width and cursor position before cut */
672 line_width
= edit_move_forward3 (edit
, edit_bol (edit
, edit
->curs1
), 0,
673 edit_eol (edit
, edit
->curs1
));
674 curs_pos
= edit
->curs_col
+ edit
->over_col
;
676 /* move cursor to start of selection */
677 edit_cursor_move (edit
, start_mark
- edit
->curs1
);
678 edit_scroll_screen_over_cursor (edit
);
680 if (start_mark
< end_mark
)
682 if (edit
->column_highlight
)
685 edit_mark_cmd (edit
, 0);
686 edit_delete_column_of_text (edit
);
687 /* move cursor to the saved position */
688 edit_move_to_line (edit
, curs_line
);
689 /* calculate line width after cut */
690 line_width
= edit_move_forward3 (edit
, edit_bol (edit
, edit
->curs1
), 0,
691 edit_eol (edit
, edit
->curs1
));
692 if (option_cursor_beyond_eol
&& curs_pos
> line_width
)
693 edit
->over_col
= curs_pos
- line_width
;
697 while (count
< end_mark
)
699 edit_delete (edit
, 1);
704 edit_set_markers (edit
, 0, 0, 0, 0);
705 edit
->force
|= REDRAW_PAGE
;
709 /* --------------------------------------------------------------------------------------------- */
711 * Get EOL symbol for searching.
713 * @param edit editor object
718 edit_search_get_current_end_line_char (const WEdit
* edit
)
729 /* --------------------------------------------------------------------------------------------- */
731 * Checking if search condition have BOL(^) or EOL ($) regexp special characters.
733 * @param search search object
734 * @return result of checks.
737 static edit_search_line_t
738 edit_get_search_line_type (mc_search_t
* search
)
740 edit_search_line_t search_line_type
= 0;
742 if (search
->search_type
!= MC_SEARCH_T_REGEX
)
743 return search_line_type
;
745 if (*search
->original
== '^')
746 search_line_type
|= AT_START_LINE
;
748 if (search
->original
[search
->original_len
- 1] == '$')
749 search_line_type
|= AT_END_LINE
;
750 return search_line_type
;
753 /* --------------------------------------------------------------------------------------------- */
755 * Calculating the start position of next line.
757 * @param edit editor object
758 * @param current_pos current position
759 * @param max_pos max position
760 * @param end_string_symbol end of line symbol
761 * @return start position of next line
765 edit_calculate_start_of_next_line (WEdit
* edit
, off_t current_pos
, off_t max_pos
,
766 char end_string_symbol
)
770 for (i
= current_pos
; i
< max_pos
; i
++)
773 if (edit_get_byte (edit
, i
) == end_string_symbol
)
780 /* --------------------------------------------------------------------------------------------- */
782 * Calculating the end position of previous line.
784 * @param edit editor object
785 * @param current_pos current position
786 * @param end_string_symbol end of line symbol
787 * @return end position of previous line
791 edit_calculate_end_of_previous_line (WEdit
* edit
, off_t current_pos
, char end_string_symbol
)
795 for (i
= current_pos
- 1; i
>= 0; i
--)
796 if (edit_get_byte (edit
, i
) == end_string_symbol
)
802 /* --------------------------------------------------------------------------------------------- */
804 * Calculating the start position of previous line.
806 * @param edit editor object
807 * @param current_pos current position
808 * @param end_string_symbol end of line symbol
809 * @return start position of previous line
813 edit_calculate_start_of_previous_line (WEdit
* edit
, off_t current_pos
, char end_string_symbol
)
815 current_pos
= edit_calculate_end_of_previous_line (edit
, current_pos
, end_string_symbol
);
816 current_pos
= edit_calculate_end_of_previous_line (edit
, current_pos
, end_string_symbol
);
818 return (current_pos
+ 1);
821 /* --------------------------------------------------------------------------------------------- */
823 * Calculating the start position of current line.
825 * @param edit editor object
826 * @param current_pos current position
827 * @param end_string_symbol end of line symbol
828 * @return start position of current line
832 edit_calculate_start_of_current_line (WEdit
* edit
, off_t current_pos
, char end_string_symbol
)
834 current_pos
= edit_calculate_end_of_previous_line (edit
, current_pos
, end_string_symbol
);
836 return (current_pos
+ 1);
839 /* --------------------------------------------------------------------------------------------- */
841 * Fixing (if needed) search start position if 'only in selection' option present.
843 * @param edit editor object
847 edit_search_fix_search_start_if_selection (WEdit
* edit
)
852 if (!edit_search_options
.only_in_selection
)
855 if (eval_marks (edit
, &start_mark
, &end_mark
) != 0)
858 if (edit_search_options
.backwards
)
860 if (edit
->search_start
> end_mark
|| edit
->search_start
<= start_mark
)
861 edit
->search_start
= end_mark
;
865 if (edit
->search_start
< start_mark
|| edit
->search_start
>= end_mark
)
866 edit
->search_start
= start_mark
;
870 /* --------------------------------------------------------------------------------------------- */
873 editcmd_find (WEdit
* edit
, gsize
* len
)
875 off_t search_start
= edit
->search_start
;
878 long end_mark
= edit
->last_byte
;
880 char end_string_symbol
;
882 end_string_symbol
= edit_search_get_current_end_line_char (edit
);
884 /* prepare for search */
885 if (edit_search_options
.only_in_selection
)
887 mark_res
= eval_marks (edit
, &start_mark
, &end_mark
);
890 edit
->search
->error
= MC_SEARCH_E_NOTFOUND
;
891 edit
->search
->error_str
= g_strdup (_("Search string not found"));
895 /* fix the start and the end of search block positions */
896 if ((edit
->search_line_type
& AT_START_LINE
) != 0
897 && (start_mark
!= 0 || edit_get_byte (edit
, start_mark
- 1) != end_string_symbol
))
900 edit_calculate_start_of_next_line (edit
, start_mark
, edit
->last_byte
,
903 if ((edit
->search_line_type
& AT_END_LINE
) != 0
904 && (end_mark
- 1 != edit
->last_byte
905 || edit_get_byte (edit
, end_mark
) != end_string_symbol
))
907 end_mark
= edit_calculate_end_of_previous_line (edit
, end_mark
, end_string_symbol
);
909 if (start_mark
>= end_mark
)
911 edit
->search
->error
= MC_SEARCH_E_NOTFOUND
;
912 edit
->search
->error_str
= g_strdup (_("Search string not found"));
918 if (edit_search_options
.backwards
)
919 end_mark
= max (1, edit
->curs1
) - 1;
923 if (edit_search_options
.backwards
)
925 /* backward search */
926 search_end
= end_mark
;
928 if ((edit
->search_line_type
& AT_START_LINE
) != 0)
930 edit_calculate_start_of_current_line (edit
, search_start
, end_string_symbol
);
932 while ((int) search_start
>= start_mark
)
934 if (search_end
> (off_t
) (search_start
+ edit
->search
->original_len
)
935 && mc_search_is_fixed_search_str (edit
->search
))
937 search_end
= search_start
+ edit
->search
->original_len
;
939 if (mc_search_run (edit
->search
, (void *) edit
, search_start
, search_end
, len
)
940 && edit
->search
->normal_offset
== search_start
)
946 if ((edit
->search_line_type
& AT_START_LINE
) != 0)
948 edit_calculate_start_of_previous_line (edit
, search_start
, end_string_symbol
);
952 edit
->search
->error_str
= g_strdup (_("Search string not found"));
957 if ((edit
->search_line_type
& AT_START_LINE
) != 0 && search_start
!= start_mark
)
959 edit_calculate_start_of_next_line (edit
, search_start
, end_mark
, end_string_symbol
);
960 return mc_search_run (edit
->search
, (void *) edit
, search_start
, end_mark
, len
);
965 /* --------------------------------------------------------------------------------------------- */
968 edit_replace_cmd__conv_to_display (char *str
)
973 tmp
= str_convert_to_display (str
);
977 return g_string_free (tmp
, FALSE
);
978 g_string_free (tmp
, TRUE
);
981 return g_strdup (str
);
984 /* --------------------------------------------------------------------------------------------- */
987 edit_replace_cmd__conv_to_input (char *str
)
992 tmp
= str_convert_to_input (str
);
996 return g_string_free (tmp
, FALSE
);
997 g_string_free (tmp
, TRUE
);
1000 return g_strdup (str
);
1003 /* --------------------------------------------------------------------------------------------- */
1006 edit_do_search (WEdit
* edit
)
1010 if (edit
->search
== NULL
)
1011 edit
->search_start
= edit
->curs1
;
1013 edit_push_undo_action (edit
, KEY_PRESS
+ edit
->start_display
);
1015 if (search_create_bookmark
)
1017 int found
= 0, books
= 0;
1018 long l
= 0, l_last
= -1;
1021 search_create_bookmark
= FALSE
;
1022 book_mark_flush (edit
, -1);
1026 if (!mc_search_run (edit
->search
, (void *) edit
, q
, edit
->last_byte
, &len
))
1029 edit
->search_start
= edit
->search
->normal_offset
;
1031 l
+= edit_count_lines (edit
, q
, edit
->search
->normal_offset
);
1034 book_mark_insert (edit
, l
, BOOK_MARK_FOUND_COLOR
);
1038 q
= edit
->search
->normal_offset
+ 1;
1042 edit_error_dialog (_("Search"), _("Search string not found"));
1044 edit_cursor_move (edit
, edit
->search_start
- edit
->curs1
);
1048 if (edit
->found_len
!= 0 && edit
->search_start
== edit
->found_start
+ 1
1049 && edit_search_options
.backwards
)
1050 edit
->search_start
--;
1052 if (edit
->found_len
!= 0 && edit
->search_start
== edit
->found_start
- 1
1053 && !edit_search_options
.backwards
)
1054 edit
->search_start
++;
1056 if (editcmd_find (edit
, &len
))
1058 edit
->found_start
= edit
->search_start
= edit
->search
->normal_offset
;
1059 edit
->found_len
= len
;
1061 edit_cursor_move (edit
, edit
->search_start
- edit
->curs1
);
1062 edit_scroll_screen_over_cursor (edit
);
1063 if (edit_search_options
.backwards
)
1064 edit
->search_start
--;
1066 edit
->search_start
++;
1070 edit
->search_start
= edit
->curs1
;
1071 if (edit
->search
->error_str
!= NULL
)
1072 edit_error_dialog (_("Search"), edit
->search
->error_str
);
1076 edit
->force
|= REDRAW_COMPLETELY
;
1077 edit_scroll_screen_over_cursor (edit
);
1080 /* --------------------------------------------------------------------------------------------- */
1083 edit_search (WEdit
* edit
)
1085 if (editcmd_dialog_search_show (edit
))
1087 edit
->search_line_type
= edit_get_search_line_type (edit
->search
);
1088 edit_search_fix_search_start_if_selection (edit
);
1089 edit_do_search (edit
);
1093 /* --------------------------------------------------------------------------------------------- */
1094 /** Return a null terminated length of text. Result must be g_free'd */
1096 static unsigned char *
1097 edit_get_block (WEdit
* edit
, long start
, long finish
, int *l
)
1099 unsigned char *s
, *r
;
1100 r
= s
= g_malloc0 (finish
- start
+ 1);
1101 if (edit
->column_highlight
)
1104 /* copy from buffer, excluding chars that are out of the column 'margins' */
1105 while (start
< finish
)
1109 x
= edit_move_forward3 (edit
, edit_bol (edit
, start
), 0, start
);
1110 c
= edit_get_byte (edit
, start
);
1111 if ((x
>= edit
->column1
&& x
< edit
->column2
)
1112 || (x
>= edit
->column2
&& x
< edit
->column1
) || c
== '\n')
1122 *l
= finish
- start
;
1123 while (start
< finish
)
1124 *s
++ = edit_get_byte (edit
, start
++);
1130 /* --------------------------------------------------------------------------------------------- */
1131 /** copies a block to clipboard file */
1134 edit_save_block_to_clip_file (WEdit
* edit
, long start
, long finish
)
1138 tmp
= mc_config_get_full_path (EDIT_CLIP_FILE
);
1139 ret
= edit_save_block (edit
, tmp
, start
, finish
);
1144 /* --------------------------------------------------------------------------------------------- */
1147 pipe_mail (WEdit
* edit
, char *to
, char *subject
, char *cc
)
1152 to
= name_quote (to
, 0);
1153 subject
= name_quote (subject
, 0);
1154 cc
= name_quote (cc
, 0);
1155 s
= g_strconcat ("mail -s ", subject
, *cc
? " -c " : "", cc
, " ", to
, (char *) NULL
);
1169 for (i
= 0; i
< edit
->last_byte
; i
++)
1170 fputc (edit_get_byte (edit
, i
), p
);
1175 /* --------------------------------------------------------------------------------------------- */
1178 is_break_char (char c
)
1180 return (isspace (c
) || strchr ("{}[]()<>=|/\\!?~-+`'\",.;:#$%^&*", c
));
1183 /* --------------------------------------------------------------------------------------------- */
1184 /** find first character of current word */
1187 edit_find_word_start (WEdit
* edit
, long *word_start
, gsize
* word_len
)
1192 /* return if at begin of file */
1193 if (edit
->curs1
<= 0)
1196 c
= (unsigned char) edit_get_byte (edit
, edit
->curs1
- 1);
1197 /* return if not at end or in word */
1198 if (is_break_char (c
))
1201 /* search start of word to be completed */
1204 /* return if at begin of file */
1205 if ((gsize
) edit
->curs1
< i
)
1209 c
= (unsigned char) edit_get_byte (edit
, edit
->curs1
- i
);
1211 if (is_break_char (c
))
1213 /* return if word starts with digit */
1217 *word_start
= edit
->curs1
- (i
- 1); /* start found */
1226 /* --------------------------------------------------------------------------------------------- */
1228 * Get current word under cursor
1230 * @param edit editor object
1231 * @param srch mc_search object
1232 * @param word_start start word position
1234 * @return newly allocated string or NULL if no any words under cursor
1238 edit_collect_completions_get_current_word (WEdit
* edit
, mc_search_t
* srch
, long word_start
)
1243 if (!mc_search_run (srch
, (void *) edit
, word_start
, edit
->last_byte
, &len
))
1246 temp
= g_string_sized_new (len
);
1248 for (i
= 0; i
< len
; i
++)
1252 chr
= edit_get_byte (edit
, word_start
+ i
);
1254 g_string_append_c (temp
, chr
);
1257 return g_string_free (temp
, temp
->len
== 0);
1260 /* --------------------------------------------------------------------------------------------- */
1261 /** collect the possible completions */
1264 edit_collect_completions (WEdit
* edit
, long word_start
, gsize word_len
,
1265 char *match_expr
, struct selection
*compl, gsize
* num
)
1273 long last_byte
, start
= -1;
1276 srch
= mc_search_new (match_expr
, -1);
1280 if (mc_config_get_bool
1281 (mc_main_config
, CONFIG_APP_SECTION
, "editor_wordcompletion_collect_entire_file", 0))
1283 last_byte
= edit
->last_byte
;
1287 last_byte
= word_start
;
1290 srch
->search_type
= MC_SEARCH_T_REGEX
;
1291 srch
->is_case_sensitive
= TRUE
;
1292 srch
->search_fn
= edit_search_cmd_callback
;
1294 current_word
= edit_collect_completions_get_current_word (edit
, srch
, word_start
);
1296 temp
= g_string_new ("");
1298 /* collect max MAX_WORD_COMPLETIONS completions */
1299 while (mc_search_run (srch
, (void *) edit
, start
+ 1, last_byte
, &len
))
1301 g_string_set_size (temp
, 0);
1302 start
= srch
->normal_offset
;
1304 /* add matched completion if not yet added */
1305 for (i
= 0; i
< len
; i
++)
1307 skip
= edit_get_byte (edit
, start
+ i
);
1311 /* skip current word */
1312 if (start
+ (long) i
== word_start
)
1315 g_string_append_c (temp
, skip
);
1321 if (current_word
!= NULL
&& strcmp (current_word
, temp
->str
) == 0)
1326 for (i
= 0; i
< *num
; i
++)
1329 ((char *) &compl[i
].text
[word_len
],
1330 (char *) &temp
->str
[word_len
], max (len
, compl[i
].len
) - word_len
) == 0)
1332 struct selection
this = compl[i
];
1333 for (++i
; i
< *num
; i
++)
1335 compl[i
- 1] = compl[i
];
1337 compl[*num
- 1] = this;
1339 break; /* skip it, already added */
1345 if (*num
== MAX_WORD_COMPLETIONS
)
1347 g_free (compl[0].text
);
1348 for (i
= 1; i
< *num
; i
++)
1350 compl[i
- 1] = compl[i
];
1357 recoded
= str_convert_to_display (temp
->str
);
1359 if (recoded
&& recoded
->len
)
1360 g_string_assign (temp
, recoded
->str
);
1362 g_string_free (recoded
, TRUE
);
1365 compl[*num
].text
= g_strdup (temp
->str
);
1366 compl[*num
].len
= temp
->len
;
1370 /* note the maximal length needed for the completion dialog */
1375 mc_search_free (srch
);
1376 g_string_free (temp
, TRUE
);
1377 g_free (current_word
);
1382 /* --------------------------------------------------------------------------------------------- */
1385 edit_insert_column_of_text (WEdit
* edit
, unsigned char *data
, int size
, int width
,
1386 long *start_pos
, long *end_pos
, int *col1
, int *col2
)
1391 cursor
= edit
->curs1
;
1392 col
= edit_get_col (edit
);
1394 for (i
= 0; i
< size
; i
++)
1396 if (data
[i
] != '\n')
1397 edit_insert (edit
, data
[i
]);
1399 { /* fill in and move to next line */
1403 if (edit_get_byte (edit
, edit
->curs1
) != '\n')
1405 l
= width
- (edit_get_col (edit
) - col
);
1408 edit_insert (edit
, ' ');
1412 for (p
= edit
->curs1
;; p
++)
1414 if (p
== edit
->last_byte
)
1416 edit_cursor_move (edit
, edit
->last_byte
- edit
->curs1
);
1417 edit_insert_ahead (edit
, '\n');
1421 if (edit_get_byte (edit
, p
) == '\n')
1427 edit_cursor_move (edit
, edit_move_forward3 (edit
, p
, col
, 0) - edit
->curs1
);
1428 l
= col
- edit_get_col (edit
);
1429 while (l
>= space_width
)
1431 edit_insert (edit
, ' ');
1438 *col2
= col
+ width
;
1439 *start_pos
= cursor
;
1440 *end_pos
= edit
->curs1
;
1441 edit_cursor_move (edit
, cursor
- edit
->curs1
);
1444 /* --------------------------------------------------------------------------------------------- */
1447 edit_macro_comparator (gconstpointer
* macro1
, gconstpointer
* macro2
)
1449 const macros_t
*m1
= (const macros_t
*) macro1
;
1450 const macros_t
*m2
= (const macros_t
*) macro2
;
1452 return m1
->hotkey
- m2
->hotkey
;
1455 /* --------------------------------------------------------------------------------------------- */
1458 edit_macro_sort_by_hotkey (void)
1460 if (macros_list
!= NULL
&& macros_list
->len
!= 0)
1461 g_array_sort (macros_list
, (GCompareFunc
) edit_macro_comparator
);
1464 /* --------------------------------------------------------------------------------------------- */
1467 edit_get_macro (WEdit
* edit
, int hotkey
, const macros_t
** macros
, guint
* indx
)
1469 const macros_t
*array_start
= &g_array_index (macros_list
, struct macros_t
, 0);
1471 macros_t search_macro
;
1475 search_macro
.hotkey
= hotkey
;
1476 result
= bsearch (&search_macro
, macros_list
->data
, macros_list
->len
,
1477 sizeof (macros_t
), (GCompareFunc
) edit_macro_comparator
);
1479 if (result
!= NULL
&& result
->macro
!= NULL
)
1481 *indx
= (result
- array_start
);
1489 /* --------------------------------------------------------------------------------------------- */
1490 /** returns FALSE on error */
1493 edit_delete_macro (WEdit
* edit
, int hotkey
)
1495 mc_config_t
*macros_config
= NULL
;
1496 const char *section_name
= "editor";
1497 gchar
*macros_fname
;
1500 const macros_t
*macros
= NULL
;
1502 /* clear array of actions for current hotkey */
1503 while (edit_get_macro (edit
, hotkey
, ¯os
, &indx
))
1505 if (macros
->macro
!= NULL
)
1506 g_array_free (macros
->macro
, TRUE
);
1508 g_array_remove_index (macros_list
, indx
);
1509 edit_macro_sort_by_hotkey ();
1512 macros_fname
= mc_config_get_full_path (MC_MACRO_FILE
);
1513 macros_config
= mc_config_init (macros_fname
);
1514 g_free (macros_fname
);
1516 if (macros_config
== NULL
)
1519 skeyname
= lookup_key_by_code (hotkey
);
1520 while (mc_config_del_key (macros_config
, section_name
, skeyname
))
1523 mc_config_save_file (macros_config
, NULL
);
1524 mc_config_deinit (macros_config
);
1529 /* --------------------------------------------------------------------------------------------- */
1530 /*** public functions ****************************************************************************/
1531 /* --------------------------------------------------------------------------------------------- */
1534 edit_help_cmd (WEdit
* edit
)
1536 ev_help_t event_data
= { NULL
, "[Internal File Editor]" };
1537 mc_event_raise (MCEVENT_GROUP_CORE
, "help", &event_data
);
1539 edit
->force
|= REDRAW_COMPLETELY
;
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 menu_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
, 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 char *name
)
1645 g_free (edit
->filename
);
1650 edit
->filename
= tilde_expand (name
);
1651 if (edit
->dir
== NULL
&& !g_path_is_absolute (name
))
1652 edit
->dir
= vfs_get_current_dir ();
1655 /* --------------------------------------------------------------------------------------------- */
1656 /* Here we want to warn the users of overwriting an existing file,
1657 but only if they have made a change to the filename */
1658 /* returns 1 on success */
1660 edit_save_as_cmd (WEdit
* edit
)
1662 /* This heads the 'Save As' dialog box */
1665 int different_filename
= 0;
1667 if (!edit_check_newline (edit
))
1670 exp
= edit_get_save_file_as (edit
);
1671 edit_push_undo_action (edit
, KEY_PRESS
+ edit
->start_display
);
1678 edit
->force
|= REDRAW_COMPLETELY
;
1684 if (strcmp (edit
->filename
, exp
))
1689 if (mc_stat (exp
, &sb
) == 0 && !S_ISREG (sb
.st_mode
))
1691 edit_error_dialog (_("Save as"),
1693 ("Cannot save: destination is not a regular file")));
1695 edit
->force
|= REDRAW_COMPLETELY
;
1699 different_filename
= 1;
1700 file
= mc_open (exp
, O_RDONLY
| O_BINARY
);
1703 /* the file exists */
1705 /* Overwrite the current file or cancel the operation */
1706 if (edit_query_dialog2
1708 _("A file already exists with this name"), _("&Overwrite"), _("&Cancel")))
1710 edit
->force
|= REDRAW_COMPLETELY
;
1717 edit
->stat1
.st_mode
|= S_IWUSR
;
1719 save_lock
= lock_file (exp
);
1723 /* filenames equal, check if already locked */
1724 if (!edit
->locked
&& !edit
->delete_file
)
1725 save_lock
= lock_file (exp
);
1728 if (different_filename
)
1731 * Allow user to write into saved (under another name) file
1732 * even if original file had r/o user permissions.
1734 edit
->stat1
.st_mode
|= S_IWRITE
;
1737 rv
= edit_save_file (edit
, exp
);
1741 /* Succesful, so unlock both files */
1742 if (different_filename
)
1747 edit
->locked
= edit_unlock_file (edit
);
1751 if (edit
->locked
|| save_lock
)
1752 edit
->locked
= edit_unlock_file (edit
);
1755 edit_set_filename (edit
, exp
);
1756 if (edit
->lb
!= LB_ASIS
)
1757 edit_reload (edit
, exp
);
1760 edit
->delete_file
= 0;
1761 if (different_filename
)
1762 edit_load_syntax (edit
, NULL
, edit
->syntax_type
);
1763 edit
->force
|= REDRAW_COMPLETELY
;
1766 edit_error_dialog (_("Save as"), get_sys_error (_("Cannot save file")));
1769 /* Failed, so maintain modify (not save) lock */
1773 edit
->force
|= REDRAW_COMPLETELY
;
1778 edit
->force
|= REDRAW_COMPLETELY
;
1782 /* {{{ Macro stuff starts here */
1783 /* --------------------------------------------------------------------------------------------- */
1786 edit_delete_macro_cmd (WEdit
* edit
)
1790 hotkey
= editcmd_dialog_raw_key_query (_("Delete macro"), _("Press macro hotkey:"), 1);
1792 if (hotkey
!= 0 && !edit_delete_macro (edit
, hotkey
))
1793 message (D_ERROR
, _("Delete macro"), _("Macro not deleted"));
1796 /* --------------------------------------------------------------------------------------------- */
1798 /** returns FALSE on error */
1800 edit_execute_macro (WEdit
* edit
, int hotkey
)
1802 gboolean res
= FALSE
;
1806 const macros_t
*macros
;
1809 if (edit_get_macro (edit
, hotkey
, ¯os
, &indx
) &&
1810 macros
->macro
!= NULL
&& macros
->macro
->len
!= 0)
1814 edit
->force
|= REDRAW_PAGE
;
1816 for (i
= 0; i
< macros
->macro
->len
; i
++)
1818 const macro_action_t
*m_act
;
1820 m_act
= &g_array_index (macros
->macro
, struct macro_action_t
, i
);
1821 edit_execute_cmd (edit
, m_act
->action
, m_act
->ch
);
1830 /* --------------------------------------------------------------------------------------------- */
1832 /** returns FALSE on error */
1834 edit_store_macro_cmd (WEdit
* edit
)
1838 GString
*marcros_string
;
1839 mc_config_t
*macros_config
= NULL
;
1840 const char *section_name
= "editor";
1841 gchar
*macros_fname
;
1842 GArray
*macros
; /* current macro */
1844 gboolean have_macro
= FALSE
;
1845 char *skeyname
= NULL
;
1847 hotkey
= editcmd_dialog_raw_key_query (_("Save macro"), _("Press the macro's new hotkey:"), 1);
1848 if (hotkey
== ESC_CHAR
)
1851 tmp_act
= keybind_lookup_keymap_command (editor_map
, hotkey
);
1853 /* return FALSE if try assign macro into restricted hotkeys */
1854 if (tmp_act
== CK_MacroStartRecord
1855 || tmp_act
== CK_MacroStopRecord
|| tmp_act
== CK_MacroStartStopRecord
)
1858 edit_delete_macro (edit
, hotkey
);
1860 macros_fname
= mc_config_get_full_path (MC_MACRO_FILE
);
1861 macros_config
= mc_config_init (macros_fname
);
1862 g_free (macros_fname
);
1864 if (macros_config
== NULL
)
1867 edit_push_undo_action (edit
, KEY_PRESS
+ edit
->start_display
);
1869 marcros_string
= g_string_sized_new (250);
1870 macros
= g_array_new (TRUE
, FALSE
, sizeof (macro_action_t
));
1872 skeyname
= lookup_key_by_code (hotkey
);
1874 for (i
= 0; i
< macro_index
; i
++)
1876 macro_action_t m_act
;
1877 const char *action_name
;
1879 action_name
= keybind_lookup_actionname (record_macro_buf
[i
].action
);
1881 if (action_name
== NULL
)
1884 m_act
.action
= record_macro_buf
[i
].action
;
1885 m_act
.ch
= record_macro_buf
[i
].ch
;
1886 g_array_append_val (macros
, m_act
);
1888 g_string_append_printf (marcros_string
, "%s:%i;", action_name
,
1889 (int) record_macro_buf
[i
].ch
);
1894 macro
.hotkey
= hotkey
;
1895 macro
.macro
= macros
;
1896 g_array_append_val (macros_list
, macro
);
1897 mc_config_set_string (macros_config
, section_name
, skeyname
, marcros_string
->str
);
1900 mc_config_del_key (macros_config
, section_name
, skeyname
);
1903 edit_macro_sort_by_hotkey ();
1905 g_string_free (marcros_string
, TRUE
);
1906 mc_config_save_file (macros_config
, NULL
);
1907 mc_config_deinit (macros_config
);
1911 /* --------------------------------------------------------------------------------------------- */
1914 edit_repeat_macro_cmd (WEdit
* edit
)
1921 f
= input_dialog (_("Repeat last commands"), _("Repeat times:"), MC_HISTORY_EDIT_REPEAT
, NULL
);
1922 if (f
== NULL
|| *f
== '\0')
1928 count_repeat
= strtol (f
, &error
, 0);
1938 edit_push_undo_action (edit
, KEY_PRESS
+ edit
->start_display
);
1939 edit
->force
|= REDRAW_PAGE
;
1941 for (j
= 0; j
< count_repeat
; j
++)
1942 for (i
= 0; i
< macro_index
; i
++)
1943 edit_execute_cmd (edit
, record_macro_buf
[i
].action
, record_macro_buf
[i
].ch
);
1944 edit_update_screen (edit
);
1948 /* --------------------------------------------------------------------------------------------- */
1949 /** return FALSE on error */
1952 edit_load_macro_cmd (WEdit
* edit
)
1954 mc_config_t
*macros_config
= NULL
;
1955 gchar
**profile_keys
, **keys
;
1956 gchar
**values
, **curr_values
;
1957 gsize len
, values_len
;
1958 const char *section_name
= "editor";
1959 gchar
*macros_fname
;
1964 macros_fname
= mc_config_get_full_path (MC_MACRO_FILE
);
1965 macros_config
= mc_config_init (macros_fname
);
1966 g_free (macros_fname
);
1968 if (macros_config
== NULL
)
1971 profile_keys
= keys
= mc_config_get_keys (macros_config
, section_name
, &len
);
1972 while (*profile_keys
!= NULL
)
1974 gboolean have_macro
;
1978 macros
= g_array_new (TRUE
, FALSE
, sizeof (macro_action_t
));
1980 curr_values
= values
= mc_config_get_string_list (macros_config
, section_name
,
1981 *profile_keys
, &values_len
);
1982 hotkey
= lookup_key (*profile_keys
, NULL
);
1985 while (*curr_values
!= NULL
&& *curr_values
[0] != '\0')
1987 char **macro_pair
= NULL
;
1989 macro_pair
= g_strsplit (*curr_values
, ":", 2);
1991 if (macro_pair
!= NULL
)
1993 macro_action_t m_act
;
1994 if (macro_pair
[0] == NULL
|| macro_pair
[0][0] == '\0')
1998 m_act
.action
= keybind_lookup_action (macro_pair
[0]);
1999 g_free (macro_pair
[0]);
2000 macro_pair
[0] = NULL
;
2002 if (macro_pair
[1] == NULL
|| macro_pair
[1][0] == '\0')
2006 m_act
.ch
= strtol (macro_pair
[1], NULL
, 0);
2007 g_free (macro_pair
[1]);
2008 macro_pair
[1] = NULL
;
2010 if (m_act
.action
!= 0)
2012 /* a shell command */
2013 if ((m_act
.action
/ CK_PipeBlock (0)) == 1)
2015 m_act
.action
= CK_PipeBlock (0) + (m_act
.ch
> 0 ? m_act
.ch
: 0);
2018 g_array_append_val (macros
, m_act
);
2021 g_strfreev (macro_pair
);
2028 macro
.hotkey
= hotkey
;
2029 macro
.macro
= macros
;
2030 g_array_append_val (macros_list
, macro
);
2033 g_strfreev (values
);
2036 mc_config_deinit (macros_config
);
2037 edit_macro_sort_by_hotkey ();
2041 /* }}} Macro stuff end here */
2043 /* --------------------------------------------------------------------------------------------- */
2044 /** returns 1 on success */
2047 edit_save_confirm_cmd (WEdit
* edit
)
2051 if (!edit_check_newline (edit
))
2054 if (edit_confirm_save
)
2056 f
= g_strdup_printf (_("Confirm save file: \"%s\""), edit
->filename
);
2057 if (edit_query_dialog2 (_("Save file"), f
, _("&Save"), _("&Cancel")))
2064 return edit_save_cmd (edit
);
2067 /* --------------------------------------------------------------------------------------------- */
2068 /** returns 1 on success */
2071 edit_new_cmd (WEdit
* edit
)
2075 if (edit_query_dialog2
2078 ("Current text was modified without a file save.\nContinue discards these changes"),
2079 _("C&ontinue"), _("&Cancel")))
2081 edit
->force
|= REDRAW_COMPLETELY
;
2085 edit
->force
|= REDRAW_COMPLETELY
;
2087 return edit_renew (edit
); /* if this gives an error, something has really screwed up */
2090 /* --------------------------------------------------------------------------------------------- */
2093 edit_load_cmd (WEdit
* edit
, edit_current_file_t what
)
2098 && (edit_query_dialog2
2100 _("Current text was modified without a file save.\n"
2101 "Continue discards these changes"), _("C&ontinue"), _("&Cancel")) == 1))
2103 edit
->force
|= REDRAW_COMPLETELY
;
2109 case EDIT_FILE_COMMON
:
2110 exp
= input_expand_dialog (_("Load"), _("Enter file name:"),
2111 MC_HISTORY_EDIT_LOAD
, edit
->filename
);
2116 edit_load_file_from_filename (edit
, exp
);
2121 case EDIT_FILE_SYNTAX
:
2122 edit_load_syntax_file (edit
);
2125 case EDIT_FILE_MENU
:
2126 edit_load_menu_file (edit
);
2133 edit
->force
|= REDRAW_COMPLETELY
;
2137 /* --------------------------------------------------------------------------------------------- */
2139 if mark2 is -1 then marking is from mark1 to the cursor.
2140 Otherwise its between the markers. This handles this.
2141 Returns 1 if no text is marked.
2145 eval_marks (WEdit
* edit
, long *start_mark
, long *end_mark
)
2147 if (edit
->mark1
!= edit
->mark2
)
2149 long start_bol
, start_eol
;
2150 long end_bol
, end_eol
;
2155 if (edit
->end_mark_curs
< 0)
2156 end_mark_curs
= edit
->curs1
;
2158 end_mark_curs
= edit
->end_mark_curs
;
2160 if (edit
->mark2
>= 0)
2162 *start_mark
= min (edit
->mark1
, edit
->mark2
);
2163 *end_mark
= max (edit
->mark1
, edit
->mark2
);
2167 *start_mark
= min (edit
->mark1
, end_mark_curs
);
2168 *end_mark
= max (edit
->mark1
, end_mark_curs
);
2169 edit
->column2
= edit
->curs_col
+ edit
->over_col
;
2172 if (edit
->column_highlight
2173 && (((edit
->mark1
> end_mark_curs
) && (edit
->column1
< edit
->column2
))
2174 || ((edit
->mark1
< end_mark_curs
) && (edit
->column1
> edit
->column2
))))
2176 start_bol
= edit_bol (edit
, *start_mark
);
2177 start_eol
= edit_eol (edit
, start_bol
- 1) + 1;
2178 end_bol
= edit_bol (edit
, *end_mark
);
2179 end_eol
= edit_eol (edit
, *end_mark
);
2180 col1
= min (edit
->column1
, edit
->column2
);
2181 col2
= max (edit
->column1
, edit
->column2
);
2184 edit_move_forward3 (edit
, start_bol
, col2
, 0) - edit_move_forward3 (edit
, start_bol
,
2187 edit_move_forward3 (edit
, end_bol
, col2
, 0) - edit_move_forward3 (edit
, end_bol
,
2190 *start_mark
-= diff1
;
2192 *start_mark
= max (*start_mark
, start_eol
);
2193 *end_mark
= min (*end_mark
, end_eol
);
2199 *start_mark
= *end_mark
= 0;
2200 edit
->column2
= edit
->column1
= 0;
2205 /* --------------------------------------------------------------------------------------------- */
2208 edit_insert_over (WEdit
* edit
)
2212 for (i
= 0; i
< edit
->over_col
; i
++)
2214 edit_insert (edit
, ' ');
2219 /* --------------------------------------------------------------------------------------------- */
2222 edit_insert_column_of_text_from_file (WEdit
* edit
, int file
,
2223 long *start_pos
, long *end_pos
, int *col1
, int *col2
)
2227 int blocklen
= -1, width
= 0;
2228 unsigned char *data
;
2230 cursor
= edit
->curs1
;
2231 col
= edit_get_col (edit
);
2232 data
= g_malloc0 (TEMP_BUF_LEN
);
2234 while ((blocklen
= mc_read (file
, (char *) data
, TEMP_BUF_LEN
)) > 0)
2237 for (width
= 0; width
< blocklen
; width
++)
2239 if (data
[width
] == '\n')
2242 for (i
= 0; i
< blocklen
; i
++)
2244 if (data
[i
] == '\n')
2245 { /* fill in and move to next line */
2248 if (edit_get_byte (edit
, edit
->curs1
) != '\n')
2250 l
= width
- (edit_get_col (edit
) - col
);
2253 edit_insert (edit
, ' ');
2257 for (p
= edit
->curs1
;; p
++)
2259 if (p
== edit
->last_byte
)
2261 edit_cursor_move (edit
, edit
->last_byte
- edit
->curs1
);
2262 edit_insert_ahead (edit
, '\n');
2266 if (edit_get_byte (edit
, p
) == '\n')
2272 edit_cursor_move (edit
, edit_move_forward3 (edit
, p
, col
, 0) - edit
->curs1
);
2273 l
= col
- edit_get_col (edit
);
2274 while (l
>= space_width
)
2276 edit_insert (edit
, ' ');
2281 edit_insert (edit
, data
[i
]);
2285 *col2
= col
+ width
;
2286 *start_pos
= cursor
;
2287 *end_pos
= edit
->curs1
;
2288 edit_cursor_move (edit
, cursor
- edit
->curs1
);
2294 /* --------------------------------------------------------------------------------------------- */
2297 edit_block_copy_cmd (WEdit
* edit
)
2299 long start_mark
, end_mark
, current
= edit
->curs1
;
2304 unsigned char *copy_buf
;
2306 edit_update_curs_col (edit
);
2307 if (eval_marks (edit
, &start_mark
, &end_mark
))
2310 copy_buf
= edit_get_block (edit
, start_mark
, end_mark
, &size
);
2312 /* all that gets pushed are deletes hence little space is used on the stack */
2314 edit_push_markers (edit
);
2316 if (edit
->column_highlight
)
2318 col_delta
= abs (edit
->column2
- edit
->column1
);
2319 edit_insert_column_of_text (edit
, copy_buf
, size
, col_delta
, &mark1
, &mark2
, &c1
, &c2
);
2324 edit_insert_ahead (edit
, copy_buf
[size
]);
2328 edit_scroll_screen_over_cursor (edit
);
2330 if (edit
->column_highlight
)
2331 edit_set_markers (edit
, edit
->curs1
, mark2
, c1
, c2
);
2332 else if (start_mark
< current
&& end_mark
> current
)
2333 edit_set_markers (edit
, start_mark
, end_mark
+ end_mark
- start_mark
, 0, 0);
2335 edit
->force
|= REDRAW_PAGE
;
2339 /* --------------------------------------------------------------------------------------------- */
2342 edit_block_move_cmd (WEdit
* edit
)
2345 unsigned char *copy_buf
= NULL
;
2346 long start_mark
, end_mark
;
2349 if (eval_marks (edit
, &start_mark
, &end_mark
))
2352 line
= edit
->curs_line
;
2353 if (edit
->mark2
< 0)
2354 edit_mark_cmd (edit
, 0);
2355 edit_push_markers (edit
);
2357 if (edit
->column_highlight
)
2365 c1
= min (edit
->column1
, edit
->column2
);
2366 c2
= max (edit
->column1
, edit
->column2
);
2367 b_width
= (c2
- c1
);
2369 edit_update_curs_col (edit
);
2372 x2
= x
+ edit
->over_col
;
2374 /* do nothing when cursor inside first line of selected area */
2375 if ((edit_eol (edit
, edit
->curs1
) == edit_eol (edit
, start_mark
)) && (x2
> c1
&& x2
<= c2
))
2378 if (edit
->curs1
> start_mark
&& edit
->curs1
< edit_eol (edit
, end_mark
))
2382 else if (x
> c1
&& x
<= c2
)
2385 /* save current selection into buffer */
2386 copy_buf
= edit_get_block (edit
, start_mark
, end_mark
, &size
);
2388 /* remove current selection */
2389 edit_block_delete_cmd (edit
);
2391 edit
->over_col
= max (0, edit
->over_col
- b_width
);
2392 /* calculate the cursor pos after delete block */
2393 current
= edit_move_forward3 (edit
, edit_bol (edit
, edit
->curs1
), x
, 0);
2394 edit_cursor_move (edit
, current
- edit
->curs1
);
2395 edit_scroll_screen_over_cursor (edit
);
2397 /* add TWS if need before block insertion */
2398 if (option_cursor_beyond_eol
&& edit
->over_col
> 0)
2399 edit_insert_over (edit
);
2401 edit_insert_column_of_text (edit
, copy_buf
, size
, b_width
, &mark1
, &mark2
, &c1
, &c2
);
2402 edit_set_markers (edit
, mark1
, mark2
, c1
, c2
);
2408 current
= edit
->curs1
;
2409 copy_buf
= g_malloc0 (end_mark
- start_mark
);
2410 edit_cursor_move (edit
, start_mark
- edit
->curs1
);
2411 edit_scroll_screen_over_cursor (edit
);
2413 while (count
< end_mark
)
2415 copy_buf
[end_mark
- count
- 1] = edit_delete (edit
, 1);
2418 edit_scroll_screen_over_cursor (edit
);
2419 edit_cursor_move (edit
,
2420 current
- edit
->curs1
-
2421 (((current
- edit
->curs1
) > 0) ? end_mark
- start_mark
: 0));
2422 edit_scroll_screen_over_cursor (edit
);
2423 while (count
-- > start_mark
)
2424 edit_insert_ahead (edit
, copy_buf
[end_mark
- count
- 1]);
2425 edit_set_markers (edit
, edit
->curs1
, edit
->curs1
+ end_mark
- start_mark
, 0, 0);
2428 edit_scroll_screen_over_cursor (edit
);
2430 edit
->force
|= REDRAW_PAGE
;
2433 /* --------------------------------------------------------------------------------------------- */
2434 /** returns 1 if canceelled by user */
2437 edit_block_delete_cmd (WEdit
* edit
)
2439 long start_mark
, end_mark
;
2440 if (eval_marks (edit
, &start_mark
, &end_mark
))
2442 edit_delete_line (edit
);
2445 return edit_block_delete (edit
);
2448 /* --------------------------------------------------------------------------------------------- */
2449 /** call with edit = 0 before shutdown to close memory leaks */
2452 edit_replace_cmd (WEdit
* edit
, int again
)
2454 /* 1 = search string, 2 = replace with */
2455 static char *saved1
= NULL
; /* saved default[123] */
2456 static char *saved2
= NULL
;
2457 char *input1
= NULL
; /* user input from the dialog */
2458 char *input2
= NULL
;
2461 long times_replaced
= 0;
2462 gboolean once_found
= FALSE
;
2466 g_free (saved1
), saved1
= NULL
;
2467 g_free (saved2
), saved2
= NULL
;
2471 edit
->force
|= REDRAW_COMPLETELY
;
2473 if (again
&& !saved1
&& !saved2
)
2478 input1
= g_strdup (saved1
? saved1
: "");
2479 input2
= g_strdup (saved2
? saved2
: "");
2483 char *tmp_inp1
, *tmp_inp2
;
2484 disp1
= edit_replace_cmd__conv_to_display (saved1
? saved1
: (char *) "");
2485 disp2
= edit_replace_cmd__conv_to_display (saved2
? saved2
: (char *) "");
2487 edit_push_undo_action (edit
, KEY_PRESS
+ edit
->start_display
);
2489 editcmd_dialog_replace_show (edit
, disp1
, disp2
, &input1
, &input2
);
2494 if (input1
== NULL
|| *input1
== '\0')
2496 edit
->force
= REDRAW_COMPLETELY
;
2502 input1
= edit_replace_cmd__conv_to_input (input1
);
2503 input2
= edit_replace_cmd__conv_to_input (input2
);
2507 g_free (saved1
), saved1
= g_strdup (input1
);
2508 g_free (saved2
), saved2
= g_strdup (input2
);
2510 mc_search_free (edit
->search
);
2511 edit
->search
= NULL
;
2517 edit
->search
= mc_search_new (input1
, -1);
2518 if (edit
->search
== NULL
)
2520 edit
->search_start
= edit
->curs1
;
2523 edit
->search
->search_type
= edit_search_options
.type
;
2524 edit
->search
->is_all_charsets
= edit_search_options
.all_codepages
;
2525 edit
->search
->is_case_sensitive
= edit_search_options
.case_sens
;
2526 edit
->search
->whole_words
= edit_search_options
.whole_words
;
2527 edit
->search
->search_fn
= edit_search_cmd_callback
;
2528 edit
->search_line_type
= edit_get_search_line_type (edit
->search
);
2529 edit_search_fix_search_start_if_selection (edit
);
2532 if (edit
->found_len
&& edit
->search_start
== edit
->found_start
+ 1
2533 && edit_search_options
.backwards
)
2534 edit
->search_start
--;
2536 if (edit
->found_len
&& edit
->search_start
== edit
->found_start
- 1
2537 && !edit_search_options
.backwards
)
2538 edit
->search_start
++;
2544 if (!editcmd_find (edit
, &len
))
2546 if (!(edit
->search
->error
== MC_SEARCH_E_OK
||
2547 (once_found
&& edit
->search
->error
== MC_SEARCH_E_NOTFOUND
)))
2549 edit_error_dialog (_("Search"), edit
->search
->error_str
);
2555 edit
->search_start
= edit
->search
->normal_offset
;
2556 /*returns negative on not found or error in pattern */
2558 if ((edit
->search_start
>= 0) && (edit
->search_start
< edit
->last_byte
))
2561 GString
*tmp_str
, *repl_str
;
2563 edit
->found_start
= edit
->search_start
;
2564 i
= edit
->found_len
= len
;
2566 edit_cursor_move (edit
, edit
->search_start
- edit
->curs1
);
2567 edit_scroll_screen_over_cursor (edit
);
2569 if (edit
->replace_mode
== 0)
2574 l
= edit
->curs_row
- edit
->widget
.lines
/ 3;
2576 edit_scroll_downward (edit
, l
);
2578 edit_scroll_upward (edit
, -l
);
2580 edit_scroll_screen_over_cursor (edit
);
2581 edit
->force
|= REDRAW_PAGE
;
2582 edit_render_keypress (edit
);
2584 /*so that undo stops at each query */
2585 edit_push_key_press (edit
);
2586 /* and prompt 2/3 down */
2587 disp1
= edit_replace_cmd__conv_to_display (saved1
);
2588 disp2
= edit_replace_cmd__conv_to_display (saved2
);
2589 prompt
= editcmd_dialog_replace_prompt_show (edit
, disp1
, disp2
, -1, -1);
2593 if (prompt
== B_REPLACE_ALL
)
2594 edit
->replace_mode
= 1;
2595 else if (prompt
== B_SKIP_REPLACE
)
2597 if (edit_search_options
.backwards
)
2598 edit
->search_start
--;
2600 edit
->search_start
++;
2601 continue; /* loop */
2603 else if (prompt
== B_CANCEL
)
2605 edit
->replace_mode
= -1;
2610 /* don't process string each time */
2611 tmp_str
= g_string_new (input2
);
2612 repl_str
= mc_search_prepare_replace_str (edit
->search
, tmp_str
);
2613 g_string_free (tmp_str
, TRUE
);
2615 if (edit
->search
->error
!= MC_SEARCH_E_OK
)
2617 edit_error_dialog (_("Replace"), edit
->search
->error_str
);
2618 g_string_free (repl_str
, TRUE
);
2622 /* delete then insert new */
2623 for (i
= 0; i
< len
; i
++)
2624 edit_delete (edit
, 1);
2626 for (i
= 0; i
< repl_str
->len
; i
++)
2627 edit_insert (edit
, repl_str
->str
[i
]);
2629 edit
->found_len
= repl_str
->len
;
2630 g_string_free (repl_str
, TRUE
);
2633 /* so that we don't find the same string again */
2634 if (edit_search_options
.backwards
)
2636 edit
->search_start
--;
2640 edit
->search_start
+= edit
->found_len
+ (len
== 0 ? 1 : 0);
2642 if (edit
->search_start
>= edit
->last_byte
)
2646 edit_scroll_screen_over_cursor (edit
);
2650 /* try and find from right here for next search */
2651 edit
->search_start
= edit
->curs1
;
2652 edit_update_curs_col (edit
);
2654 edit
->force
|= REDRAW_PAGE
;
2655 edit_render_keypress (edit
);
2657 if (times_replaced
== 0)
2658 query_dialog (_("Replace"), _("Search string not found"), D_NORMAL
, 1, _("&OK"));
2662 while (edit
->replace_mode
>= 0);
2664 edit_scroll_screen_over_cursor (edit
);
2665 edit
->force
|= REDRAW_COMPLETELY
;
2666 edit_render_keypress (edit
);
2668 if ((edit
->replace_mode
== 1) && (times_replaced
!= 0))
2669 message (D_NORMAL
, _("Replace"), _("%ld replacements made"), times_replaced
);
2676 /* --------------------------------------------------------------------------------------------- */
2679 edit_search_cmd_callback (const void *user_data
, gsize char_offset
)
2681 return edit_get_byte ((WEdit
*) user_data
, (long) char_offset
);
2684 /* --------------------------------------------------------------------------------------------- */
2687 edit_search_cmd (WEdit
* edit
, gboolean again
)
2695 else if (edit
->last_search_string
!= NULL
)
2696 edit_do_search (edit
);
2699 /* find last search string in history */
2702 history
= history_get (MC_HISTORY_SHARED_SEARCH
);
2703 if (history
!= NULL
&& history
->data
!= NULL
)
2705 edit
->last_search_string
= (char *) history
->data
;
2706 history
->data
= NULL
;
2707 history
= g_list_first (history
);
2708 g_list_foreach (history
, (GFunc
) g_free
, NULL
);
2709 g_list_free (history
);
2711 edit
->search
= mc_search_new (edit
->last_search_string
, -1);
2712 if (edit
->search
== NULL
)
2714 /* if not... then ask for an expression */
2715 g_free (edit
->last_search_string
);
2716 edit
->last_search_string
= NULL
;
2721 edit
->search
->search_type
= edit_search_options
.type
;
2722 edit
->search
->is_all_charsets
= edit_search_options
.all_codepages
;
2723 edit
->search
->is_case_sensitive
= edit_search_options
.case_sens
;
2724 edit
->search
->whole_words
= edit_search_options
.whole_words
;
2725 edit
->search
->search_fn
= edit_search_cmd_callback
;
2726 edit
->search_line_type
= edit_get_search_line_type (edit
->search
);
2727 edit_do_search (edit
);
2732 /* if not... then ask for an expression */
2733 g_free (edit
->last_search_string
);
2734 edit
->last_search_string
= NULL
;
2741 /* --------------------------------------------------------------------------------------------- */
2743 * Check if it's OK to close the editor. If there are unsaved changes,
2744 * ask user. Return 1 if it's OK to exit, 0 to continue editing.
2748 edit_ok_to_exit (WEdit
* edit
)
2752 if (!edit
->modified
)
2755 if (!mc_global
.midnight_shutdown
)
2757 if (!edit_check_newline (edit
))
2761 act
= edit_query_dialog3 (_("Quit"), _("File was modified. Save with exit?"),
2762 _("&Yes"), _("&No"), _("&Cancel quit"));
2767 edit_query_dialog2 (_("Quit"),
2768 _("Midnight Commander is being shut down.\nSave modified file?"),
2769 _("&Yes"), _("&No"));
2779 edit_push_markers (edit
);
2780 edit_set_markers (edit
, 0, 0, 0, 0);
2781 if (!edit_save_cmd (edit
) || mc_global
.midnight_shutdown
)
2782 return mc_global
.midnight_shutdown
;
2786 case 2: /* Cancel quit */
2794 /* --------------------------------------------------------------------------------------------- */
2795 /** save block, returns 1 on success */
2798 edit_save_block (WEdit
* edit
, const char *filename
, long start
, long finish
)
2802 file
= mc_open (filename
, O_CREAT
| O_WRONLY
| O_TRUNC
,
2803 S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IROTH
| O_BINARY
);
2807 if (edit
->column_highlight
)
2810 r
= mc_write (file
, VERTICAL_MAGIC
, sizeof (VERTICAL_MAGIC
));
2813 unsigned char *block
, *p
;
2814 p
= block
= edit_get_block (edit
, start
, finish
, &len
);
2817 r
= mc_write (file
, p
, len
);
2830 len
= finish
- start
;
2831 buf
= g_malloc0 (TEMP_BUF_LEN
);
2832 while (start
!= finish
)
2834 end
= min (finish
, start
+ TEMP_BUF_LEN
);
2835 for (; i
< end
; i
++)
2836 buf
[i
- start
] = edit_get_byte (edit
, i
);
2837 len
-= mc_write (file
, (char *) buf
, end
- start
);
2848 /* --------------------------------------------------------------------------------------------- */
2851 edit_paste_from_history (WEdit
* edit
)
2854 edit_error_dialog (_("Error"), _("This function is not implemented"));
2857 /* --------------------------------------------------------------------------------------------- */
2860 edit_copy_to_X_buf_cmd (WEdit
* edit
)
2862 long start_mark
, end_mark
;
2863 if (eval_marks (edit
, &start_mark
, &end_mark
))
2865 if (!edit_save_block_to_clip_file (edit
, start_mark
, end_mark
))
2867 edit_error_dialog (_("Copy to clipboard"), get_sys_error (_("Unable to save to file")));
2870 /* try use external clipboard utility */
2871 mc_event_raise (MCEVENT_GROUP_CORE
, "clipboard_file_to_ext_clip", NULL
);
2876 /* --------------------------------------------------------------------------------------------- */
2879 edit_cut_to_X_buf_cmd (WEdit
* edit
)
2881 long start_mark
, end_mark
;
2882 if (eval_marks (edit
, &start_mark
, &end_mark
))
2884 if (!edit_save_block_to_clip_file (edit
, start_mark
, end_mark
))
2886 edit_error_dialog (_("Cut to clipboard"), _("Unable to save to file"));
2889 /* try use external clipboard utility */
2890 mc_event_raise (MCEVENT_GROUP_CORE
, "clipboard_file_to_ext_clip", NULL
);
2892 edit_block_delete_cmd (edit
);
2893 edit_mark_cmd (edit
, 1);
2897 /* --------------------------------------------------------------------------------------------- */
2900 edit_paste_from_X_buf_cmd (WEdit
* edit
)
2903 /* try use external clipboard utility */
2904 mc_event_raise (MCEVENT_GROUP_CORE
, "clipboard_file_from_ext_clip", NULL
);
2905 tmp
= mc_config_get_full_path (EDIT_CLIP_FILE
);
2906 edit_insert_file (edit
, tmp
);
2911 /* --------------------------------------------------------------------------------------------- */
2913 * Ask user for the line and go to that line.
2914 * Negative numbers mean line from the end (i.e. -1 is the last line).
2918 edit_goto_cmd (WEdit
* edit
)
2921 static long line
= 0; /* line as typed, saved as default */
2926 g_snprintf (s
, sizeof (s
), "%ld", line
);
2927 f
= input_dialog (_("Goto line"), _("Enter line:"), MC_HISTORY_EDIT_GOTO_LINE
, line
? s
: "");
2937 l
= strtol (f
, &error
, 0);
2946 l
= edit
->total_lines
+ l
+ 2;
2947 edit_move_display (edit
, l
- edit
->widget
.lines
/ 2 - 1);
2948 edit_move_to_line (edit
, l
- 1);
2949 edit
->force
|= REDRAW_COMPLETELY
;
2954 /* --------------------------------------------------------------------------------------------- */
2955 /** Return 1 on success */
2958 edit_save_block_cmd (WEdit
* edit
)
2960 long start_mark
, end_mark
;
2963 if (eval_marks (edit
, &start_mark
, &end_mark
))
2966 tmp
= mc_config_get_full_path (EDIT_CLIP_FILE
);
2968 input_expand_dialog (_("Save block"), _("Enter file name:"),
2969 MC_HISTORY_EDIT_SAVE_BLOCK
, tmp
);
2971 edit_push_undo_action (edit
, KEY_PRESS
+ edit
->start_display
);
2981 if (edit_save_block (edit
, exp
, start_mark
, end_mark
))
2984 edit
->force
|= REDRAW_COMPLETELY
;
2990 edit_error_dialog (_("Save block"), get_sys_error (_("Cannot save file")));
2994 edit
->force
|= REDRAW_COMPLETELY
;
2999 /* --------------------------------------------------------------------------------------------- */
3000 /** returns TRUE on success */
3003 edit_insert_file_cmd (WEdit
* edit
)
3007 gboolean ret
= FALSE
;
3009 tmp
= mc_config_get_full_path (EDIT_CLIP_FILE
);
3010 exp
= input_expand_dialog (_("Insert file"), _("Enter file name:"),
3011 MC_HISTORY_EDIT_INSERT_FILE
, tmp
);
3014 edit_push_undo_action (edit
, KEY_PRESS
+ edit
->start_display
);
3016 if (exp
!= NULL
&& *exp
!= '\0')
3018 ret
= (edit_insert_file (edit
, exp
) >= 0);
3020 edit_error_dialog (_("Insert file"), get_sys_error (_("Cannot insert file")));
3025 edit
->force
|= REDRAW_COMPLETELY
;
3029 /* --------------------------------------------------------------------------------------------- */
3030 /** sorts a block, returns -1 on system fail, 1 on cancel and 0 on success */
3033 edit_sort_cmd (WEdit
* edit
)
3035 static char *old
= 0;
3036 char *exp
, *tmp
, *tmp_edit_block_name
, *tmp_edit_temp_name
;
3037 long start_mark
, end_mark
;
3040 if (eval_marks (edit
, &start_mark
, &end_mark
))
3042 edit_error_dialog (_("Sort block"), _("You must first highlight a block of text"));
3046 tmp
= mc_config_get_full_path (EDIT_BLOCK_FILE
);
3047 edit_save_block (edit
, tmp
, start_mark
, end_mark
);
3050 exp
= input_dialog (_("Run sort"),
3051 _("Enter sort options (see manpage) separated by whitespace:"),
3052 MC_HISTORY_EDIT_SORT
, (old
!= NULL
) ? old
: "");
3058 tmp_edit_block_name
= mc_config_get_full_path (EDIT_BLOCK_FILE
);
3059 tmp_edit_temp_name
= mc_config_get_full_path (EDIT_TEMP_FILE
);
3061 g_strconcat (" sort ", exp
, " ", tmp_edit_block_name
,
3062 " > ", tmp_edit_temp_name
, (char *) NULL
);
3063 g_free (tmp_edit_temp_name
);
3064 g_free (tmp_edit_block_name
);
3070 if (e
== -1 || e
== 127)
3072 edit_error_dialog (_("Sort"), get_sys_error (_("Cannot execute sort command")));
3077 sprintf (q
, "%d ", e
);
3078 tmp
= g_strdup_printf (_("Sort returned non-zero: %s"), q
);
3079 edit_error_dialog (_("Sort"), tmp
);
3085 edit
->force
|= REDRAW_COMPLETELY
;
3087 if (edit_block_delete_cmd (edit
))
3089 tmp
= mc_config_get_full_path (EDIT_TEMP_FILE
);
3090 edit_insert_file (edit
, tmp
);
3095 /* --------------------------------------------------------------------------------------------- */
3097 * Ask user for a command, execute it and paste its output back to the
3102 edit_ext_cmd (WEdit
* edit
)
3104 char *exp
, *tmp
, *tmp_edit_temp_file
;
3108 input_dialog (_("Paste output of external command"),
3109 _("Enter shell command(s):"), MC_HISTORY_EDIT_PASTE_EXTCMD
, NULL
);
3114 tmp_edit_temp_file
= mc_config_get_full_path (EDIT_TEMP_FILE
);
3115 tmp
= g_strconcat (exp
, " > ", tmp_edit_temp_file
, (char *) NULL
);
3116 g_free (tmp_edit_temp_file
);
3123 edit_error_dialog (_("External command"), get_sys_error (_("Cannot execute command")));
3127 edit
->force
|= REDRAW_COMPLETELY
;
3128 tmp
= mc_config_get_full_path (EDIT_TEMP_FILE
);
3129 edit_insert_file (edit
, tmp
);
3134 /* --------------------------------------------------------------------------------------------- */
3135 /** if block is 1, a block must be highlighted and the shell command
3136 processes it. If block is 0 the shell command is a straight system
3137 command, that just produces some output which is to be inserted */
3140 edit_block_process_cmd (WEdit
* edit
, int macro_number
)
3143 char *macros_fname
= NULL
;
3145 fname
= g_strdup_printf ("%s.%i.sh", MC_EXTMACRO_FILE
, macro_number
);
3146 macros_fname
= g_build_filename (mc_config_get_data_path (), fname
, (char *) NULL
);
3147 user_menu (edit
, macros_fname
, 0);
3149 g_free (macros_fname
);
3150 edit
->force
|= REDRAW_COMPLETELY
;
3153 /* --------------------------------------------------------------------------------------------- */
3156 edit_mail_dialog (WEdit
* edit
)
3159 char *tmail_subject
;
3162 static char *mail_cc_last
= 0;
3163 static char *mail_subject_last
= 0;
3164 static char *mail_to_last
= 0;
3166 QuickWidget quick_widgets
[] = {
3167 /* 0 */ QUICK_BUTTON (6, 10, 9, MAIL_DLG_HEIGHT
, N_("&Cancel"), B_CANCEL
, NULL
),
3168 /* 1 */ QUICK_BUTTON (2, 10, 9, MAIL_DLG_HEIGHT
, N_("&OK"), B_ENTER
, NULL
),
3169 /* 2 */ QUICK_INPUT (3, 50, 8, MAIL_DLG_HEIGHT
, "", 44, 0, "mail-dlg-input", &tmail_cc
),
3170 /* 3 */ QUICK_LABEL (3, 50, 7, MAIL_DLG_HEIGHT
, N_("Copies to")),
3171 /* 4 */ QUICK_INPUT (3, 50, 6, MAIL_DLG_HEIGHT
, "", 44, 0, "mail-dlg-input-2",
3173 /* 5 */ QUICK_LABEL (3, 50, 5, MAIL_DLG_HEIGHT
, N_("Subject")),
3174 /* 6 */ QUICK_INPUT (3, 50, 4, MAIL_DLG_HEIGHT
, "", 44, 0, "mail-dlg-input-3", &tmail_to
),
3175 /* 7 */ QUICK_LABEL (3, 50, 3, MAIL_DLG_HEIGHT
, N_("To")),
3176 /* 8 */ QUICK_LABEL (3, 50, 2, MAIL_DLG_HEIGHT
, N_("mail -s <subject> -c <cc> <to>")),
3180 QuickDialog Quick_input
= {
3181 50, MAIL_DLG_HEIGHT
, -1, -1, N_("Mail"),
3182 "[Input Line Keys]", quick_widgets
, NULL
, FALSE
3185 quick_widgets
[2].u
.input
.text
= mail_cc_last
? mail_cc_last
: "";
3186 quick_widgets
[4].u
.input
.text
= mail_subject_last
? mail_subject_last
: "";
3187 quick_widgets
[6].u
.input
.text
= mail_to_last
? mail_to_last
: "";
3189 if (quick_dialog (&Quick_input
) != B_CANCEL
)
3191 g_free (mail_cc_last
);
3192 g_free (mail_subject_last
);
3193 g_free (mail_to_last
);
3194 mail_cc_last
= tmail_cc
;
3195 mail_subject_last
= tmail_subject
;
3196 mail_to_last
= tmail_to
;
3197 pipe_mail (edit
, mail_to_last
, mail_subject_last
, mail_cc_last
);
3202 /*******************/
3203 /* Word Completion */
3204 /*******************/
3206 /* --------------------------------------------------------------------------------------------- */
3208 * Complete current word using regular expression search
3209 * backwards beginning at the current cursor position.
3213 edit_complete_word_cmd (WEdit
* edit
)
3215 gsize i
, max_len
, word_len
= 0, num_compl
= 0;
3216 long word_start
= 0;
3217 unsigned char *bufpos
;
3219 struct selection
compl[MAX_WORD_COMPLETIONS
]; /* completions */
3221 /* search start of word to be completed */
3222 if (!edit_find_word_start (edit
, &word_start
, &word_len
))
3225 /* prepare match expression */
3226 bufpos
= &edit
->buffers1
[word_start
>> S_EDIT_BUF_SIZE
][word_start
& M_EDIT_BUF_SIZE
];
3228 /* match_expr = g_strdup_printf ("\\b%.*s[a-zA-Z_0-9]+", word_len, bufpos); */
3231 ("(^|\\s+|\\b)%.*s[^\\s\\.=\\+\\[\\]\\(\\)\\,\\;\\:\\\"\\'\\-\\?\\/\\|\\\\\\{\\}\\*\\&\\^\\%%\\$#@\\!]+",
3232 (int) word_len
, bufpos
);
3234 /* collect the possible completions */
3235 /* start search from begin to end of file */
3237 edit_collect_completions (edit
, word_start
, word_len
, match_expr
,
3238 (struct selection
*) &compl, &num_compl
);
3242 /* insert completed word if there is only one match */
3245 for (i
= word_len
; i
< compl[0].len
; i
++)
3246 edit_insert (edit
, *(compl[0].text
+ i
));
3248 /* more than one possible completion => ask the user */
3251 /* !!! usually only a beep is expected and when <ALT-TAB> is !!! */
3252 /* !!! pressed again the selection dialog pops up, but that !!! */
3253 /* !!! seems to require a further internal state !!! */
3256 /* let the user select the preferred completion */
3257 editcmd_dialog_completion_show (edit
, max_len
, word_len
,
3258 (struct selection
*) &compl, num_compl
);
3262 g_free (match_expr
);
3263 /* release memory before return */
3264 for (i
= 0; i
< num_compl
; i
++)
3265 g_free (compl[i
].text
);
3268 /* --------------------------------------------------------------------------------------------- */
3271 edit_select_codepage_cmd (WEdit
* edit
)
3274 if (do_select_codepage ())
3275 edit_set_codeset (edit
);
3277 edit
->force
= REDRAW_COMPLETELY
;
3278 edit_refresh_cmd (edit
);
3284 /* --------------------------------------------------------------------------------------------- */
3287 edit_insert_literal_cmd (WEdit
* edit
)
3289 int char_for_insertion
= editcmd_dialog_raw_key_query (_("Insert literal"),
3290 _("Press any key:"), 0);
3291 edit_execute_key_command (edit
, -1, ascii_alpha_to_cntrl (char_for_insertion
));
3294 /* --------------------------------------------------------------------------------------------- */
3297 edit_begin_end_macro_cmd (WEdit
* edit
)
3299 /* edit is a pointer to the widget */
3302 unsigned long command
= macro_index
< 0 ? CK_MacroStartRecord
: CK_MacroStopRecord
;
3303 edit_execute_key_command (edit
, command
, -1);
3307 /* --------------------------------------------------------------------------------------------- */
3310 edit_begin_end_repeat_cmd (WEdit
* edit
)
3312 /* edit is a pointer to the widget */
3315 unsigned long command
= macro_index
< 0 ? CK_RepeatStartRecord
: CK_RepeatStopRecord
;
3316 edit_execute_key_command (edit
, command
, -1);
3320 /* --------------------------------------------------------------------------------------------- */
3323 edit_load_forward_cmd (WEdit
* edit
)
3327 if (edit_query_dialog2
3329 _("Current text was modified without a file save\n"
3330 "Continue discards these changes"), _("C&ontinue"), _("&Cancel")))
3332 edit
->force
|= REDRAW_COMPLETELY
;
3336 if (edit_stack_iterator
+ 1 < MAX_HISTORY_MOVETO
)
3338 if (edit_history_moveto
[edit_stack_iterator
+ 1].line
< 1)
3342 edit_stack_iterator
++;
3343 if (edit_history_moveto
[edit_stack_iterator
].filename
)
3345 edit_reload_line (edit
, edit_history_moveto
[edit_stack_iterator
].filename
,
3346 edit_history_moveto
[edit_stack_iterator
].line
);
3360 /* --------------------------------------------------------------------------------------------- */
3363 edit_load_back_cmd (WEdit
* edit
)
3367 if (edit_query_dialog2
3369 _("Current text was modified without a file save\n"
3370 "Continue discards these changes"), _("C&ontinue"), _("&Cancel")))
3372 edit
->force
|= REDRAW_COMPLETELY
;
3376 if (edit_stack_iterator
> 0)
3378 edit_stack_iterator
--;
3379 if (edit_history_moveto
[edit_stack_iterator
].filename
)
3381 edit_reload_line (edit
, edit_history_moveto
[edit_stack_iterator
].filename
,
3382 edit_history_moveto
[edit_stack_iterator
].line
);
3396 /* --------------------------------------------------------------------------------------------- */
3399 edit_get_match_keyword_cmd (WEdit
* edit
)
3401 gsize word_len
= 0, max_len
= 0;
3404 long word_start
= 0;
3405 unsigned char *bufpos
;
3409 char *tagfile
= NULL
;
3411 etags_hash_t def_hash
[MAX_DEFINITIONS
];
3413 for (i
= 0; i
< MAX_DEFINITIONS
; i
++)
3415 def_hash
[i
].filename
= NULL
;
3418 /* search start of word to be completed */
3419 if (!edit_find_word_start (edit
, &word_start
, &word_len
))
3422 /* prepare match expression */
3423 bufpos
= &edit
->buffers1
[word_start
>> S_EDIT_BUF_SIZE
][word_start
& M_EDIT_BUF_SIZE
];
3424 match_expr
= g_strdup_printf ("%.*s", (int) word_len
, bufpos
);
3426 ptr
= g_get_current_dir ();
3427 path
= g_strconcat (ptr
, G_DIR_SEPARATOR_S
, (char *) NULL
);
3430 /* Recursive search file 'TAGS' in parent dirs */
3433 ptr
= g_path_get_dirname (path
);
3437 tagfile
= mc_build_filename (path
, TAGS_NAME
, (char *) NULL
);
3438 if (exist_file (tagfile
))
3441 while (strcmp (path
, G_DIR_SEPARATOR_S
) != 0);
3446 etags_set_definition_hash (tagfile
, path
, match_expr
, (etags_hash_t
*) & def_hash
);
3451 max_len
= MAX_WIDTH_DEF_DIALOG
;
3455 editcmd_dialog_select_definition_show (edit
, match_expr
, max_len
, word_len
,
3456 (etags_hash_t
*) & def_hash
, num_def
);
3458 g_free (match_expr
);
3461 /* --------------------------------------------------------------------------------------------- */