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 */
42 #include <sys/types.h>
50 #include "lib/global.h"
51 #include "lib/tty/tty.h"
52 #include "lib/tty/key.h" /* XCTRL */
53 #include "lib/mcconfig.h"
55 #include "lib/strutil.h" /* utf string functions */
57 #include "lib/util.h" /* tilde_expand() */
58 #include "lib/vfs/vfs.h"
59 #include "lib/widget.h"
60 #include "lib/charsets.h"
61 #include "lib/event.h" /* mc_event_raise() */
63 #include "src/history.h"
64 #include "src/setup.h" /* option_tab_spacing */
65 #include "src/main.h" /* mactos_t */
66 #include "src/selcodepage.h"
67 #include "src/keybind-defaults.h"
68 #include "src/util.h" /* check_for_default() */
69 #include "src/filemanager/layout.h" /* mc_refresh() */
71 #include "edit-impl.h"
72 #include "edit-widget.h"
73 #include "editcmd_dialogs.h"
76 /*** global variables ****************************************************************************/
78 /* search and replace: */
79 int search_create_bookmark
= FALSE
;
81 /* queries on a save */
82 int edit_confirm_save
= 1;
84 /*** file scope macro definitions ****************************************************************/
88 #define TEMP_BUF_LEN 1024
92 /* thanks to Liviu Daia <daia@stoilow.imar.ro> for getting this
93 (and the above) routines to work properly - paul */
95 #define is_digit(x) ((x) >= '0' && (x) <= '9')
97 #define MAIL_DLG_HEIGHT 12
99 #define MAX_WORD_COMPLETIONS 100 /* in listbox */
101 /*** file scope type declarations ****************************************************************/
103 /*** file scope variables ************************************************************************/
105 /*** file scope functions ************************************************************************/
106 /* --------------------------------------------------------------------------------------------- */
108 /* If 0 (quick save) then a) create/truncate <filename> file,
109 b) save to <filename>;
110 if 1 (safe save) then a) save to <tempnam>,
111 b) rename <tempnam> to <filename>;
112 if 2 (do backups) then a) save to <tempnam>,
113 b) rename <filename> to <filename.backup_ext>,
114 c) rename <tempnam> to <filename>. */
116 /* returns 0 on error, -1 on abort */
119 edit_save_file (WEdit
* edit
, const char *filename
)
125 gchar
*real_filename
;
126 int this_save_mode
, fd
= -1;
133 if (*filename
!= PATH_SEP
&& edit
->dir
)
135 real_filename
= concat_dir_and_file (edit
->dir
, filename
);
139 real_filename
= g_strdup (filename
);
142 this_save_mode
= option_save_mode
;
143 if (this_save_mode
!= EDIT_QUICK_SAVE
)
145 vfs_path_t
*vpath
= vfs_path_from_str (real_filename
);
146 if (!vfs_file_is_local (vpath
) || (fd
= mc_open (real_filename
, O_RDONLY
| O_BINARY
)) == -1)
149 * The file does not exists yet, so no safe save or
150 * backup are necessary.
152 this_save_mode
= EDIT_QUICK_SAVE
;
154 vfs_path_free (vpath
);
159 if (this_save_mode
== EDIT_QUICK_SAVE
&& !edit
->skip_detach_prompt
)
164 rv
= mc_stat (real_filename
, &sb
);
165 if (rv
== 0 && sb
.st_nlink
> 1)
167 rv
= edit_query_dialog3 (_("Warning"),
168 _("File has hard-links. Detach before saving?"),
169 _("&Yes"), _("&No"), _("&Cancel"));
173 this_save_mode
= EDIT_SAFE_SAVE
;
176 edit
->skip_detach_prompt
= 1;
179 g_free (real_filename
);
184 /* Prevent overwriting changes from other editor sessions. */
185 if (rv
== 0 && edit
->stat1
.st_mtime
!= 0 && edit
->stat1
.st_mtime
!= sb
.st_mtime
)
188 /* The default action is "Cancel". */
191 rv
= edit_query_dialog2 (_("Warning"),
192 _("The file has been modified in the meantime. Save anyway?"),
193 _("&Yes"), _("&Cancel"));
196 g_free (real_filename
);
202 if (this_save_mode
!= EDIT_QUICK_SAVE
)
204 char *savedir
, *saveprefix
;
205 const char *slashpos
;
206 slashpos
= strrchr (real_filename
, PATH_SEP
);
209 savedir
= g_strdup (real_filename
);
210 savedir
[slashpos
- real_filename
+ 1] = '\0';
213 savedir
= g_strdup (".");
214 saveprefix
= concat_dir_and_file (savedir
, "cooledit");
216 fd
= mc_mkstemps (&savename
, saveprefix
, NULL
);
220 g_free (real_filename
);
224 * Close for now because mc_mkstemps use pure open system call
225 * to create temporary file and it needs to be reopened by
226 * VFS-aware mc_open().
231 savename
= g_strdup (real_filename
);
234 ret
= mc_chown (savename
, edit
->stat1
.st_uid
, edit
->stat1
.st_gid
);
235 ret
= mc_chmod (savename
, edit
->stat1
.st_mode
);
238 fd
= mc_open (savename
, O_CREAT
| O_WRONLY
| O_TRUNC
| O_BINARY
, edit
->stat1
.st_mode
);
243 p
= edit_get_write_filter (savename
, real_filename
);
249 file
= (FILE *) popen (p
, "w");
253 filelen
= edit_write_stream (edit
, file
);
257 if (pclose (file
) != 0)
259 tmp
= g_strdup_printf (_("Error writing to pipe: %s"), p
);
260 edit_error_dialog (_("Error"), tmp
);
269 tmp
= g_strdup_printf (_("Cannot open pipe for writing: %s"), p
);
270 edit_error_dialog (_("Error"), get_sys_error (tmp
));
277 else if (edit
->lb
== LB_ASIS
)
278 { /* do not change line breaks */
281 filelen
= edit
->last_byte
;
282 while (buf
<= (edit
->curs1
>> S_EDIT_BUF_SIZE
) - 1)
284 if (mc_write (fd
, (char *) edit
->buffers1
[buf
], EDIT_BUF_SIZE
) != EDIT_BUF_SIZE
)
292 (fd
, (char *) edit
->buffers1
[buf
],
293 edit
->curs1
& M_EDIT_BUF_SIZE
) != (edit
->curs1
& M_EDIT_BUF_SIZE
))
297 else if (edit
->curs2
)
300 buf
= (edit
->curs2
>> S_EDIT_BUF_SIZE
);
303 (char *) edit
->buffers2
[buf
] + EDIT_BUF_SIZE
-
304 (edit
->curs2
& M_EDIT_BUF_SIZE
) - 1,
305 1 + (edit
->curs2
& M_EDIT_BUF_SIZE
)) != 1 + (edit
->curs2
& M_EDIT_BUF_SIZE
))
313 if (mc_write (fd
, (char *) edit
->buffers2
[buf
], EDIT_BUF_SIZE
) != EDIT_BUF_SIZE
)
325 /* Update the file information, especially the mtime. */
326 if (mc_stat (savename
, &edit
->stat1
) == -1)
330 { /* change line breaks */
335 file
= (FILE *) fopen (savename
, "w");
339 filelen
= edit_write_stream (edit
, file
);
346 msg
= g_strdup_printf (_("Cannot open file for writing: %s"), savename
);
347 edit_error_dialog (_("Error"), msg
);
353 if (filelen
!= edit
->last_byte
)
356 if (this_save_mode
== EDIT_DO_BACKUP
)
358 assert (option_backup_ext
!= NULL
);
359 tmp
= g_strconcat (real_filename
, option_backup_ext
, (char *) NULL
);
360 if (mc_rename (real_filename
, tmp
) == -1)
367 if (this_save_mode
!= EDIT_QUICK_SAVE
)
368 if (mc_rename (savename
, real_filename
) == -1)
371 g_free (real_filename
);
374 /* FIXME: Is this safe ?
375 * if (this_save_mode != EDIT_QUICK_SAVE)
376 * mc_unlink (savename);
378 g_free (real_filename
);
383 /* --------------------------------------------------------------------------------------------- */
386 edit_check_newline (WEdit
* edit
)
388 return !(option_check_nl_at_eof
&& edit
->last_byte
> 0
389 && edit_get_byte (edit
, edit
->last_byte
- 1) != '\n'
390 && edit_query_dialog2 (_("Warning"),
391 _("The file you are saving is not finished with a newline"),
392 _("C&ontinue"), _("&Cancel")));
395 /* --------------------------------------------------------------------------------------------- */
398 edit_get_save_file_as (WEdit
* edit
)
401 #define DLG_HEIGHT 14
403 static LineBreaks cur_lb
= LB_ASIS
;
405 char *filename
= edit
->filename
;
407 const char *lb_names
[LB_NAMES
] = {
408 N_("&Do not change"),
409 N_("&Unix format (LF)"),
410 N_("&Windows/DOS format (CR LF)"),
411 N_("&Macintosh format (CR)")
414 QuickWidget quick_widgets
[] = {
415 QUICK_BUTTON (6, 10, DLG_HEIGHT
- 3, DLG_HEIGHT
, N_("&Cancel"), B_CANCEL
, NULL
),
416 QUICK_BUTTON (2, 10, DLG_HEIGHT
- 3, DLG_HEIGHT
, N_("&OK"), B_ENTER
, NULL
),
417 QUICK_RADIO (5, DLG_WIDTH
, DLG_HEIGHT
- 8, DLG_HEIGHT
, LB_NAMES
, lb_names
, (int *) &cur_lb
),
418 QUICK_LABEL (3, DLG_WIDTH
, DLG_HEIGHT
- 9, DLG_HEIGHT
, N_("Change line breaks to:")),
419 QUICK_INPUT (3, DLG_WIDTH
, DLG_HEIGHT
- 11, DLG_HEIGHT
, filename
, DLG_WIDTH
- 6, 0,
420 "save-as", &filename
),
421 QUICK_LABEL (3, DLG_WIDTH
, DLG_HEIGHT
- 12, DLG_HEIGHT
, N_("Enter file name:")),
425 QuickDialog Quick_options
= {
426 DLG_WIDTH
, DLG_HEIGHT
, -1, -1,
427 N_("Save As"), "[Save File As]",
428 quick_widgets
, NULL
, FALSE
431 if (quick_dialog (&Quick_options
) != B_CANCEL
)
436 fname
= tilde_expand (filename
);
447 /** returns 1 on success */
450 edit_save_cmd (WEdit
* edit
)
452 int res
, save_lock
= 0;
454 if (!edit
->locked
&& !edit
->delete_file
)
455 save_lock
= edit_lock_file (edit
);
456 res
= edit_save_file (edit
, edit
->filename
);
458 /* Maintain modify (not save) lock on failure */
459 if ((res
> 0 && edit
->locked
) || save_lock
)
460 edit
->locked
= edit_unlock_file (edit
);
462 /* On failure try 'save as', it does locking on its own */
464 return edit_save_as_cmd (edit
);
465 edit
->force
|= REDRAW_COMPLETELY
;
468 edit
->delete_file
= 0;
475 /* --------------------------------------------------------------------------------------------- */
476 /** returns 1 on error */
479 edit_load_file_from_filename (WEdit
* edit
, char *exp
)
481 int prev_locked
= edit
->locked
;
482 char *prev_filename
= g_strdup (edit
->filename
);
484 if (!edit_reload (edit
, exp
))
486 g_free (prev_filename
);
494 fullpath
= mc_build_filename (edit
->dir
, prev_filename
, (char *) NULL
);
495 unlock_file (fullpath
);
498 g_free (prev_filename
);
502 /* --------------------------------------------------------------------------------------------- */
505 edit_load_syntax_file (WEdit
* edit
)
512 dir
= query_dialog (_("Syntax file edit"),
513 _("Which syntax file you want to edit?"), D_NORMAL
, 2,
514 _("&User"), _("&System Wide"));
517 extdir
= g_build_filename (mc_global
.sysconfig_dir
, "syntax", "Syntax", (char *) NULL
);
518 if (!exist_file (extdir
))
521 extdir
= g_build_filename (mc_global
.share_data_dir
, "syntax", "Syntax", (char *) NULL
);
528 buffer
= mc_config_get_full_path (EDIT_SYNTAX_FILE
);
529 check_for_default (extdir
, buffer
);
530 edit_load_file_from_filename (edit
, buffer
);
534 edit_load_file_from_filename (edit
, extdir
);
539 /* --------------------------------------------------------------------------------------------- */
542 edit_load_menu_file (WEdit
* edit
)
548 dir
= query_dialog (_("Menu edit"),
549 _("Which menu file do you want to edit?"), D_NORMAL
,
550 geteuid () != 0 ? 2 : 3, _("&Local"), _("&User"), _("&System Wide"));
552 menufile
= concat_dir_and_file (mc_global
.sysconfig_dir
, EDIT_GLOBAL_MENU
);
554 if (!exist_file (menufile
))
557 menufile
= concat_dir_and_file (mc_global
.share_data_dir
, EDIT_GLOBAL_MENU
);
563 buffer
= g_strdup (EDIT_LOCAL_MENU
);
564 check_for_default (menufile
, buffer
);
565 chmod (buffer
, 0600);
569 buffer
= mc_config_get_full_path (EDIT_HOME_MENU
);
570 check_for_default (menufile
, buffer
);
574 buffer
= concat_dir_and_file (mc_global
.sysconfig_dir
, EDIT_GLOBAL_MENU
);
575 if (!exist_file (buffer
))
578 buffer
= concat_dir_and_file (mc_global
.share_data_dir
, EDIT_GLOBAL_MENU
);
587 edit_load_file_from_filename (edit
, buffer
);
593 /* --------------------------------------------------------------------------------------------- */
596 edit_delete_column_of_text (WEdit
* edit
)
598 long p
, q
, r
, m1
, m2
;
601 eval_marks (edit
, &m1
, &m2
);
602 n
= edit_move_forward (edit
, m1
, 0, m2
) + 1;
603 c
= edit_move_forward3 (edit
, edit_bol (edit
, m1
), 0, m1
);
604 d
= edit_move_forward3 (edit
, edit_bol (edit
, m2
), 0, m2
);
605 b
= max (min (c
, d
), min (edit
->column1
, edit
->column2
));
606 c
= max (c
, max (edit
->column1
, edit
->column2
));
610 r
= edit_bol (edit
, edit
->curs1
);
611 p
= edit_move_forward3 (edit
, r
, b
, 0);
612 q
= edit_move_forward3 (edit
, r
, c
, 0);
617 edit_cursor_move (edit
, p
- edit
->curs1
);
620 /* delete line between margins */
621 if (edit_get_byte (edit
, edit
->curs1
) != '\n')
622 edit_delete (edit
, 1);
626 /* move to next line except on the last delete */
627 edit_cursor_move (edit
, edit_move_forward (edit
, edit
->curs1
, 1, 0) - edit
->curs1
);
631 /* --------------------------------------------------------------------------------------------- */
632 /** if success return 0 */
635 edit_block_delete (WEdit
* edit
)
638 long start_mark
, end_mark
;
639 int curs_pos
, line_width
;
640 long curs_line
, c1
, c2
;
642 if (eval_marks (edit
, &start_mark
, &end_mark
))
644 if (edit
->column_highlight
&& edit
->mark2
< 0)
645 edit_mark_cmd (edit
, 0);
646 if ((end_mark
- start_mark
) > option_max_undo
/ 2)
648 /* Warning message with a query to continue or cancel the operation */
649 if (edit_query_dialog2
652 ("Block is large, you may not be able to undo this action"),
653 _("C&ontinue"), _("&Cancel")))
658 c1
= min (edit
->column1
, edit
->column2
);
659 c2
= max (edit
->column1
, edit
->column2
);
663 edit_push_markers (edit
);
665 curs_line
= edit
->curs_line
;
667 /* calculate line width and cursor position before cut */
668 line_width
= edit_move_forward3 (edit
, edit_bol (edit
, edit
->curs1
), 0,
669 edit_eol (edit
, edit
->curs1
));
670 curs_pos
= edit
->curs_col
+ edit
->over_col
;
672 /* move cursor to start of selection */
673 edit_cursor_move (edit
, start_mark
- edit
->curs1
);
674 edit_scroll_screen_over_cursor (edit
);
676 if (start_mark
< end_mark
)
678 if (edit
->column_highlight
)
681 edit_mark_cmd (edit
, 0);
682 edit_delete_column_of_text (edit
);
683 /* move cursor to the saved position */
684 edit_move_to_line (edit
, curs_line
);
685 /* calculate line width after cut */
686 line_width
= edit_move_forward3 (edit
, edit_bol (edit
, edit
->curs1
), 0,
687 edit_eol (edit
, edit
->curs1
));
688 if (option_cursor_beyond_eol
&& curs_pos
> line_width
)
689 edit
->over_col
= curs_pos
- line_width
;
693 while (count
< end_mark
)
695 edit_delete (edit
, 1);
700 edit_set_markers (edit
, 0, 0, 0, 0);
701 edit
->force
|= REDRAW_PAGE
;
705 /* --------------------------------------------------------------------------------------------- */
708 editcmd_find (WEdit
* edit
, gsize
* len
)
710 off_t search_start
= edit
->search_start
;
713 long end_mark
= edit
->last_byte
;
716 if (edit_search_options
.only_in_selection
)
718 mark_res
= eval_marks (edit
, &start_mark
, &end_mark
);
721 edit
->search
->error
= MC_SEARCH_E_NOTFOUND
;
722 edit
->search
->error_str
= g_strdup (_("Search string not found"));
725 if (edit_search_options
.backwards
)
727 if (search_start
> end_mark
|| search_start
<= start_mark
)
729 search_start
= end_mark
;
734 if (search_start
< start_mark
|| search_start
>= end_mark
)
736 search_start
= start_mark
;
742 if (edit_search_options
.backwards
)
743 end_mark
= max (1, edit
->curs1
) - 1;
745 if (edit_search_options
.backwards
)
747 search_end
= end_mark
;
748 while ((int) search_start
>= start_mark
)
750 if (search_end
> (off_t
) (search_start
+ edit
->search
->original_len
) &&
751 mc_search_is_fixed_search_str (edit
->search
))
753 search_end
= search_start
+ edit
->search
->original_len
;
755 if (mc_search_run (edit
->search
, (void *) edit
, search_start
, search_end
, len
)
756 && edit
->search
->normal_offset
== search_start
)
762 edit
->search
->error_str
= g_strdup (_("Search string not found"));
766 return mc_search_run (edit
->search
, (void *) edit
, search_start
, end_mark
, len
);
771 /* --------------------------------------------------------------------------------------------- */
774 edit_replace_cmd__conv_to_display (char *str
)
779 tmp
= str_convert_to_display (str
);
783 return g_string_free (tmp
, FALSE
);
784 g_string_free (tmp
, TRUE
);
787 return g_strdup (str
);
790 /* --------------------------------------------------------------------------------------------- */
793 edit_replace_cmd__conv_to_input (char *str
)
798 tmp
= str_convert_to_input (str
);
802 return g_string_free (tmp
, FALSE
);
803 g_string_free (tmp
, TRUE
);
806 return g_strdup (str
);
809 /* --------------------------------------------------------------------------------------------- */
812 edit_do_search (WEdit
* edit
)
816 if (edit
->search
== NULL
)
817 edit
->search_start
= edit
->curs1
;
819 edit_push_undo_action (edit
, KEY_PRESS
+ edit
->start_display
);
821 if (search_create_bookmark
)
823 int found
= 0, books
= 0;
824 long l
= 0, l_last
= -1;
827 search_create_bookmark
= FALSE
;
828 book_mark_flush (edit
, -1);
832 if (!mc_search_run (edit
->search
, (void *) edit
, q
, edit
->last_byte
, &len
))
835 edit
->search_start
= edit
->search
->normal_offset
;
837 l
+= edit_count_lines (edit
, q
, edit
->search
->normal_offset
);
840 book_mark_insert (edit
, l
, BOOK_MARK_FOUND_COLOR
);
844 q
= edit
->search
->normal_offset
+ 1;
848 edit_error_dialog (_("Search"), _("Search string not found"));
850 edit_cursor_move (edit
, edit
->search_start
- edit
->curs1
);
854 if (edit
->found_len
!= 0 && edit
->search_start
== edit
->found_start
+ 1
855 && edit_search_options
.backwards
)
856 edit
->search_start
--;
858 if (edit
->found_len
!= 0 && edit
->search_start
== edit
->found_start
- 1
859 && !edit_search_options
.backwards
)
860 edit
->search_start
++;
862 if (editcmd_find (edit
, &len
))
864 edit
->found_start
= edit
->search_start
= edit
->search
->normal_offset
;
865 edit
->found_len
= len
;
867 edit_cursor_move (edit
, edit
->search_start
- edit
->curs1
);
868 edit_scroll_screen_over_cursor (edit
);
869 if (edit_search_options
.backwards
)
870 edit
->search_start
--;
872 edit
->search_start
++;
876 edit
->search_start
= edit
->curs1
;
877 if (edit
->search
->error_str
!= NULL
)
878 edit_error_dialog (_("Search"), edit
->search
->error_str
);
882 edit
->force
|= REDRAW_COMPLETELY
;
883 edit_scroll_screen_over_cursor (edit
);
886 /* --------------------------------------------------------------------------------------------- */
889 edit_search (WEdit
* edit
)
891 if (editcmd_dialog_search_show (edit
))
892 edit_do_search (edit
);
895 /* --------------------------------------------------------------------------------------------- */
896 /** Return a null terminated length of text. Result must be g_free'd */
898 static unsigned char *
899 edit_get_block (WEdit
* edit
, long start
, long finish
, int *l
)
901 unsigned char *s
, *r
;
902 r
= s
= g_malloc0 (finish
- start
+ 1);
903 if (edit
->column_highlight
)
906 /* copy from buffer, excluding chars that are out of the column 'margins' */
907 while (start
< finish
)
911 x
= edit_move_forward3 (edit
, edit_bol (edit
, start
), 0, start
);
912 c
= edit_get_byte (edit
, start
);
913 if ((x
>= edit
->column1
&& x
< edit
->column2
)
914 || (x
>= edit
->column2
&& x
< edit
->column1
) || c
== '\n')
925 while (start
< finish
)
926 *s
++ = edit_get_byte (edit
, start
++);
932 /* --------------------------------------------------------------------------------------------- */
933 /** copies a block to clipboard file */
936 edit_save_block_to_clip_file (WEdit
* edit
, long start
, long finish
)
940 tmp
= mc_config_get_full_path (EDIT_CLIP_FILE
);
941 ret
= edit_save_block (edit
, tmp
, start
, finish
);
946 /* --------------------------------------------------------------------------------------------- */
949 pipe_mail (WEdit
* edit
, char *to
, char *subject
, char *cc
)
954 to
= name_quote (to
, 0);
955 subject
= name_quote (subject
, 0);
956 cc
= name_quote (cc
, 0);
957 s
= g_strconcat ("mail -s ", subject
, *cc
? " -c " : "", cc
, " ", to
, (char *) NULL
);
971 for (i
= 0; i
< edit
->last_byte
; i
++)
972 fputc (edit_get_byte (edit
, i
), p
);
977 /* --------------------------------------------------------------------------------------------- */
980 is_break_char (char c
)
982 return (isspace (c
) || strchr ("{}[]()<>=|/\\!?~-+`'\",.;:#$%^&*", c
));
985 /* --------------------------------------------------------------------------------------------- */
986 /** find first character of current word */
989 edit_find_word_start (WEdit
* edit
, long *word_start
, gsize
* word_len
)
994 /* return if at begin of file */
995 if (edit
->curs1
<= 0)
998 c
= (unsigned char) edit_get_byte (edit
, edit
->curs1
- 1);
999 /* return if not at end or in word */
1000 if (is_break_char (c
))
1003 /* search start of word to be completed */
1006 /* return if at begin of file */
1007 if ((gsize
) edit
->curs1
< i
)
1011 c
= (unsigned char) edit_get_byte (edit
, edit
->curs1
- i
);
1013 if (is_break_char (c
))
1015 /* return if word starts with digit */
1019 *word_start
= edit
->curs1
- (i
- 1); /* start found */
1028 /* --------------------------------------------------------------------------------------------- */
1030 * Get current word under cursor
1032 * @param edit editor object
1033 * @param srch mc_search object
1034 * @param word_start start word position
1036 * @return newly allocated string or NULL if no any words under cursor
1040 edit_collect_completions_get_current_word (WEdit
* edit
, mc_search_t
* srch
, long word_start
)
1045 if (!mc_search_run (srch
, (void *) edit
, word_start
, edit
->last_byte
, &len
))
1048 temp
= g_string_sized_new (len
);
1050 for (i
= 0; i
< len
; i
++)
1054 chr
= edit_get_byte (edit
, word_start
+ i
);
1056 g_string_append_c (temp
, chr
);
1059 return g_string_free (temp
, temp
->len
== 0);
1062 /* --------------------------------------------------------------------------------------------- */
1063 /** collect the possible completions */
1066 edit_collect_completions (WEdit
* edit
, long word_start
, gsize word_len
,
1067 char *match_expr
, struct selection
*compl, gsize
* num
)
1075 long last_byte
, start
= -1;
1078 srch
= mc_search_new (match_expr
, -1);
1082 if (mc_config_get_bool
1083 (mc_main_config
, CONFIG_APP_SECTION
, "editor_wordcompletion_collect_entire_file", 0))
1085 last_byte
= edit
->last_byte
;
1089 last_byte
= word_start
;
1092 srch
->search_type
= MC_SEARCH_T_REGEX
;
1093 srch
->is_case_sensitive
= TRUE
;
1094 srch
->search_fn
= edit_search_cmd_callback
;
1096 current_word
= edit_collect_completions_get_current_word (edit
, srch
, word_start
);
1098 temp
= g_string_new ("");
1100 /* collect max MAX_WORD_COMPLETIONS completions */
1101 while (mc_search_run (srch
, (void *) edit
, start
+ 1, last_byte
, &len
))
1103 g_string_set_size (temp
, 0);
1104 start
= srch
->normal_offset
;
1106 /* add matched completion if not yet added */
1107 for (i
= 0; i
< len
; i
++)
1109 skip
= edit_get_byte (edit
, start
+ i
);
1113 /* skip current word */
1114 if (start
+ (long) i
== word_start
)
1117 g_string_append_c (temp
, skip
);
1123 if (current_word
!= NULL
&& strcmp (current_word
, temp
->str
) == 0)
1128 for (i
= 0; i
< *num
; i
++)
1131 ((char *) &compl[i
].text
[word_len
],
1132 (char *) &temp
->str
[word_len
], max (len
, compl[i
].len
) - word_len
) == 0)
1134 struct selection
this = compl[i
];
1135 for (++i
; i
< *num
; i
++)
1137 compl[i
- 1] = compl[i
];
1139 compl[*num
- 1] = this;
1141 break; /* skip it, already added */
1147 if (*num
== MAX_WORD_COMPLETIONS
)
1149 g_free (compl[0].text
);
1150 for (i
= 1; i
< *num
; i
++)
1152 compl[i
- 1] = compl[i
];
1159 recoded
= str_convert_to_display (temp
->str
);
1161 if (recoded
&& recoded
->len
)
1162 g_string_assign (temp
, recoded
->str
);
1164 g_string_free (recoded
, TRUE
);
1167 compl[*num
].text
= g_strdup (temp
->str
);
1168 compl[*num
].len
= temp
->len
;
1172 /* note the maximal length needed for the completion dialog */
1177 mc_search_free (srch
);
1178 g_string_free (temp
, TRUE
);
1179 g_free (current_word
);
1184 /* --------------------------------------------------------------------------------------------- */
1187 edit_insert_column_of_text (WEdit
* edit
, unsigned char *data
, int size
, int width
,
1188 long *start_pos
, long *end_pos
, int *col1
, int *col2
)
1193 cursor
= edit
->curs1
;
1194 col
= edit_get_col (edit
);
1196 for (i
= 0; i
< size
; i
++)
1198 if (data
[i
] != '\n')
1199 edit_insert (edit
, data
[i
]);
1201 { /* fill in and move to next line */
1205 if (edit_get_byte (edit
, edit
->curs1
) != '\n')
1207 l
= width
- (edit_get_col (edit
) - col
);
1210 edit_insert (edit
, ' ');
1214 for (p
= edit
->curs1
;; p
++)
1216 if (p
== edit
->last_byte
)
1218 edit_cursor_move (edit
, edit
->last_byte
- edit
->curs1
);
1219 edit_insert_ahead (edit
, '\n');
1223 if (edit_get_byte (edit
, p
) == '\n')
1229 edit_cursor_move (edit
, edit_move_forward3 (edit
, p
, col
, 0) - edit
->curs1
);
1230 l
= col
- edit_get_col (edit
);
1231 while (l
>= space_width
)
1233 edit_insert (edit
, ' ');
1240 *col2
= col
+ width
;
1241 *start_pos
= cursor
;
1242 *end_pos
= edit
->curs1
;
1243 edit_cursor_move (edit
, cursor
- edit
->curs1
);
1246 /* --------------------------------------------------------------------------------------------- */
1249 edit_macro_comparator (gconstpointer
* macro1
, gconstpointer
* macro2
)
1251 const macros_t
*m1
= (const macros_t
*) macro1
;
1252 const macros_t
*m2
= (const macros_t
*) macro2
;
1254 return m1
->hotkey
- m2
->hotkey
;
1257 /* --------------------------------------------------------------------------------------------- */
1260 edit_macro_sort_by_hotkey (void)
1262 if (macros_list
!= NULL
&& macros_list
->len
!= 0)
1263 g_array_sort (macros_list
, (GCompareFunc
) edit_macro_comparator
);
1266 /* --------------------------------------------------------------------------------------------- */
1269 edit_get_macro (WEdit
* edit
, int hotkey
, const macros_t
** macros
, guint
* indx
)
1271 const macros_t
*array_start
= &g_array_index (macros_list
, struct macros_t
, 0);
1273 macros_t search_macro
;
1277 search_macro
.hotkey
= hotkey
;
1278 result
= bsearch (&search_macro
, macros_list
->data
, macros_list
->len
,
1279 sizeof (macros_t
), (GCompareFunc
) edit_macro_comparator
);
1281 if (result
!= NULL
&& result
->macro
!= NULL
)
1283 *indx
= (result
- array_start
);
1291 /* --------------------------------------------------------------------------------------------- */
1292 /** returns FALSE on error */
1295 edit_delete_macro (WEdit
* edit
, int hotkey
)
1297 mc_config_t
*macros_config
= NULL
;
1298 const char *section_name
= "editor";
1299 gchar
*macros_fname
;
1302 const macros_t
*macros
= NULL
;
1304 /* clear array of actions for current hotkey */
1305 while (edit_get_macro (edit
, hotkey
, ¯os
, &indx
))
1307 if (macros
->macro
!= NULL
)
1308 g_array_free (macros
->macro
, TRUE
);
1310 g_array_remove_index (macros_list
, indx
);
1311 edit_macro_sort_by_hotkey ();
1314 macros_fname
= mc_config_get_full_path (MC_MACRO_FILE
);
1315 macros_config
= mc_config_init (macros_fname
);
1316 g_free (macros_fname
);
1318 if (macros_config
== NULL
)
1321 skeyname
= lookup_key_by_code (hotkey
);
1322 while (mc_config_del_key (macros_config
, section_name
, skeyname
))
1325 mc_config_save_file (macros_config
, NULL
);
1326 mc_config_deinit (macros_config
);
1331 /* --------------------------------------------------------------------------------------------- */
1332 /*** public functions ****************************************************************************/
1333 /* --------------------------------------------------------------------------------------------- */
1336 edit_help_cmd (WEdit
* edit
)
1338 ev_help_t event_data
= { NULL
, "[Internal File Editor]" };
1339 mc_event_raise (MCEVENT_GROUP_CORE
, "help", &event_data
);
1341 edit
->force
|= REDRAW_COMPLETELY
;
1344 /* --------------------------------------------------------------------------------------------- */
1347 edit_refresh_cmd (WEdit
* edit
)
1352 edit_get_syntax_color (edit
, -1, &color
);
1353 tty_touch_screen ();
1360 #endif /* !HAVE_SLANG */
1364 /* --------------------------------------------------------------------------------------------- */
1367 menu_save_mode_cmd (void)
1370 const int DLG_X
= 38;
1371 const int DLG_Y
= 13;
1375 const char *str
[] = {
1378 N_("&Do backups with following extension:")
1381 QuickWidget widgets
[] = {
1383 QUICK_BUTTON (18, DLG_X
, DLG_Y
- 3, DLG_Y
, N_("&Cancel"), B_CANCEL
, NULL
),
1385 QUICK_BUTTON (6, DLG_X
, DLG_Y
- 3, DLG_Y
, N_("&OK"), B_ENTER
, NULL
),
1387 QUICK_CHECKBOX (4, DLG_X
, 8, DLG_Y
, N_("Check &POSIX new line"), &option_check_nl_at_eof
),
1389 QUICK_INPUT (8, DLG_X
, 6, DLG_Y
, option_backup_ext
, 9, 0, "edit-backup-ext", &str_result
),
1391 QUICK_RADIO (4, DLG_X
, 3, DLG_Y
, 3, str
, &option_save_mode
),
1395 QuickDialog dialog
= {
1396 DLG_X
, DLG_Y
, -1, -1, N_("Edit Save Mode"),
1397 "[Edit Save Mode]", widgets
, NULL
, FALSE
1402 size_t w0
, w1
, b_len
, w3
;
1404 assert (option_backup_ext
!= NULL
);
1406 /* OK/Cancel buttons */
1407 w0
= str_term_width1 (_(widgets
[0].u
.button
.text
)) + 3;
1408 w1
= str_term_width1 (_(widgets
[1].u
.button
.text
)) + 5; /* default button */
1409 b_len
= w0
+ w1
+ 3;
1411 maxlen
= max (b_len
, (size_t) str_term_width1 (_(dialog
.title
)) + 2);
1414 for (i
= 0; i
< 3; i
++)
1419 w3
= max (w3
, (size_t) str_term_width1 (str
[i
]));
1422 maxlen
= max (maxlen
, w3
+ 4);
1424 dialog
.xlen
= min ((size_t) COLS
, maxlen
+ 8);
1426 widgets
[3].u
.input
.len
= w3
;
1427 widgets
[1].relative_x
= (dialog
.xlen
- b_len
) / 2;
1428 widgets
[0].relative_x
= widgets
[1].relative_x
+ w0
+ 2;
1430 for (i
= 0; i
< sizeof (widgets
) / sizeof (widgets
[0]); i
++)
1431 widgets
[i
].x_divisions
= dialog
.xlen
;
1433 if (quick_dialog (&dialog
) != B_CANCEL
)
1435 g_free (option_backup_ext
);
1436 option_backup_ext
= str_result
;
1440 /* --------------------------------------------------------------------------------------------- */
1443 edit_set_filename (WEdit
* edit
, const char *name
)
1445 g_free (edit
->filename
);
1450 edit
->filename
= tilde_expand (name
);
1451 if (edit
->dir
== NULL
&& !g_path_is_absolute (name
))
1452 edit
->dir
= vfs_get_current_dir ();
1455 /* --------------------------------------------------------------------------------------------- */
1456 /* Here we want to warn the users of overwriting an existing file,
1457 but only if they have made a change to the filename */
1458 /* returns 1 on success */
1460 edit_save_as_cmd (WEdit
* edit
)
1462 /* This heads the 'Save As' dialog box */
1465 int different_filename
= 0;
1467 if (!edit_check_newline (edit
))
1470 exp
= edit_get_save_file_as (edit
);
1471 edit_push_undo_action (edit
, KEY_PRESS
+ edit
->start_display
);
1478 edit
->force
|= REDRAW_COMPLETELY
;
1484 if (strcmp (edit
->filename
, exp
))
1487 different_filename
= 1;
1488 file
= mc_open (exp
, O_RDONLY
| O_BINARY
);
1491 /* the file exists */
1493 /* Overwrite the current file or cancel the operation */
1494 if (edit_query_dialog2
1496 _("A file already exists with this name"), _("&Overwrite"), _("&Cancel")))
1498 edit
->force
|= REDRAW_COMPLETELY
;
1505 edit
->stat1
.st_mode
|= S_IWUSR
;
1507 save_lock
= lock_file (exp
);
1511 /* filenames equal, check if already locked */
1512 if (!edit
->locked
&& !edit
->delete_file
)
1513 save_lock
= lock_file (exp
);
1516 if (different_filename
)
1519 * Allow user to write into saved (under another name) file
1520 * even if original file had r/o user permissions.
1522 edit
->stat1
.st_mode
|= S_IWRITE
;
1525 rv
= edit_save_file (edit
, exp
);
1529 /* Succesful, so unlock both files */
1530 if (different_filename
)
1535 edit
->locked
= edit_unlock_file (edit
);
1539 if (edit
->locked
|| save_lock
)
1540 edit
->locked
= edit_unlock_file (edit
);
1543 edit_set_filename (edit
, exp
);
1544 if (edit
->lb
!= LB_ASIS
)
1545 edit_reload (edit
, exp
);
1548 edit
->delete_file
= 0;
1549 if (different_filename
)
1550 edit_load_syntax (edit
, NULL
, edit
->syntax_type
);
1551 edit
->force
|= REDRAW_COMPLETELY
;
1554 edit_error_dialog (_("Save as"), get_sys_error (_("Cannot save file")));
1557 /* Failed, so maintain modify (not save) lock */
1561 edit
->force
|= REDRAW_COMPLETELY
;
1566 edit
->force
|= REDRAW_COMPLETELY
;
1570 /* {{{ Macro stuff starts here */
1571 /* --------------------------------------------------------------------------------------------- */
1574 edit_delete_macro_cmd (WEdit
* edit
)
1578 hotkey
= editcmd_dialog_raw_key_query (_("Delete macro"), _("Press macro hotkey:"), 1);
1580 if (hotkey
!= 0 && !edit_delete_macro (edit
, hotkey
))
1581 message (D_ERROR
, _("Delete macro"), _("Macro not deleted"));
1584 /* --------------------------------------------------------------------------------------------- */
1586 /** returns FALSE on error */
1588 edit_execute_macro (WEdit
* edit
, int hotkey
)
1590 gboolean res
= FALSE
;
1594 const macros_t
*macros
;
1597 if (edit_get_macro (edit
, hotkey
, ¯os
, &indx
) &&
1598 macros
->macro
!= NULL
&& macros
->macro
->len
!= 0)
1602 edit
->force
|= REDRAW_PAGE
;
1604 for (i
= 0; i
< macros
->macro
->len
; i
++)
1606 const macro_action_t
*m_act
;
1608 m_act
= &g_array_index (macros
->macro
, struct macro_action_t
, i
);
1609 edit_execute_cmd (edit
, m_act
->action
, m_act
->ch
);
1614 edit_update_screen (edit
);
1618 /* --------------------------------------------------------------------------------------------- */
1620 /** returns FALSE on error */
1622 edit_store_macro_cmd (WEdit
* edit
)
1626 GString
*marcros_string
;
1627 mc_config_t
*macros_config
= NULL
;
1628 const char *section_name
= "editor";
1629 gchar
*macros_fname
;
1630 GArray
*macros
; /* current macro */
1632 gboolean have_macro
= FALSE
;
1633 char *skeyname
= NULL
;
1635 hotkey
= editcmd_dialog_raw_key_query (_("Save macro"), _("Press the macro's new hotkey:"), 1);
1636 if (hotkey
== ESC_CHAR
)
1639 tmp_act
= keybind_lookup_keymap_command (editor_map
, hotkey
);
1641 /* return FALSE if try assign macro into restricted hotkeys */
1642 if (tmp_act
== CK_MacroStartRecord
1643 || tmp_act
== CK_MacroStopRecord
|| tmp_act
== CK_MacroStartStopRecord
)
1646 edit_delete_macro (edit
, hotkey
);
1648 macros_fname
= mc_config_get_full_path (MC_MACRO_FILE
);
1649 macros_config
= mc_config_init (macros_fname
);
1650 g_free (macros_fname
);
1652 if (macros_config
== NULL
)
1655 edit_push_undo_action (edit
, KEY_PRESS
+ edit
->start_display
);
1657 marcros_string
= g_string_sized_new (250);
1658 macros
= g_array_new (TRUE
, FALSE
, sizeof (macro_action_t
));
1660 skeyname
= lookup_key_by_code (hotkey
);
1662 for (i
= 0; i
< macro_index
; i
++)
1664 macro_action_t m_act
;
1665 const char *action_name
;
1667 action_name
= keybind_lookup_actionname (record_macro_buf
[i
].action
);
1669 if (action_name
== NULL
)
1672 m_act
.action
= record_macro_buf
[i
].action
;
1673 m_act
.ch
= record_macro_buf
[i
].ch
;
1674 g_array_append_val (macros
, m_act
);
1676 g_string_append_printf (marcros_string
, "%s:%i;", action_name
,
1677 (int) record_macro_buf
[i
].ch
);
1682 macro
.hotkey
= hotkey
;
1683 macro
.macro
= macros
;
1684 g_array_append_val (macros_list
, macro
);
1685 mc_config_set_string (macros_config
, section_name
, skeyname
, marcros_string
->str
);
1688 mc_config_del_key (macros_config
, section_name
, skeyname
);
1691 edit_macro_sort_by_hotkey ();
1693 g_string_free (marcros_string
, TRUE
);
1694 mc_config_save_file (macros_config
, NULL
);
1695 mc_config_deinit (macros_config
);
1699 /* --------------------------------------------------------------------------------------------- */
1702 edit_repeat_macro_cmd (WEdit
* edit
)
1709 f
= input_dialog (_("Repeat last commands"), _("Repeat times:"), MC_HISTORY_EDIT_REPEAT
, NULL
);
1710 if (f
== NULL
|| *f
== '\0')
1716 count_repeat
= strtol (f
, &error
, 0);
1726 edit_push_undo_action (edit
, KEY_PRESS
+ edit
->start_display
);
1727 edit
->force
|= REDRAW_PAGE
;
1729 for (j
= 0; j
< count_repeat
; j
++)
1730 for (i
= 0; i
< macro_index
; i
++)
1731 edit_execute_cmd (edit
, record_macro_buf
[i
].action
, record_macro_buf
[i
].ch
);
1732 edit_update_screen (edit
);
1736 /* --------------------------------------------------------------------------------------------- */
1737 /** return FALSE on error */
1740 edit_load_macro_cmd (WEdit
* edit
)
1742 mc_config_t
*macros_config
= NULL
;
1743 gchar
**profile_keys
, **keys
;
1744 gchar
**values
, **curr_values
;
1745 gsize len
, values_len
;
1746 const char *section_name
= "editor";
1747 gchar
*macros_fname
;
1752 macros_fname
= mc_config_get_full_path (MC_MACRO_FILE
);
1753 macros_config
= mc_config_init (macros_fname
);
1754 g_free (macros_fname
);
1756 if (macros_config
== NULL
)
1759 profile_keys
= keys
= mc_config_get_keys (macros_config
, section_name
, &len
);
1760 while (*profile_keys
!= NULL
)
1762 gboolean have_macro
;
1766 macros
= g_array_new (TRUE
, FALSE
, sizeof (macro_action_t
));
1768 curr_values
= values
= mc_config_get_string_list (macros_config
, section_name
,
1769 *profile_keys
, &values_len
);
1770 hotkey
= lookup_key (*profile_keys
, NULL
);
1773 while (*curr_values
!= NULL
&& *curr_values
[0] != '\0')
1775 char **macro_pair
= NULL
;
1777 macro_pair
= g_strsplit (*curr_values
, ":", 2);
1779 if (macro_pair
!= NULL
)
1781 macro_action_t m_act
;
1782 if (macro_pair
[0] == NULL
|| macro_pair
[0][0] == '\0')
1786 m_act
.action
= keybind_lookup_action (macro_pair
[0]);
1787 g_free (macro_pair
[0]);
1788 macro_pair
[0] = NULL
;
1790 if (macro_pair
[1] == NULL
|| macro_pair
[1][0] == '\0')
1794 m_act
.ch
= strtol (macro_pair
[1], NULL
, 0);
1795 g_free (macro_pair
[1]);
1796 macro_pair
[1] = NULL
;
1798 if (m_act
.action
!= 0)
1800 /* a shell command */
1801 if ((m_act
.action
/ CK_PipeBlock (0)) == 1)
1803 m_act
.action
= CK_PipeBlock (0) + (m_act
.ch
> 0 ? m_act
.ch
: 0);
1806 g_array_append_val (macros
, m_act
);
1809 g_strfreev (macro_pair
);
1816 macro
.hotkey
= hotkey
;
1817 macro
.macro
= macros
;
1818 g_array_append_val (macros_list
, macro
);
1821 g_strfreev (values
);
1824 mc_config_deinit (macros_config
);
1825 edit_macro_sort_by_hotkey ();
1829 /* }}} Macro stuff end here */
1831 /* --------------------------------------------------------------------------------------------- */
1832 /** returns 1 on success */
1835 edit_save_confirm_cmd (WEdit
* edit
)
1839 if (!edit_check_newline (edit
))
1842 if (edit_confirm_save
)
1844 f
= g_strdup_printf (_("Confirm save file: \"%s\""), edit
->filename
);
1845 if (edit_query_dialog2 (_("Save file"), f
, _("&Save"), _("&Cancel")))
1852 return edit_save_cmd (edit
);
1855 /* --------------------------------------------------------------------------------------------- */
1856 /** returns 1 on success */
1859 edit_new_cmd (WEdit
* edit
)
1863 if (edit_query_dialog2
1866 ("Current text was modified without a file save.\nContinue discards these changes"),
1867 _("C&ontinue"), _("&Cancel")))
1869 edit
->force
|= REDRAW_COMPLETELY
;
1873 edit
->force
|= REDRAW_COMPLETELY
;
1875 return edit_renew (edit
); /* if this gives an error, something has really screwed up */
1878 /* --------------------------------------------------------------------------------------------- */
1881 edit_load_cmd (WEdit
* edit
, edit_current_file_t what
)
1886 && (edit_query_dialog2
1888 _("Current text was modified without a file save.\n"
1889 "Continue discards these changes"), _("C&ontinue"), _("&Cancel")) == 1))
1891 edit
->force
|= REDRAW_COMPLETELY
;
1897 case EDIT_FILE_COMMON
:
1898 exp
= input_expand_dialog (_("Load"), _("Enter file name:"),
1899 MC_HISTORY_EDIT_LOAD
, edit
->filename
);
1904 edit_load_file_from_filename (edit
, exp
);
1909 case EDIT_FILE_SYNTAX
:
1910 edit_load_syntax_file (edit
);
1913 case EDIT_FILE_MENU
:
1914 edit_load_menu_file (edit
);
1921 edit
->force
|= REDRAW_COMPLETELY
;
1925 /* --------------------------------------------------------------------------------------------- */
1927 if mark2 is -1 then marking is from mark1 to the cursor.
1928 Otherwise its between the markers. This handles this.
1929 Returns 1 if no text is marked.
1933 eval_marks (WEdit
* edit
, long *start_mark
, long *end_mark
)
1935 if (edit
->mark1
!= edit
->mark2
)
1937 long start_bol
, start_eol
;
1938 long end_bol
, end_eol
;
1943 if (edit
->end_mark_curs
< 0)
1944 end_mark_curs
= edit
->curs1
;
1946 end_mark_curs
= edit
->end_mark_curs
;
1948 if (edit
->mark2
>= 0)
1950 *start_mark
= min (edit
->mark1
, edit
->mark2
);
1951 *end_mark
= max (edit
->mark1
, edit
->mark2
);
1955 *start_mark
= min (edit
->mark1
, end_mark_curs
);
1956 *end_mark
= max (edit
->mark1
, end_mark_curs
);
1957 edit
->column2
= edit
->curs_col
+ edit
->over_col
;
1960 if (edit
->column_highlight
1961 && (((edit
->mark1
> end_mark_curs
) && (edit
->column1
< edit
->column2
))
1962 || ((edit
->mark1
< end_mark_curs
) && (edit
->column1
> edit
->column2
))))
1964 start_bol
= edit_bol (edit
, *start_mark
);
1965 start_eol
= edit_eol (edit
, start_bol
- 1) + 1;
1966 end_bol
= edit_bol (edit
, *end_mark
);
1967 end_eol
= edit_eol (edit
, *end_mark
);
1968 col1
= min (edit
->column1
, edit
->column2
);
1969 col2
= max (edit
->column1
, edit
->column2
);
1972 edit_move_forward3 (edit
, start_bol
, col2
, 0) - edit_move_forward3 (edit
, start_bol
,
1975 edit_move_forward3 (edit
, end_bol
, col2
, 0) - edit_move_forward3 (edit
, end_bol
,
1978 *start_mark
-= diff1
;
1980 *start_mark
= max (*start_mark
, start_eol
);
1981 *end_mark
= min (*end_mark
, end_eol
);
1987 *start_mark
= *end_mark
= 0;
1988 edit
->column2
= edit
->column1
= 0;
1993 /* --------------------------------------------------------------------------------------------- */
1996 edit_insert_over (WEdit
* edit
)
2000 for (i
= 0; i
< edit
->over_col
; i
++)
2002 edit_insert (edit
, ' ');
2007 /* --------------------------------------------------------------------------------------------- */
2010 edit_insert_column_of_text_from_file (WEdit
* edit
, int file
,
2011 long *start_pos
, long *end_pos
, int *col1
, int *col2
)
2015 int blocklen
= -1, width
= 0;
2016 unsigned char *data
;
2018 cursor
= edit
->curs1
;
2019 col
= edit_get_col (edit
);
2020 data
= g_malloc0 (TEMP_BUF_LEN
);
2022 while ((blocklen
= mc_read (file
, (char *) data
, TEMP_BUF_LEN
)) > 0)
2025 for (width
= 0; width
< blocklen
; width
++)
2027 if (data
[width
] == '\n')
2030 for (i
= 0; i
< blocklen
; i
++)
2032 if (data
[i
] == '\n')
2033 { /* fill in and move to next line */
2036 if (edit_get_byte (edit
, edit
->curs1
) != '\n')
2038 l
= width
- (edit_get_col (edit
) - col
);
2041 edit_insert (edit
, ' ');
2045 for (p
= edit
->curs1
;; p
++)
2047 if (p
== edit
->last_byte
)
2049 edit_cursor_move (edit
, edit
->last_byte
- edit
->curs1
);
2050 edit_insert_ahead (edit
, '\n');
2054 if (edit_get_byte (edit
, p
) == '\n')
2060 edit_cursor_move (edit
, edit_move_forward3 (edit
, p
, col
, 0) - edit
->curs1
);
2061 l
= col
- edit_get_col (edit
);
2062 while (l
>= space_width
)
2064 edit_insert (edit
, ' ');
2069 edit_insert (edit
, data
[i
]);
2073 *col2
= col
+ width
;
2074 *start_pos
= cursor
;
2075 *end_pos
= edit
->curs1
;
2076 edit_cursor_move (edit
, cursor
- edit
->curs1
);
2082 /* --------------------------------------------------------------------------------------------- */
2085 edit_block_copy_cmd (WEdit
* edit
)
2087 long start_mark
, end_mark
, current
= edit
->curs1
;
2092 unsigned char *copy_buf
;
2094 edit_update_curs_col (edit
);
2095 if (eval_marks (edit
, &start_mark
, &end_mark
))
2098 copy_buf
= edit_get_block (edit
, start_mark
, end_mark
, &size
);
2100 /* all that gets pushed are deletes hence little space is used on the stack */
2102 edit_push_markers (edit
);
2104 if (edit
->column_highlight
)
2106 col_delta
= abs (edit
->column2
- edit
->column1
);
2107 edit_insert_column_of_text (edit
, copy_buf
, size
, col_delta
, &mark1
, &mark2
, &c1
, &c2
);
2112 edit_insert_ahead (edit
, copy_buf
[size
]);
2116 edit_scroll_screen_over_cursor (edit
);
2118 if (edit
->column_highlight
)
2119 edit_set_markers (edit
, edit
->curs1
, mark2
, c1
, c2
);
2120 else if (start_mark
< current
&& end_mark
> current
)
2121 edit_set_markers (edit
, start_mark
, end_mark
+ end_mark
- start_mark
, 0, 0);
2123 edit
->force
|= REDRAW_PAGE
;
2127 /* --------------------------------------------------------------------------------------------- */
2130 edit_block_move_cmd (WEdit
* edit
)
2133 unsigned char *copy_buf
= NULL
;
2134 long start_mark
, end_mark
;
2137 if (eval_marks (edit
, &start_mark
, &end_mark
))
2140 line
= edit
->curs_line
;
2141 if (edit
->mark2
< 0)
2142 edit_mark_cmd (edit
, 0);
2143 edit_push_markers (edit
);
2145 if (edit
->column_highlight
)
2153 c1
= min (edit
->column1
, edit
->column2
);
2154 c2
= max (edit
->column1
, edit
->column2
);
2155 b_width
= (c2
- c1
);
2157 edit_update_curs_col (edit
);
2160 x2
= x
+ edit
->over_col
;
2162 /* do nothing when cursor inside first line of selected area */
2163 if ((edit_eol (edit
, edit
->curs1
) == edit_eol (edit
, start_mark
)) && (x2
> c1
&& x2
<= c2
))
2166 if (edit
->curs1
> start_mark
&& edit
->curs1
< edit_eol (edit
, end_mark
))
2170 else if (x
> c1
&& x
<= c2
)
2173 /* save current selection into buffer */
2174 copy_buf
= edit_get_block (edit
, start_mark
, end_mark
, &size
);
2176 /* remove current selection */
2177 edit_block_delete_cmd (edit
);
2179 edit
->over_col
= max (0, edit
->over_col
- b_width
);
2180 /* calculate the cursor pos after delete block */
2181 current
= edit_move_forward3 (edit
, edit_bol (edit
, edit
->curs1
), x
, 0);
2182 edit_cursor_move (edit
, current
- edit
->curs1
);
2183 edit_scroll_screen_over_cursor (edit
);
2185 /* add TWS if need before block insertion */
2186 if (option_cursor_beyond_eol
&& edit
->over_col
> 0)
2187 edit_insert_over (edit
);
2189 edit_insert_column_of_text (edit
, copy_buf
, size
, b_width
, &mark1
, &mark2
, &c1
, &c2
);
2190 edit_set_markers (edit
, mark1
, mark2
, c1
, c2
);
2196 current
= edit
->curs1
;
2197 copy_buf
= g_malloc0 (end_mark
- start_mark
);
2198 edit_cursor_move (edit
, start_mark
- edit
->curs1
);
2199 edit_scroll_screen_over_cursor (edit
);
2201 while (count
< end_mark
)
2203 copy_buf
[end_mark
- count
- 1] = edit_delete (edit
, 1);
2206 edit_scroll_screen_over_cursor (edit
);
2207 edit_cursor_move (edit
,
2208 current
- edit
->curs1
-
2209 (((current
- edit
->curs1
) > 0) ? end_mark
- start_mark
: 0));
2210 edit_scroll_screen_over_cursor (edit
);
2211 while (count
-- > start_mark
)
2212 edit_insert_ahead (edit
, copy_buf
[end_mark
- count
- 1]);
2213 edit_set_markers (edit
, edit
->curs1
, edit
->curs1
+ end_mark
- start_mark
, 0, 0);
2216 edit_scroll_screen_over_cursor (edit
);
2218 edit
->force
|= REDRAW_PAGE
;
2221 /* --------------------------------------------------------------------------------------------- */
2222 /** returns 1 if canceelled by user */
2225 edit_block_delete_cmd (WEdit
* edit
)
2227 long start_mark
, end_mark
;
2228 if (eval_marks (edit
, &start_mark
, &end_mark
))
2230 edit_delete_line (edit
);
2233 return edit_block_delete (edit
);
2236 /* --------------------------------------------------------------------------------------------- */
2237 /** call with edit = 0 before shutdown to close memory leaks */
2240 edit_replace_cmd (WEdit
* edit
, int again
)
2242 /* 1 = search string, 2 = replace with */
2243 static char *saved1
= NULL
; /* saved default[123] */
2244 static char *saved2
= NULL
;
2245 char *input1
= NULL
; /* user input from the dialog */
2246 char *input2
= NULL
;
2249 long times_replaced
= 0;
2250 gboolean once_found
= FALSE
;
2254 g_free (saved1
), saved1
= NULL
;
2255 g_free (saved2
), saved2
= NULL
;
2259 edit
->force
|= REDRAW_COMPLETELY
;
2261 if (again
&& !saved1
&& !saved2
)
2266 input1
= g_strdup (saved1
? saved1
: "");
2267 input2
= g_strdup (saved2
? saved2
: "");
2271 char *tmp_inp1
, *tmp_inp2
;
2272 disp1
= edit_replace_cmd__conv_to_display (saved1
? saved1
: (char *) "");
2273 disp2
= edit_replace_cmd__conv_to_display (saved2
? saved2
: (char *) "");
2275 edit_push_undo_action (edit
, KEY_PRESS
+ edit
->start_display
);
2277 editcmd_dialog_replace_show (edit
, disp1
, disp2
, &input1
, &input2
);
2282 if (input1
== NULL
|| *input1
== '\0')
2284 edit
->force
= REDRAW_COMPLETELY
;
2290 input1
= edit_replace_cmd__conv_to_input (input1
);
2291 input2
= edit_replace_cmd__conv_to_input (input2
);
2295 g_free (saved1
), saved1
= g_strdup (input1
);
2296 g_free (saved2
), saved2
= g_strdup (input2
);
2298 mc_search_free (edit
->search
);
2299 edit
->search
= NULL
;
2304 edit
->search
= mc_search_new (input1
, -1);
2305 if (edit
->search
== NULL
)
2307 edit
->search_start
= edit
->curs1
;
2310 edit
->search
->search_type
= edit_search_options
.type
;
2311 edit
->search
->is_all_charsets
= edit_search_options
.all_codepages
;
2312 edit
->search
->is_case_sensitive
= edit_search_options
.case_sens
;
2313 edit
->search
->whole_words
= edit_search_options
.whole_words
;
2314 edit
->search
->search_fn
= edit_search_cmd_callback
;
2317 if (edit
->found_len
&& edit
->search_start
== edit
->found_start
+ 1
2318 && edit_search_options
.backwards
)
2319 edit
->search_start
--;
2321 if (edit
->found_len
&& edit
->search_start
== edit
->found_start
- 1
2322 && !edit_search_options
.backwards
)
2323 edit
->search_start
++;
2329 if (!editcmd_find (edit
, &len
))
2331 if (!(edit
->search
->error
== MC_SEARCH_E_OK
||
2332 (once_found
&& edit
->search
->error
== MC_SEARCH_E_NOTFOUND
)))
2334 edit_error_dialog (_("Search"), edit
->search
->error_str
);
2340 edit
->search_start
= edit
->search
->normal_offset
;
2341 /*returns negative on not found or error in pattern */
2343 if ((edit
->search_start
>= 0) && (edit
->search_start
< edit
->last_byte
))
2346 GString
*tmp_str
, *repl_str
;
2348 edit
->found_start
= edit
->search_start
;
2349 i
= edit
->found_len
= len
;
2351 edit_cursor_move (edit
, edit
->search_start
- edit
->curs1
);
2352 edit_scroll_screen_over_cursor (edit
);
2354 if (edit
->replace_mode
== 0)
2359 l
= edit
->curs_row
- edit
->widget
.lines
/ 3;
2361 edit_scroll_downward (edit
, l
);
2363 edit_scroll_upward (edit
, -l
);
2365 edit_scroll_screen_over_cursor (edit
);
2366 edit
->force
|= REDRAW_PAGE
;
2367 edit_render_keypress (edit
);
2369 /*so that undo stops at each query */
2370 edit_push_key_press (edit
);
2371 /* and prompt 2/3 down */
2372 disp1
= edit_replace_cmd__conv_to_display (saved1
);
2373 disp2
= edit_replace_cmd__conv_to_display (saved2
);
2374 prompt
= editcmd_dialog_replace_prompt_show (edit
, disp1
, disp2
, -1, -1);
2378 if (prompt
== B_REPLACE_ALL
)
2379 edit
->replace_mode
= 1;
2380 else if (prompt
== B_SKIP_REPLACE
)
2382 if (edit_search_options
.backwards
)
2383 edit
->search_start
--;
2385 edit
->search_start
++;
2386 continue; /* loop */
2388 else if (prompt
== B_CANCEL
)
2390 edit
->replace_mode
= -1;
2395 /* don't process string each time */
2396 tmp_str
= g_string_new (input2
);
2397 repl_str
= mc_search_prepare_replace_str (edit
->search
, tmp_str
);
2398 g_string_free (tmp_str
, TRUE
);
2400 if (edit
->search
->error
!= MC_SEARCH_E_OK
)
2402 edit_error_dialog (_("Replace"), edit
->search
->error_str
);
2403 g_string_free (repl_str
, TRUE
);
2407 /* delete then insert new */
2408 for (i
= 0; i
< len
; i
++)
2409 edit_delete (edit
, 1);
2411 for (i
= 0; i
< repl_str
->len
; i
++)
2412 edit_insert (edit
, repl_str
->str
[i
]);
2414 edit
->found_len
= repl_str
->len
;
2415 g_string_free (repl_str
, TRUE
);
2418 /* so that we don't find the same string again */
2419 if (edit_search_options
.backwards
)
2420 edit
->search_start
--;
2423 edit
->search_start
+= edit
->found_len
;
2425 if (edit
->search_start
>= edit
->last_byte
)
2429 edit_scroll_screen_over_cursor (edit
);
2433 /* try and find from right here for next search */
2434 edit
->search_start
= edit
->curs1
;
2435 edit_update_curs_col (edit
);
2437 edit
->force
|= REDRAW_PAGE
;
2438 edit_render_keypress (edit
);
2440 if (times_replaced
== 0)
2441 query_dialog (_("Replace"), _("Search string not found"), D_NORMAL
, 1, _("&OK"));
2445 while (edit
->replace_mode
>= 0);
2447 edit_scroll_screen_over_cursor (edit
);
2448 edit
->force
|= REDRAW_COMPLETELY
;
2449 edit_render_keypress (edit
);
2451 if ((edit
->replace_mode
== 1) && (times_replaced
!= 0))
2452 message (D_NORMAL
, _("Replace"), _("%ld replacements made"), times_replaced
);
2459 /* --------------------------------------------------------------------------------------------- */
2462 edit_search_cmd_callback (const void *user_data
, gsize char_offset
)
2464 return edit_get_byte ((WEdit
*) user_data
, (long) char_offset
);
2467 /* --------------------------------------------------------------------------------------------- */
2470 edit_search_cmd (WEdit
* edit
, gboolean again
)
2477 else if (edit
->last_search_string
!= NULL
)
2478 edit_do_search (edit
);
2481 /* find last search string in history */
2484 history
= history_get (MC_HISTORY_SHARED_SEARCH
);
2485 if (history
!= NULL
&& history
->data
!= NULL
)
2487 edit
->last_search_string
= (char *) history
->data
;
2488 history
->data
= NULL
;
2489 history
= g_list_first (history
);
2490 g_list_foreach (history
, (GFunc
) g_free
, NULL
);
2491 g_list_free (history
);
2493 edit
->search
= mc_search_new (edit
->last_search_string
, -1);
2494 if (edit
->search
== NULL
)
2496 /* if not... then ask for an expression */
2497 g_free (edit
->last_search_string
);
2498 edit
->last_search_string
= NULL
;
2503 edit
->search
->search_type
= edit_search_options
.type
;
2504 edit
->search
->is_all_charsets
= edit_search_options
.all_codepages
;
2505 edit
->search
->is_case_sensitive
= edit_search_options
.case_sens
;
2506 edit
->search
->whole_words
= edit_search_options
.whole_words
;
2507 edit
->search
->search_fn
= edit_search_cmd_callback
;
2508 edit_do_search (edit
);
2513 /* if not... then ask for an expression */
2514 g_free (edit
->last_search_string
);
2515 edit
->last_search_string
= NULL
;
2522 /* --------------------------------------------------------------------------------------------- */
2524 * Check if it's OK to close the editor. If there are unsaved changes,
2525 * ask user. Return 1 if it's OK to exit, 0 to continue editing.
2529 edit_ok_to_exit (WEdit
* edit
)
2533 if (!edit
->modified
)
2536 if (!mc_global
.widget
.midnight_shutdown
)
2538 if (!edit_check_newline (edit
))
2542 act
= edit_query_dialog3 (_("Quit"), _("File was modified. Save with exit?"),
2543 _("&Yes"), _("&No"), _("&Cancel quit"));
2548 edit_query_dialog2 (_("Quit"),
2549 _("Midnight Commander is being shut down.\nSave modified file?"),
2550 _("&Yes"), _("&No"));
2560 edit_push_markers (edit
);
2561 edit_set_markers (edit
, 0, 0, 0, 0);
2562 if (!edit_save_cmd (edit
) || mc_global
.widget
.midnight_shutdown
)
2563 return mc_global
.widget
.midnight_shutdown
;
2567 case 2: /* Cancel quit */
2575 /* --------------------------------------------------------------------------------------------- */
2576 /** save block, returns 1 on success */
2579 edit_save_block (WEdit
* edit
, const char *filename
, long start
, long finish
)
2583 file
= mc_open (filename
, O_CREAT
| O_WRONLY
| O_TRUNC
,
2584 S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IROTH
| O_BINARY
);
2588 if (edit
->column_highlight
)
2591 r
= mc_write (file
, VERTICAL_MAGIC
, sizeof (VERTICAL_MAGIC
));
2594 unsigned char *block
, *p
;
2595 p
= block
= edit_get_block (edit
, start
, finish
, &len
);
2598 r
= mc_write (file
, p
, len
);
2611 len
= finish
- start
;
2612 buf
= g_malloc0 (TEMP_BUF_LEN
);
2613 while (start
!= finish
)
2615 end
= min (finish
, start
+ TEMP_BUF_LEN
);
2616 for (; i
< end
; i
++)
2617 buf
[i
- start
] = edit_get_byte (edit
, i
);
2618 len
-= mc_write (file
, (char *) buf
, end
- start
);
2629 /* --------------------------------------------------------------------------------------------- */
2632 edit_paste_from_history (WEdit
* edit
)
2635 edit_error_dialog (_("Error"), _("This function is not implemented"));
2638 /* --------------------------------------------------------------------------------------------- */
2641 edit_copy_to_X_buf_cmd (WEdit
* edit
)
2643 long start_mark
, end_mark
;
2644 if (eval_marks (edit
, &start_mark
, &end_mark
))
2646 if (!edit_save_block_to_clip_file (edit
, start_mark
, end_mark
))
2648 edit_error_dialog (_("Copy to clipboard"), get_sys_error (_("Unable to save to file")));
2651 /* try use external clipboard utility */
2652 mc_event_raise (MCEVENT_GROUP_CORE
, "clipboard_file_to_ext_clip", NULL
);
2657 /* --------------------------------------------------------------------------------------------- */
2660 edit_cut_to_X_buf_cmd (WEdit
* edit
)
2662 long start_mark
, end_mark
;
2663 if (eval_marks (edit
, &start_mark
, &end_mark
))
2665 if (!edit_save_block_to_clip_file (edit
, start_mark
, end_mark
))
2667 edit_error_dialog (_("Cut to clipboard"), _("Unable to save to file"));
2670 /* try use external clipboard utility */
2671 mc_event_raise (MCEVENT_GROUP_CORE
, "clipboard_file_to_ext_clip", NULL
);
2673 edit_block_delete_cmd (edit
);
2674 edit_mark_cmd (edit
, 1);
2678 /* --------------------------------------------------------------------------------------------- */
2681 edit_paste_from_X_buf_cmd (WEdit
* edit
)
2684 /* try use external clipboard utility */
2685 mc_event_raise (MCEVENT_GROUP_CORE
, "clipboard_file_from_ext_clip", NULL
);
2686 tmp
= mc_config_get_full_path (EDIT_CLIP_FILE
);
2687 edit_insert_file (edit
, tmp
);
2692 /* --------------------------------------------------------------------------------------------- */
2694 * Ask user for the line and go to that line.
2695 * Negative numbers mean line from the end (i.e. -1 is the last line).
2699 edit_goto_cmd (WEdit
* edit
)
2702 static long line
= 0; /* line as typed, saved as default */
2707 g_snprintf (s
, sizeof (s
), "%ld", line
);
2708 f
= input_dialog (_("Goto line"), _("Enter line:"), MC_HISTORY_EDIT_GOTO_LINE
, line
? s
: "");
2718 l
= strtol (f
, &error
, 0);
2727 l
= edit
->total_lines
+ l
+ 2;
2728 edit_move_display (edit
, l
- edit
->widget
.lines
/ 2 - 1);
2729 edit_move_to_line (edit
, l
- 1);
2730 edit
->force
|= REDRAW_COMPLETELY
;
2735 /* --------------------------------------------------------------------------------------------- */
2736 /** Return 1 on success */
2739 edit_save_block_cmd (WEdit
* edit
)
2741 long start_mark
, end_mark
;
2744 if (eval_marks (edit
, &start_mark
, &end_mark
))
2747 tmp
= mc_config_get_full_path (EDIT_CLIP_FILE
);
2749 input_expand_dialog (_("Save block"), _("Enter file name:"),
2750 MC_HISTORY_EDIT_SAVE_BLOCK
, tmp
);
2752 edit_push_undo_action (edit
, KEY_PRESS
+ edit
->start_display
);
2762 if (edit_save_block (edit
, exp
, start_mark
, end_mark
))
2765 edit
->force
|= REDRAW_COMPLETELY
;
2771 edit_error_dialog (_("Save block"), get_sys_error (_("Cannot save file")));
2775 edit
->force
|= REDRAW_COMPLETELY
;
2780 /* --------------------------------------------------------------------------------------------- */
2781 /** returns TRUE on success */
2784 edit_insert_file_cmd (WEdit
* edit
)
2788 gboolean ret
= FALSE
;
2790 tmp
= mc_config_get_full_path (EDIT_CLIP_FILE
);
2791 exp
= input_expand_dialog (_("Insert file"), _("Enter file name:"),
2792 MC_HISTORY_EDIT_INSERT_FILE
, tmp
);
2795 edit_push_undo_action (edit
, KEY_PRESS
+ edit
->start_display
);
2797 if (exp
!= NULL
&& *exp
!= '\0')
2799 ret
= (edit_insert_file (edit
, exp
) >= 0);
2801 edit_error_dialog (_("Insert file"), get_sys_error (_("Cannot insert file")));
2806 edit
->force
|= REDRAW_COMPLETELY
;
2810 /* --------------------------------------------------------------------------------------------- */
2811 /** sorts a block, returns -1 on system fail, 1 on cancel and 0 on success */
2814 edit_sort_cmd (WEdit
* edit
)
2816 static char *old
= 0;
2817 char *exp
, *tmp
, *tmp_edit_block_name
, *tmp_edit_temp_name
;
2818 long start_mark
, end_mark
;
2821 if (eval_marks (edit
, &start_mark
, &end_mark
))
2823 edit_error_dialog (_("Sort block"), _("You must first highlight a block of text"));
2827 tmp
= mc_config_get_full_path (EDIT_BLOCK_FILE
);
2828 edit_save_block (edit
, tmp
, start_mark
, end_mark
);
2831 exp
= input_dialog (_("Run sort"),
2832 _("Enter sort options (see manpage) separated by whitespace:"),
2833 MC_HISTORY_EDIT_SORT
, (old
!= NULL
) ? old
: "");
2839 tmp_edit_block_name
= mc_config_get_full_path (EDIT_BLOCK_FILE
);
2840 tmp_edit_temp_name
= mc_config_get_full_path (EDIT_TEMP_FILE
);
2842 g_strconcat (" sort ", exp
, " ", tmp_edit_block_name
,
2843 " > ", tmp_edit_temp_name
, (char *) NULL
);
2844 g_free (tmp_edit_temp_name
);
2845 g_free (tmp_edit_block_name
);
2851 if (e
== -1 || e
== 127)
2853 edit_error_dialog (_("Sort"), get_sys_error (_("Cannot execute sort command")));
2858 sprintf (q
, "%d ", e
);
2859 tmp
= g_strdup_printf (_("Sort returned non-zero: %s"), q
);
2860 edit_error_dialog (_("Sort"), tmp
);
2866 edit
->force
|= REDRAW_COMPLETELY
;
2868 if (edit_block_delete_cmd (edit
))
2870 tmp
= mc_config_get_full_path (EDIT_TEMP_FILE
);
2871 edit_insert_file (edit
, tmp
);
2876 /* --------------------------------------------------------------------------------------------- */
2878 * Ask user for a command, execute it and paste its output back to the
2883 edit_ext_cmd (WEdit
* edit
)
2885 char *exp
, *tmp
, *tmp_edit_temp_file
;
2889 input_dialog (_("Paste output of external command"),
2890 _("Enter shell command(s):"), MC_HISTORY_EDIT_PASTE_EXTCMD
, NULL
);
2895 tmp_edit_temp_file
= mc_config_get_full_path (EDIT_TEMP_FILE
);
2896 tmp
= g_strconcat (exp
, " > ", tmp_edit_temp_file
, (char *) NULL
);
2897 g_free (tmp_edit_temp_file
);
2904 edit_error_dialog (_("External command"), get_sys_error (_("Cannot execute command")));
2908 edit
->force
|= REDRAW_COMPLETELY
;
2909 tmp
= mc_config_get_full_path (EDIT_TEMP_FILE
);
2910 edit_insert_file (edit
, tmp
);
2915 /* --------------------------------------------------------------------------------------------- */
2916 /** if block is 1, a block must be highlighted and the shell command
2917 processes it. If block is 0 the shell command is a straight system
2918 command, that just produces some output which is to be inserted */
2921 edit_block_process_cmd (WEdit
* edit
, int macro_number
)
2924 char *macros_fname
= NULL
;
2926 fname
= g_strdup_printf ("%s.%i.sh", MC_EXTMACRO_FILE
, macro_number
);
2927 macros_fname
= g_build_filename (mc_config_get_data_path (), fname
, (char *) NULL
);
2928 user_menu (edit
, macros_fname
, 0);
2930 g_free (macros_fname
);
2931 edit
->force
|= REDRAW_COMPLETELY
;
2934 /* --------------------------------------------------------------------------------------------- */
2937 edit_mail_dialog (WEdit
* edit
)
2940 char *tmail_subject
;
2943 static char *mail_cc_last
= 0;
2944 static char *mail_subject_last
= 0;
2945 static char *mail_to_last
= 0;
2947 QuickWidget quick_widgets
[] = {
2948 /* 0 */ QUICK_BUTTON (6, 10, 9, MAIL_DLG_HEIGHT
, N_("&Cancel"), B_CANCEL
, NULL
),
2949 /* 1 */ QUICK_BUTTON (2, 10, 9, MAIL_DLG_HEIGHT
, N_("&OK"), B_ENTER
, NULL
),
2950 /* 2 */ QUICK_INPUT (3, 50, 8, MAIL_DLG_HEIGHT
, "", 44, 0, "mail-dlg-input", &tmail_cc
),
2951 /* 3 */ QUICK_LABEL (3, 50, 7, MAIL_DLG_HEIGHT
, N_("Copies to")),
2952 /* 4 */ QUICK_INPUT (3, 50, 6, MAIL_DLG_HEIGHT
, "", 44, 0, "mail-dlg-input-2",
2954 /* 5 */ QUICK_LABEL (3, 50, 5, MAIL_DLG_HEIGHT
, N_("Subject")),
2955 /* 6 */ QUICK_INPUT (3, 50, 4, MAIL_DLG_HEIGHT
, "", 44, 0, "mail-dlg-input-3", &tmail_to
),
2956 /* 7 */ QUICK_LABEL (3, 50, 3, MAIL_DLG_HEIGHT
, N_("To")),
2957 /* 8 */ QUICK_LABEL (3, 50, 2, MAIL_DLG_HEIGHT
, N_("mail -s <subject> -c <cc> <to>")),
2961 QuickDialog Quick_input
= {
2962 50, MAIL_DLG_HEIGHT
, -1, -1, N_("Mail"),
2963 "[Input Line Keys]", quick_widgets
, NULL
, FALSE
2966 quick_widgets
[2].u
.input
.text
= mail_cc_last
? mail_cc_last
: "";
2967 quick_widgets
[4].u
.input
.text
= mail_subject_last
? mail_subject_last
: "";
2968 quick_widgets
[6].u
.input
.text
= mail_to_last
? mail_to_last
: "";
2970 if (quick_dialog (&Quick_input
) != B_CANCEL
)
2972 g_free (mail_cc_last
);
2973 g_free (mail_subject_last
);
2974 g_free (mail_to_last
);
2975 mail_cc_last
= tmail_cc
;
2976 mail_subject_last
= tmail_subject
;
2977 mail_to_last
= tmail_to
;
2978 pipe_mail (edit
, mail_to_last
, mail_subject_last
, mail_cc_last
);
2983 /*******************/
2984 /* Word Completion */
2985 /*******************/
2987 /* --------------------------------------------------------------------------------------------- */
2989 * Complete current word using regular expression search
2990 * backwards beginning at the current cursor position.
2994 edit_complete_word_cmd (WEdit
* edit
)
2996 gsize i
, max_len
, word_len
= 0, num_compl
= 0;
2997 long word_start
= 0;
2998 unsigned char *bufpos
;
3000 struct selection
compl[MAX_WORD_COMPLETIONS
]; /* completions */
3002 /* search start of word to be completed */
3003 if (!edit_find_word_start (edit
, &word_start
, &word_len
))
3006 /* prepare match expression */
3007 bufpos
= &edit
->buffers1
[word_start
>> S_EDIT_BUF_SIZE
][word_start
& M_EDIT_BUF_SIZE
];
3009 /* match_expr = g_strdup_printf ("\\b%.*s[a-zA-Z_0-9]+", word_len, bufpos); */
3012 ("(^|\\s+|\\b)%.*s[^\\s\\.=\\+\\[\\]\\(\\)\\,\\;\\:\\\"\\'\\-\\?\\/\\|\\\\\\{\\}\\*\\&\\^\\%%\\$#@\\!]+",
3013 (int) word_len
, bufpos
);
3015 /* collect the possible completions */
3016 /* start search from begin to end of file */
3018 edit_collect_completions (edit
, word_start
, word_len
, match_expr
,
3019 (struct selection
*) &compl, &num_compl
);
3023 /* insert completed word if there is only one match */
3026 for (i
= word_len
; i
< compl[0].len
; i
++)
3027 edit_insert (edit
, *(compl[0].text
+ i
));
3029 /* more than one possible completion => ask the user */
3032 /* !!! usually only a beep is expected and when <ALT-TAB> is !!! */
3033 /* !!! pressed again the selection dialog pops up, but that !!! */
3034 /* !!! seems to require a further internal state !!! */
3037 /* let the user select the preferred completion */
3038 editcmd_dialog_completion_show (edit
, max_len
, word_len
,
3039 (struct selection
*) &compl, num_compl
);
3043 g_free (match_expr
);
3044 /* release memory before return */
3045 for (i
= 0; i
< num_compl
; i
++)
3046 g_free (compl[i
].text
);
3049 /* --------------------------------------------------------------------------------------------- */
3052 edit_select_codepage_cmd (WEdit
* edit
)
3055 if (do_select_codepage ())
3056 edit_set_codeset (edit
);
3058 edit
->force
= REDRAW_COMPLETELY
;
3059 edit_refresh_cmd (edit
);
3065 /* --------------------------------------------------------------------------------------------- */
3068 edit_insert_literal_cmd (WEdit
* edit
)
3070 int char_for_insertion
= editcmd_dialog_raw_key_query (_("Insert literal"),
3071 _("Press any key:"), 0);
3072 edit_execute_key_command (edit
, -1, ascii_alpha_to_cntrl (char_for_insertion
));
3075 /* --------------------------------------------------------------------------------------------- */
3078 edit_begin_end_macro_cmd (WEdit
* edit
)
3080 /* edit is a pointer to the widget */
3083 unsigned long command
= macro_index
< 0 ? CK_MacroStartRecord
: CK_MacroStopRecord
;
3084 edit_execute_key_command (edit
, command
, -1);
3088 /* --------------------------------------------------------------------------------------------- */
3091 edit_begin_end_repeat_cmd (WEdit
* edit
)
3093 /* edit is a pointer to the widget */
3096 unsigned long command
= macro_index
< 0 ? CK_RepeatStartRecord
: CK_RepeatStopRecord
;
3097 edit_execute_key_command (edit
, command
, -1);
3101 /* --------------------------------------------------------------------------------------------- */
3104 edit_load_forward_cmd (WEdit
* edit
)
3108 if (edit_query_dialog2
3110 _("Current text was modified without a file save\n"
3111 "Continue discards these changes"), _("C&ontinue"), _("&Cancel")))
3113 edit
->force
|= REDRAW_COMPLETELY
;
3117 if (edit_stack_iterator
+ 1 < MAX_HISTORY_MOVETO
)
3119 if (edit_history_moveto
[edit_stack_iterator
+ 1].line
< 1)
3123 edit_stack_iterator
++;
3124 if (edit_history_moveto
[edit_stack_iterator
].filename
)
3126 edit_reload_line (edit
, edit_history_moveto
[edit_stack_iterator
].filename
,
3127 edit_history_moveto
[edit_stack_iterator
].line
);
3141 /* --------------------------------------------------------------------------------------------- */
3144 edit_load_back_cmd (WEdit
* edit
)
3148 if (edit_query_dialog2
3150 _("Current text was modified without a file save\n"
3151 "Continue discards these changes"), _("C&ontinue"), _("&Cancel")))
3153 edit
->force
|= REDRAW_COMPLETELY
;
3157 if (edit_stack_iterator
> 0)
3159 edit_stack_iterator
--;
3160 if (edit_history_moveto
[edit_stack_iterator
].filename
)
3162 edit_reload_line (edit
, edit_history_moveto
[edit_stack_iterator
].filename
,
3163 edit_history_moveto
[edit_stack_iterator
].line
);
3177 /* --------------------------------------------------------------------------------------------- */
3180 edit_get_match_keyword_cmd (WEdit
* edit
)
3182 gsize word_len
= 0, max_len
= 0;
3185 long word_start
= 0;
3186 unsigned char *bufpos
;
3190 char *tagfile
= NULL
;
3192 etags_hash_t def_hash
[MAX_DEFINITIONS
];
3194 for (i
= 0; i
< MAX_DEFINITIONS
; i
++)
3196 def_hash
[i
].filename
= NULL
;
3199 /* search start of word to be completed */
3200 if (!edit_find_word_start (edit
, &word_start
, &word_len
))
3203 /* prepare match expression */
3204 bufpos
= &edit
->buffers1
[word_start
>> S_EDIT_BUF_SIZE
][word_start
& M_EDIT_BUF_SIZE
];
3205 match_expr
= g_strdup_printf ("%.*s", (int) word_len
, bufpos
);
3207 ptr
= g_get_current_dir ();
3208 path
= g_strconcat (ptr
, G_DIR_SEPARATOR_S
, (char *) NULL
);
3211 /* Recursive search file 'TAGS' in parent dirs */
3214 ptr
= g_path_get_dirname (path
);
3218 tagfile
= mc_build_filename (path
, TAGS_NAME
, (char *) NULL
);
3219 if (exist_file (tagfile
))
3222 while (strcmp (path
, G_DIR_SEPARATOR_S
) != 0);
3227 etags_set_definition_hash (tagfile
, path
, match_expr
, (etags_hash_t
*) & def_hash
);
3232 max_len
= MAX_WIDTH_DEF_DIALOG
;
3236 editcmd_dialog_select_definition_show (edit
, match_expr
, max_len
, word_len
,
3237 (etags_hash_t
*) & def_hash
, num_def
);
3239 g_free (match_expr
);
3242 /* --------------------------------------------------------------------------------------------- */