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 /* --------------------------------------------------------------------------------------------- */
712 editcmd_find (WEdit
* edit
, gsize
* len
)
714 off_t search_start
= edit
->search_start
;
717 long end_mark
= edit
->last_byte
;
720 if (edit_search_options
.only_in_selection
)
722 mark_res
= eval_marks (edit
, &start_mark
, &end_mark
);
725 edit
->search
->error
= MC_SEARCH_E_NOTFOUND
;
726 edit
->search
->error_str
= g_strdup (_("Search string not found"));
729 if (edit_search_options
.backwards
)
731 if (search_start
> end_mark
|| search_start
<= start_mark
)
733 search_start
= end_mark
;
738 if (search_start
< start_mark
|| search_start
>= end_mark
)
740 search_start
= start_mark
;
746 if (edit_search_options
.backwards
)
747 end_mark
= max (1, edit
->curs1
) - 1;
749 if (edit_search_options
.backwards
)
751 search_end
= end_mark
;
752 while ((int) search_start
>= start_mark
)
754 if (search_end
> (off_t
) (search_start
+ edit
->search
->original_len
) &&
755 mc_search_is_fixed_search_str (edit
->search
))
757 search_end
= search_start
+ edit
->search
->original_len
;
759 if (mc_search_run (edit
->search
, (void *) edit
, search_start
, search_end
, len
)
760 && edit
->search
->normal_offset
== search_start
)
766 edit
->search
->error_str
= g_strdup (_("Search string not found"));
770 return mc_search_run (edit
->search
, (void *) edit
, search_start
, end_mark
, len
);
775 /* --------------------------------------------------------------------------------------------- */
778 edit_replace_cmd__conv_to_display (char *str
)
783 tmp
= str_convert_to_display (str
);
787 return g_string_free (tmp
, FALSE
);
788 g_string_free (tmp
, TRUE
);
791 return g_strdup (str
);
794 /* --------------------------------------------------------------------------------------------- */
797 edit_replace_cmd__conv_to_input (char *str
)
802 tmp
= str_convert_to_input (str
);
806 return g_string_free (tmp
, FALSE
);
807 g_string_free (tmp
, TRUE
);
810 return g_strdup (str
);
813 /* --------------------------------------------------------------------------------------------- */
816 edit_do_search (WEdit
* edit
)
820 if (edit
->search
== NULL
)
821 edit
->search_start
= edit
->curs1
;
823 edit_push_undo_action (edit
, KEY_PRESS
+ edit
->start_display
);
825 if (search_create_bookmark
)
827 int found
= 0, books
= 0;
828 long l
= 0, l_last
= -1;
831 search_create_bookmark
= FALSE
;
832 book_mark_flush (edit
, -1);
836 if (!mc_search_run (edit
->search
, (void *) edit
, q
, edit
->last_byte
, &len
))
839 edit
->search_start
= edit
->search
->normal_offset
;
841 l
+= edit_count_lines (edit
, q
, edit
->search
->normal_offset
);
844 book_mark_insert (edit
, l
, BOOK_MARK_FOUND_COLOR
);
848 q
= edit
->search
->normal_offset
+ 1;
852 edit_error_dialog (_("Search"), _("Search string not found"));
854 edit_cursor_move (edit
, edit
->search_start
- edit
->curs1
);
858 if (edit
->found_len
!= 0 && edit
->search_start
== edit
->found_start
+ 1
859 && edit_search_options
.backwards
)
860 edit
->search_start
--;
862 if (edit
->found_len
!= 0 && edit
->search_start
== edit
->found_start
- 1
863 && !edit_search_options
.backwards
)
864 edit
->search_start
++;
866 if (editcmd_find (edit
, &len
))
868 edit
->found_start
= edit
->search_start
= edit
->search
->normal_offset
;
869 edit
->found_len
= len
;
871 edit_cursor_move (edit
, edit
->search_start
- edit
->curs1
);
872 edit_scroll_screen_over_cursor (edit
);
873 if (edit_search_options
.backwards
)
874 edit
->search_start
--;
876 edit
->search_start
++;
880 edit
->search_start
= edit
->curs1
;
881 if (edit
->search
->error_str
!= NULL
)
882 edit_error_dialog (_("Search"), edit
->search
->error_str
);
886 edit
->force
|= REDRAW_COMPLETELY
;
887 edit_scroll_screen_over_cursor (edit
);
890 /* --------------------------------------------------------------------------------------------- */
893 edit_search (WEdit
* edit
)
895 if (editcmd_dialog_search_show (edit
))
896 edit_do_search (edit
);
899 /* --------------------------------------------------------------------------------------------- */
900 /** Return a null terminated length of text. Result must be g_free'd */
902 static unsigned char *
903 edit_get_block (WEdit
* edit
, long start
, long finish
, int *l
)
905 unsigned char *s
, *r
;
906 r
= s
= g_malloc0 (finish
- start
+ 1);
907 if (edit
->column_highlight
)
910 /* copy from buffer, excluding chars that are out of the column 'margins' */
911 while (start
< finish
)
915 x
= edit_move_forward3 (edit
, edit_bol (edit
, start
), 0, start
);
916 c
= edit_get_byte (edit
, start
);
917 if ((x
>= edit
->column1
&& x
< edit
->column2
)
918 || (x
>= edit
->column2
&& x
< edit
->column1
) || c
== '\n')
929 while (start
< finish
)
930 *s
++ = edit_get_byte (edit
, start
++);
936 /* --------------------------------------------------------------------------------------------- */
937 /** copies a block to clipboard file */
940 edit_save_block_to_clip_file (WEdit
* edit
, long start
, long finish
)
944 tmp
= mc_config_get_full_path (EDIT_CLIP_FILE
);
945 ret
= edit_save_block (edit
, tmp
, start
, finish
);
950 /* --------------------------------------------------------------------------------------------- */
953 pipe_mail (WEdit
* edit
, char *to
, char *subject
, char *cc
)
958 to
= name_quote (to
, 0);
959 subject
= name_quote (subject
, 0);
960 cc
= name_quote (cc
, 0);
961 s
= g_strconcat ("mail -s ", subject
, *cc
? " -c " : "", cc
, " ", to
, (char *) NULL
);
975 for (i
= 0; i
< edit
->last_byte
; i
++)
976 fputc (edit_get_byte (edit
, i
), p
);
981 /* --------------------------------------------------------------------------------------------- */
984 is_break_char (char c
)
986 return (isspace (c
) || strchr ("{}[]()<>=|/\\!?~-+`'\",.;:#$%^&*", c
));
989 /* --------------------------------------------------------------------------------------------- */
990 /** find first character of current word */
993 edit_find_word_start (WEdit
* edit
, long *word_start
, gsize
* word_len
)
998 /* return if at begin of file */
999 if (edit
->curs1
<= 0)
1002 c
= (unsigned char) edit_get_byte (edit
, edit
->curs1
- 1);
1003 /* return if not at end or in word */
1004 if (is_break_char (c
))
1007 /* search start of word to be completed */
1010 /* return if at begin of file */
1011 if ((gsize
) edit
->curs1
< i
)
1015 c
= (unsigned char) edit_get_byte (edit
, edit
->curs1
- i
);
1017 if (is_break_char (c
))
1019 /* return if word starts with digit */
1023 *word_start
= edit
->curs1
- (i
- 1); /* start found */
1032 /* --------------------------------------------------------------------------------------------- */
1034 * Get current word under cursor
1036 * @param edit editor object
1037 * @param srch mc_search object
1038 * @param word_start start word position
1040 * @return newly allocated string or NULL if no any words under cursor
1044 edit_collect_completions_get_current_word (WEdit
* edit
, mc_search_t
* srch
, long word_start
)
1049 if (!mc_search_run (srch
, (void *) edit
, word_start
, edit
->last_byte
, &len
))
1052 temp
= g_string_sized_new (len
);
1054 for (i
= 0; i
< len
; i
++)
1058 chr
= edit_get_byte (edit
, word_start
+ i
);
1060 g_string_append_c (temp
, chr
);
1063 return g_string_free (temp
, temp
->len
== 0);
1066 /* --------------------------------------------------------------------------------------------- */
1067 /** collect the possible completions */
1070 edit_collect_completions (WEdit
* edit
, long word_start
, gsize word_len
,
1071 char *match_expr
, struct selection
*compl, gsize
* num
)
1079 long last_byte
, start
= -1;
1082 srch
= mc_search_new (match_expr
, -1);
1086 if (mc_config_get_bool
1087 (mc_main_config
, CONFIG_APP_SECTION
, "editor_wordcompletion_collect_entire_file", 0))
1089 last_byte
= edit
->last_byte
;
1093 last_byte
= word_start
;
1096 srch
->search_type
= MC_SEARCH_T_REGEX
;
1097 srch
->is_case_sensitive
= TRUE
;
1098 srch
->search_fn
= edit_search_cmd_callback
;
1100 current_word
= edit_collect_completions_get_current_word (edit
, srch
, word_start
);
1102 temp
= g_string_new ("");
1104 /* collect max MAX_WORD_COMPLETIONS completions */
1105 while (mc_search_run (srch
, (void *) edit
, start
+ 1, last_byte
, &len
))
1107 g_string_set_size (temp
, 0);
1108 start
= srch
->normal_offset
;
1110 /* add matched completion if not yet added */
1111 for (i
= 0; i
< len
; i
++)
1113 skip
= edit_get_byte (edit
, start
+ i
);
1117 /* skip current word */
1118 if (start
+ (long) i
== word_start
)
1121 g_string_append_c (temp
, skip
);
1127 if (current_word
!= NULL
&& strcmp (current_word
, temp
->str
) == 0)
1132 for (i
= 0; i
< *num
; i
++)
1135 ((char *) &compl[i
].text
[word_len
],
1136 (char *) &temp
->str
[word_len
], max (len
, compl[i
].len
) - word_len
) == 0)
1138 struct selection
this = compl[i
];
1139 for (++i
; i
< *num
; i
++)
1141 compl[i
- 1] = compl[i
];
1143 compl[*num
- 1] = this;
1145 break; /* skip it, already added */
1151 if (*num
== MAX_WORD_COMPLETIONS
)
1153 g_free (compl[0].text
);
1154 for (i
= 1; i
< *num
; i
++)
1156 compl[i
- 1] = compl[i
];
1163 recoded
= str_convert_to_display (temp
->str
);
1165 if (recoded
&& recoded
->len
)
1166 g_string_assign (temp
, recoded
->str
);
1168 g_string_free (recoded
, TRUE
);
1171 compl[*num
].text
= g_strdup (temp
->str
);
1172 compl[*num
].len
= temp
->len
;
1176 /* note the maximal length needed for the completion dialog */
1181 mc_search_free (srch
);
1182 g_string_free (temp
, TRUE
);
1183 g_free (current_word
);
1188 /* --------------------------------------------------------------------------------------------- */
1191 edit_insert_column_of_text (WEdit
* edit
, unsigned char *data
, int size
, int width
,
1192 long *start_pos
, long *end_pos
, int *col1
, int *col2
)
1197 cursor
= edit
->curs1
;
1198 col
= edit_get_col (edit
);
1200 for (i
= 0; i
< size
; i
++)
1202 if (data
[i
] != '\n')
1203 edit_insert (edit
, data
[i
]);
1205 { /* fill in and move to next line */
1209 if (edit_get_byte (edit
, edit
->curs1
) != '\n')
1211 l
= width
- (edit_get_col (edit
) - col
);
1214 edit_insert (edit
, ' ');
1218 for (p
= edit
->curs1
;; p
++)
1220 if (p
== edit
->last_byte
)
1222 edit_cursor_move (edit
, edit
->last_byte
- edit
->curs1
);
1223 edit_insert_ahead (edit
, '\n');
1227 if (edit_get_byte (edit
, p
) == '\n')
1233 edit_cursor_move (edit
, edit_move_forward3 (edit
, p
, col
, 0) - edit
->curs1
);
1234 l
= col
- edit_get_col (edit
);
1235 while (l
>= space_width
)
1237 edit_insert (edit
, ' ');
1244 *col2
= col
+ width
;
1245 *start_pos
= cursor
;
1246 *end_pos
= edit
->curs1
;
1247 edit_cursor_move (edit
, cursor
- edit
->curs1
);
1250 /* --------------------------------------------------------------------------------------------- */
1253 edit_macro_comparator (gconstpointer
* macro1
, gconstpointer
* macro2
)
1255 const macros_t
*m1
= (const macros_t
*) macro1
;
1256 const macros_t
*m2
= (const macros_t
*) macro2
;
1258 return m1
->hotkey
- m2
->hotkey
;
1261 /* --------------------------------------------------------------------------------------------- */
1264 edit_macro_sort_by_hotkey (void)
1266 if (macros_list
!= NULL
&& macros_list
->len
!= 0)
1267 g_array_sort (macros_list
, (GCompareFunc
) edit_macro_comparator
);
1270 /* --------------------------------------------------------------------------------------------- */
1273 edit_get_macro (WEdit
* edit
, int hotkey
, const macros_t
** macros
, guint
* indx
)
1275 const macros_t
*array_start
= &g_array_index (macros_list
, struct macros_t
, 0);
1277 macros_t search_macro
;
1281 search_macro
.hotkey
= hotkey
;
1282 result
= bsearch (&search_macro
, macros_list
->data
, macros_list
->len
,
1283 sizeof (macros_t
), (GCompareFunc
) edit_macro_comparator
);
1285 if (result
!= NULL
&& result
->macro
!= NULL
)
1287 *indx
= (result
- array_start
);
1295 /* --------------------------------------------------------------------------------------------- */
1296 /** returns FALSE on error */
1299 edit_delete_macro (WEdit
* edit
, int hotkey
)
1301 mc_config_t
*macros_config
= NULL
;
1302 const char *section_name
= "editor";
1303 gchar
*macros_fname
;
1306 const macros_t
*macros
= NULL
;
1308 /* clear array of actions for current hotkey */
1309 while (edit_get_macro (edit
, hotkey
, ¯os
, &indx
))
1311 if (macros
->macro
!= NULL
)
1312 g_array_free (macros
->macro
, TRUE
);
1314 g_array_remove_index (macros_list
, indx
);
1315 edit_macro_sort_by_hotkey ();
1318 macros_fname
= mc_config_get_full_path (MC_MACRO_FILE
);
1319 macros_config
= mc_config_init (macros_fname
);
1320 g_free (macros_fname
);
1322 if (macros_config
== NULL
)
1325 skeyname
= lookup_key_by_code (hotkey
);
1326 while (mc_config_del_key (macros_config
, section_name
, skeyname
))
1329 mc_config_save_file (macros_config
, NULL
);
1330 mc_config_deinit (macros_config
);
1335 /* --------------------------------------------------------------------------------------------- */
1336 /*** public functions ****************************************************************************/
1337 /* --------------------------------------------------------------------------------------------- */
1340 edit_help_cmd (WEdit
* edit
)
1342 ev_help_t event_data
= { NULL
, "[Internal File Editor]" };
1343 mc_event_raise (MCEVENT_GROUP_CORE
, "help", &event_data
);
1345 edit
->force
|= REDRAW_COMPLETELY
;
1348 /* --------------------------------------------------------------------------------------------- */
1351 edit_refresh_cmd (WEdit
* edit
)
1356 edit_get_syntax_color (edit
, -1, &color
);
1357 tty_touch_screen ();
1364 #endif /* !HAVE_SLANG */
1368 /* --------------------------------------------------------------------------------------------- */
1371 menu_save_mode_cmd (void)
1374 const int DLG_X
= 38;
1375 const int DLG_Y
= 13;
1379 const char *str
[] = {
1382 N_("&Do backups with following extension:")
1385 QuickWidget widgets
[] = {
1387 QUICK_BUTTON (18, DLG_X
, DLG_Y
- 3, DLG_Y
, N_("&Cancel"), B_CANCEL
, NULL
),
1389 QUICK_BUTTON (6, DLG_X
, DLG_Y
- 3, DLG_Y
, N_("&OK"), B_ENTER
, NULL
),
1391 QUICK_CHECKBOX (4, DLG_X
, 8, DLG_Y
, N_("Check &POSIX new line"), &option_check_nl_at_eof
),
1393 QUICK_INPUT (8, DLG_X
, 6, DLG_Y
, option_backup_ext
, 9, 0, "edit-backup-ext", &str_result
),
1395 QUICK_RADIO (4, DLG_X
, 3, DLG_Y
, 3, str
, &option_save_mode
),
1399 QuickDialog dialog
= {
1400 DLG_X
, DLG_Y
, -1, -1, N_("Edit Save Mode"),
1401 "[Edit Save Mode]", widgets
, NULL
, FALSE
1406 size_t w0
, w1
, b_len
, w3
;
1408 #ifdef HAVE_ASSERT_H
1409 assert (option_backup_ext
!= NULL
);
1412 /* OK/Cancel buttons */
1413 w0
= str_term_width1 (_(widgets
[0].u
.button
.text
)) + 3;
1414 w1
= str_term_width1 (_(widgets
[1].u
.button
.text
)) + 5; /* default button */
1415 b_len
= w0
+ w1
+ 3;
1417 maxlen
= max (b_len
, (size_t) str_term_width1 (_(dialog
.title
)) + 2);
1420 for (i
= 0; i
< 3; i
++)
1425 w3
= max (w3
, (size_t) str_term_width1 (str
[i
]));
1428 maxlen
= max (maxlen
, w3
+ 4);
1430 dialog
.xlen
= min ((size_t) COLS
, maxlen
+ 8);
1432 widgets
[3].u
.input
.len
= w3
;
1433 widgets
[1].relative_x
= (dialog
.xlen
- b_len
) / 2;
1434 widgets
[0].relative_x
= widgets
[1].relative_x
+ w0
+ 2;
1436 for (i
= 0; i
< sizeof (widgets
) / sizeof (widgets
[0]); i
++)
1437 widgets
[i
].x_divisions
= dialog
.xlen
;
1439 if (quick_dialog (&dialog
) != B_CANCEL
)
1441 g_free (option_backup_ext
);
1442 option_backup_ext
= str_result
;
1446 /* --------------------------------------------------------------------------------------------- */
1449 edit_set_filename (WEdit
* edit
, const char *name
)
1451 g_free (edit
->filename
);
1456 edit
->filename
= tilde_expand (name
);
1457 if (edit
->dir
== NULL
&& !g_path_is_absolute (name
))
1458 edit
->dir
= vfs_get_current_dir ();
1461 /* --------------------------------------------------------------------------------------------- */
1462 /* Here we want to warn the users of overwriting an existing file,
1463 but only if they have made a change to the filename */
1464 /* returns 1 on success */
1466 edit_save_as_cmd (WEdit
* edit
)
1468 /* This heads the 'Save As' dialog box */
1471 int different_filename
= 0;
1473 if (!edit_check_newline (edit
))
1476 exp
= edit_get_save_file_as (edit
);
1477 edit_push_undo_action (edit
, KEY_PRESS
+ edit
->start_display
);
1484 edit
->force
|= REDRAW_COMPLETELY
;
1490 if (strcmp (edit
->filename
, exp
))
1495 if (mc_stat (exp
, &sb
) == 0 && !S_ISREG (sb
.st_mode
))
1497 edit_error_dialog (_("Save as"),
1499 ("Cannot save: destination is not a regular file")));
1501 edit
->force
|= REDRAW_COMPLETELY
;
1505 different_filename
= 1;
1506 file
= mc_open (exp
, O_RDONLY
| O_BINARY
);
1509 /* the file exists */
1511 /* Overwrite the current file or cancel the operation */
1512 if (edit_query_dialog2
1514 _("A file already exists with this name"), _("&Overwrite"), _("&Cancel")))
1516 edit
->force
|= REDRAW_COMPLETELY
;
1523 edit
->stat1
.st_mode
|= S_IWUSR
;
1525 save_lock
= lock_file (exp
);
1529 /* filenames equal, check if already locked */
1530 if (!edit
->locked
&& !edit
->delete_file
)
1531 save_lock
= lock_file (exp
);
1534 if (different_filename
)
1537 * Allow user to write into saved (under another name) file
1538 * even if original file had r/o user permissions.
1540 edit
->stat1
.st_mode
|= S_IWRITE
;
1543 rv
= edit_save_file (edit
, exp
);
1547 /* Succesful, so unlock both files */
1548 if (different_filename
)
1553 edit
->locked
= edit_unlock_file (edit
);
1557 if (edit
->locked
|| save_lock
)
1558 edit
->locked
= edit_unlock_file (edit
);
1561 edit_set_filename (edit
, exp
);
1562 if (edit
->lb
!= LB_ASIS
)
1563 edit_reload (edit
, exp
);
1566 edit
->delete_file
= 0;
1567 if (different_filename
)
1568 edit_load_syntax (edit
, NULL
, edit
->syntax_type
);
1569 edit
->force
|= REDRAW_COMPLETELY
;
1572 edit_error_dialog (_("Save as"), get_sys_error (_("Cannot save file")));
1575 /* Failed, so maintain modify (not save) lock */
1579 edit
->force
|= REDRAW_COMPLETELY
;
1584 edit
->force
|= REDRAW_COMPLETELY
;
1588 /* {{{ Macro stuff starts here */
1589 /* --------------------------------------------------------------------------------------------- */
1592 edit_delete_macro_cmd (WEdit
* edit
)
1596 hotkey
= editcmd_dialog_raw_key_query (_("Delete macro"), _("Press macro hotkey:"), 1);
1598 if (hotkey
!= 0 && !edit_delete_macro (edit
, hotkey
))
1599 message (D_ERROR
, _("Delete macro"), _("Macro not deleted"));
1602 /* --------------------------------------------------------------------------------------------- */
1604 /** returns FALSE on error */
1606 edit_execute_macro (WEdit
* edit
, int hotkey
)
1608 gboolean res
= FALSE
;
1612 const macros_t
*macros
;
1615 if (edit_get_macro (edit
, hotkey
, ¯os
, &indx
) &&
1616 macros
->macro
!= NULL
&& macros
->macro
->len
!= 0)
1620 edit
->force
|= REDRAW_PAGE
;
1622 for (i
= 0; i
< macros
->macro
->len
; i
++)
1624 const macro_action_t
*m_act
;
1626 m_act
= &g_array_index (macros
->macro
, struct macro_action_t
, i
);
1627 edit_execute_cmd (edit
, m_act
->action
, m_act
->ch
);
1636 /* --------------------------------------------------------------------------------------------- */
1638 /** returns FALSE on error */
1640 edit_store_macro_cmd (WEdit
* edit
)
1644 GString
*marcros_string
;
1645 mc_config_t
*macros_config
= NULL
;
1646 const char *section_name
= "editor";
1647 gchar
*macros_fname
;
1648 GArray
*macros
; /* current macro */
1650 gboolean have_macro
= FALSE
;
1651 char *skeyname
= NULL
;
1653 hotkey
= editcmd_dialog_raw_key_query (_("Save macro"), _("Press the macro's new hotkey:"), 1);
1654 if (hotkey
== ESC_CHAR
)
1657 tmp_act
= keybind_lookup_keymap_command (editor_map
, hotkey
);
1659 /* return FALSE if try assign macro into restricted hotkeys */
1660 if (tmp_act
== CK_MacroStartRecord
1661 || tmp_act
== CK_MacroStopRecord
|| tmp_act
== CK_MacroStartStopRecord
)
1664 edit_delete_macro (edit
, hotkey
);
1666 macros_fname
= mc_config_get_full_path (MC_MACRO_FILE
);
1667 macros_config
= mc_config_init (macros_fname
);
1668 g_free (macros_fname
);
1670 if (macros_config
== NULL
)
1673 edit_push_undo_action (edit
, KEY_PRESS
+ edit
->start_display
);
1675 marcros_string
= g_string_sized_new (250);
1676 macros
= g_array_new (TRUE
, FALSE
, sizeof (macro_action_t
));
1678 skeyname
= lookup_key_by_code (hotkey
);
1680 for (i
= 0; i
< macro_index
; i
++)
1682 macro_action_t m_act
;
1683 const char *action_name
;
1685 action_name
= keybind_lookup_actionname (record_macro_buf
[i
].action
);
1687 if (action_name
== NULL
)
1690 m_act
.action
= record_macro_buf
[i
].action
;
1691 m_act
.ch
= record_macro_buf
[i
].ch
;
1692 g_array_append_val (macros
, m_act
);
1694 g_string_append_printf (marcros_string
, "%s:%i;", action_name
,
1695 (int) record_macro_buf
[i
].ch
);
1700 macro
.hotkey
= hotkey
;
1701 macro
.macro
= macros
;
1702 g_array_append_val (macros_list
, macro
);
1703 mc_config_set_string (macros_config
, section_name
, skeyname
, marcros_string
->str
);
1706 mc_config_del_key (macros_config
, section_name
, skeyname
);
1709 edit_macro_sort_by_hotkey ();
1711 g_string_free (marcros_string
, TRUE
);
1712 mc_config_save_file (macros_config
, NULL
);
1713 mc_config_deinit (macros_config
);
1717 /* --------------------------------------------------------------------------------------------- */
1720 edit_repeat_macro_cmd (WEdit
* edit
)
1727 f
= input_dialog (_("Repeat last commands"), _("Repeat times:"), MC_HISTORY_EDIT_REPEAT
, NULL
);
1728 if (f
== NULL
|| *f
== '\0')
1734 count_repeat
= strtol (f
, &error
, 0);
1744 edit_push_undo_action (edit
, KEY_PRESS
+ edit
->start_display
);
1745 edit
->force
|= REDRAW_PAGE
;
1747 for (j
= 0; j
< count_repeat
; j
++)
1748 for (i
= 0; i
< macro_index
; i
++)
1749 edit_execute_cmd (edit
, record_macro_buf
[i
].action
, record_macro_buf
[i
].ch
);
1750 edit_update_screen (edit
);
1754 /* --------------------------------------------------------------------------------------------- */
1755 /** return FALSE on error */
1758 edit_load_macro_cmd (WEdit
* edit
)
1760 mc_config_t
*macros_config
= NULL
;
1761 gchar
**profile_keys
, **keys
;
1762 gchar
**values
, **curr_values
;
1763 gsize len
, values_len
;
1764 const char *section_name
= "editor";
1765 gchar
*macros_fname
;
1770 macros_fname
= mc_config_get_full_path (MC_MACRO_FILE
);
1771 macros_config
= mc_config_init (macros_fname
);
1772 g_free (macros_fname
);
1774 if (macros_config
== NULL
)
1777 profile_keys
= keys
= mc_config_get_keys (macros_config
, section_name
, &len
);
1778 while (*profile_keys
!= NULL
)
1780 gboolean have_macro
;
1784 macros
= g_array_new (TRUE
, FALSE
, sizeof (macro_action_t
));
1786 curr_values
= values
= mc_config_get_string_list (macros_config
, section_name
,
1787 *profile_keys
, &values_len
);
1788 hotkey
= lookup_key (*profile_keys
, NULL
);
1791 while (*curr_values
!= NULL
&& *curr_values
[0] != '\0')
1793 char **macro_pair
= NULL
;
1795 macro_pair
= g_strsplit (*curr_values
, ":", 2);
1797 if (macro_pair
!= NULL
)
1799 macro_action_t m_act
;
1800 if (macro_pair
[0] == NULL
|| macro_pair
[0][0] == '\0')
1804 m_act
.action
= keybind_lookup_action (macro_pair
[0]);
1805 g_free (macro_pair
[0]);
1806 macro_pair
[0] = NULL
;
1808 if (macro_pair
[1] == NULL
|| macro_pair
[1][0] == '\0')
1812 m_act
.ch
= strtol (macro_pair
[1], NULL
, 0);
1813 g_free (macro_pair
[1]);
1814 macro_pair
[1] = NULL
;
1816 if (m_act
.action
!= 0)
1818 /* a shell command */
1819 if ((m_act
.action
/ CK_PipeBlock (0)) == 1)
1821 m_act
.action
= CK_PipeBlock (0) + (m_act
.ch
> 0 ? m_act
.ch
: 0);
1824 g_array_append_val (macros
, m_act
);
1827 g_strfreev (macro_pair
);
1834 macro
.hotkey
= hotkey
;
1835 macro
.macro
= macros
;
1836 g_array_append_val (macros_list
, macro
);
1839 g_strfreev (values
);
1842 mc_config_deinit (macros_config
);
1843 edit_macro_sort_by_hotkey ();
1847 /* }}} Macro stuff end here */
1849 /* --------------------------------------------------------------------------------------------- */
1850 /** returns 1 on success */
1853 edit_save_confirm_cmd (WEdit
* edit
)
1857 if (!edit_check_newline (edit
))
1860 if (edit_confirm_save
)
1862 f
= g_strdup_printf (_("Confirm save file: \"%s\""), edit
->filename
);
1863 if (edit_query_dialog2 (_("Save file"), f
, _("&Save"), _("&Cancel")))
1870 return edit_save_cmd (edit
);
1873 /* --------------------------------------------------------------------------------------------- */
1874 /** returns 1 on success */
1877 edit_new_cmd (WEdit
* edit
)
1881 if (edit_query_dialog2
1884 ("Current text was modified without a file save.\nContinue discards these changes"),
1885 _("C&ontinue"), _("&Cancel")))
1887 edit
->force
|= REDRAW_COMPLETELY
;
1891 edit
->force
|= REDRAW_COMPLETELY
;
1893 return edit_renew (edit
); /* if this gives an error, something has really screwed up */
1896 /* --------------------------------------------------------------------------------------------- */
1899 edit_load_cmd (WEdit
* edit
, edit_current_file_t what
)
1904 && (edit_query_dialog2
1906 _("Current text was modified without a file save.\n"
1907 "Continue discards these changes"), _("C&ontinue"), _("&Cancel")) == 1))
1909 edit
->force
|= REDRAW_COMPLETELY
;
1915 case EDIT_FILE_COMMON
:
1916 exp
= input_expand_dialog (_("Load"), _("Enter file name:"),
1917 MC_HISTORY_EDIT_LOAD
, edit
->filename
);
1922 edit_load_file_from_filename (edit
, exp
);
1927 case EDIT_FILE_SYNTAX
:
1928 edit_load_syntax_file (edit
);
1931 case EDIT_FILE_MENU
:
1932 edit_load_menu_file (edit
);
1939 edit
->force
|= REDRAW_COMPLETELY
;
1943 /* --------------------------------------------------------------------------------------------- */
1945 if mark2 is -1 then marking is from mark1 to the cursor.
1946 Otherwise its between the markers. This handles this.
1947 Returns 1 if no text is marked.
1951 eval_marks (WEdit
* edit
, long *start_mark
, long *end_mark
)
1953 if (edit
->mark1
!= edit
->mark2
)
1955 long start_bol
, start_eol
;
1956 long end_bol
, end_eol
;
1961 if (edit
->end_mark_curs
< 0)
1962 end_mark_curs
= edit
->curs1
;
1964 end_mark_curs
= edit
->end_mark_curs
;
1966 if (edit
->mark2
>= 0)
1968 *start_mark
= min (edit
->mark1
, edit
->mark2
);
1969 *end_mark
= max (edit
->mark1
, edit
->mark2
);
1973 *start_mark
= min (edit
->mark1
, end_mark_curs
);
1974 *end_mark
= max (edit
->mark1
, end_mark_curs
);
1975 edit
->column2
= edit
->curs_col
+ edit
->over_col
;
1978 if (edit
->column_highlight
1979 && (((edit
->mark1
> end_mark_curs
) && (edit
->column1
< edit
->column2
))
1980 || ((edit
->mark1
< end_mark_curs
) && (edit
->column1
> edit
->column2
))))
1982 start_bol
= edit_bol (edit
, *start_mark
);
1983 start_eol
= edit_eol (edit
, start_bol
- 1) + 1;
1984 end_bol
= edit_bol (edit
, *end_mark
);
1985 end_eol
= edit_eol (edit
, *end_mark
);
1986 col1
= min (edit
->column1
, edit
->column2
);
1987 col2
= max (edit
->column1
, edit
->column2
);
1990 edit_move_forward3 (edit
, start_bol
, col2
, 0) - edit_move_forward3 (edit
, start_bol
,
1993 edit_move_forward3 (edit
, end_bol
, col2
, 0) - edit_move_forward3 (edit
, end_bol
,
1996 *start_mark
-= diff1
;
1998 *start_mark
= max (*start_mark
, start_eol
);
1999 *end_mark
= min (*end_mark
, end_eol
);
2005 *start_mark
= *end_mark
= 0;
2006 edit
->column2
= edit
->column1
= 0;
2011 /* --------------------------------------------------------------------------------------------- */
2014 edit_insert_over (WEdit
* edit
)
2018 for (i
= 0; i
< edit
->over_col
; i
++)
2020 edit_insert (edit
, ' ');
2025 /* --------------------------------------------------------------------------------------------- */
2028 edit_insert_column_of_text_from_file (WEdit
* edit
, int file
,
2029 long *start_pos
, long *end_pos
, int *col1
, int *col2
)
2033 int blocklen
= -1, width
= 0;
2034 unsigned char *data
;
2036 cursor
= edit
->curs1
;
2037 col
= edit_get_col (edit
);
2038 data
= g_malloc0 (TEMP_BUF_LEN
);
2040 while ((blocklen
= mc_read (file
, (char *) data
, TEMP_BUF_LEN
)) > 0)
2043 for (width
= 0; width
< blocklen
; width
++)
2045 if (data
[width
] == '\n')
2048 for (i
= 0; i
< blocklen
; i
++)
2050 if (data
[i
] == '\n')
2051 { /* fill in and move to next line */
2054 if (edit_get_byte (edit
, edit
->curs1
) != '\n')
2056 l
= width
- (edit_get_col (edit
) - col
);
2059 edit_insert (edit
, ' ');
2063 for (p
= edit
->curs1
;; p
++)
2065 if (p
== edit
->last_byte
)
2067 edit_cursor_move (edit
, edit
->last_byte
- edit
->curs1
);
2068 edit_insert_ahead (edit
, '\n');
2072 if (edit_get_byte (edit
, p
) == '\n')
2078 edit_cursor_move (edit
, edit_move_forward3 (edit
, p
, col
, 0) - edit
->curs1
);
2079 l
= col
- edit_get_col (edit
);
2080 while (l
>= space_width
)
2082 edit_insert (edit
, ' ');
2087 edit_insert (edit
, data
[i
]);
2091 *col2
= col
+ width
;
2092 *start_pos
= cursor
;
2093 *end_pos
= edit
->curs1
;
2094 edit_cursor_move (edit
, cursor
- edit
->curs1
);
2100 /* --------------------------------------------------------------------------------------------- */
2103 edit_block_copy_cmd (WEdit
* edit
)
2105 long start_mark
, end_mark
, current
= edit
->curs1
;
2110 unsigned char *copy_buf
;
2112 edit_update_curs_col (edit
);
2113 if (eval_marks (edit
, &start_mark
, &end_mark
))
2116 copy_buf
= edit_get_block (edit
, start_mark
, end_mark
, &size
);
2118 /* all that gets pushed are deletes hence little space is used on the stack */
2120 edit_push_markers (edit
);
2122 if (edit
->column_highlight
)
2124 col_delta
= abs (edit
->column2
- edit
->column1
);
2125 edit_insert_column_of_text (edit
, copy_buf
, size
, col_delta
, &mark1
, &mark2
, &c1
, &c2
);
2130 edit_insert_ahead (edit
, copy_buf
[size
]);
2134 edit_scroll_screen_over_cursor (edit
);
2136 if (edit
->column_highlight
)
2137 edit_set_markers (edit
, edit
->curs1
, mark2
, c1
, c2
);
2138 else if (start_mark
< current
&& end_mark
> current
)
2139 edit_set_markers (edit
, start_mark
, end_mark
+ end_mark
- start_mark
, 0, 0);
2141 edit
->force
|= REDRAW_PAGE
;
2145 /* --------------------------------------------------------------------------------------------- */
2148 edit_block_move_cmd (WEdit
* edit
)
2151 unsigned char *copy_buf
= NULL
;
2152 long start_mark
, end_mark
;
2155 if (eval_marks (edit
, &start_mark
, &end_mark
))
2158 line
= edit
->curs_line
;
2159 if (edit
->mark2
< 0)
2160 edit_mark_cmd (edit
, 0);
2161 edit_push_markers (edit
);
2163 if (edit
->column_highlight
)
2171 c1
= min (edit
->column1
, edit
->column2
);
2172 c2
= max (edit
->column1
, edit
->column2
);
2173 b_width
= (c2
- c1
);
2175 edit_update_curs_col (edit
);
2178 x2
= x
+ edit
->over_col
;
2180 /* do nothing when cursor inside first line of selected area */
2181 if ((edit_eol (edit
, edit
->curs1
) == edit_eol (edit
, start_mark
)) && (x2
> c1
&& x2
<= c2
))
2184 if (edit
->curs1
> start_mark
&& edit
->curs1
< edit_eol (edit
, end_mark
))
2188 else if (x
> c1
&& x
<= c2
)
2191 /* save current selection into buffer */
2192 copy_buf
= edit_get_block (edit
, start_mark
, end_mark
, &size
);
2194 /* remove current selection */
2195 edit_block_delete_cmd (edit
);
2197 edit
->over_col
= max (0, edit
->over_col
- b_width
);
2198 /* calculate the cursor pos after delete block */
2199 current
= edit_move_forward3 (edit
, edit_bol (edit
, edit
->curs1
), x
, 0);
2200 edit_cursor_move (edit
, current
- edit
->curs1
);
2201 edit_scroll_screen_over_cursor (edit
);
2203 /* add TWS if need before block insertion */
2204 if (option_cursor_beyond_eol
&& edit
->over_col
> 0)
2205 edit_insert_over (edit
);
2207 edit_insert_column_of_text (edit
, copy_buf
, size
, b_width
, &mark1
, &mark2
, &c1
, &c2
);
2208 edit_set_markers (edit
, mark1
, mark2
, c1
, c2
);
2214 current
= edit
->curs1
;
2215 copy_buf
= g_malloc0 (end_mark
- start_mark
);
2216 edit_cursor_move (edit
, start_mark
- edit
->curs1
);
2217 edit_scroll_screen_over_cursor (edit
);
2219 while (count
< end_mark
)
2221 copy_buf
[end_mark
- count
- 1] = edit_delete (edit
, 1);
2224 edit_scroll_screen_over_cursor (edit
);
2225 edit_cursor_move (edit
,
2226 current
- edit
->curs1
-
2227 (((current
- edit
->curs1
) > 0) ? end_mark
- start_mark
: 0));
2228 edit_scroll_screen_over_cursor (edit
);
2229 while (count
-- > start_mark
)
2230 edit_insert_ahead (edit
, copy_buf
[end_mark
- count
- 1]);
2231 edit_set_markers (edit
, edit
->curs1
, edit
->curs1
+ end_mark
- start_mark
, 0, 0);
2234 edit_scroll_screen_over_cursor (edit
);
2236 edit
->force
|= REDRAW_PAGE
;
2239 /* --------------------------------------------------------------------------------------------- */
2240 /** returns 1 if canceelled by user */
2243 edit_block_delete_cmd (WEdit
* edit
)
2245 long start_mark
, end_mark
;
2246 if (eval_marks (edit
, &start_mark
, &end_mark
))
2248 edit_delete_line (edit
);
2251 return edit_block_delete (edit
);
2254 /* --------------------------------------------------------------------------------------------- */
2255 /** call with edit = 0 before shutdown to close memory leaks */
2258 edit_replace_cmd (WEdit
* edit
, int again
)
2260 /* 1 = search string, 2 = replace with */
2261 static char *saved1
= NULL
; /* saved default[123] */
2262 static char *saved2
= NULL
;
2263 char *input1
= NULL
; /* user input from the dialog */
2264 char *input2
= NULL
;
2267 long times_replaced
= 0;
2268 gboolean once_found
= FALSE
;
2272 g_free (saved1
), saved1
= NULL
;
2273 g_free (saved2
), saved2
= NULL
;
2277 edit
->force
|= REDRAW_COMPLETELY
;
2279 if (again
&& !saved1
&& !saved2
)
2284 input1
= g_strdup (saved1
? saved1
: "");
2285 input2
= g_strdup (saved2
? saved2
: "");
2289 char *tmp_inp1
, *tmp_inp2
;
2290 disp1
= edit_replace_cmd__conv_to_display (saved1
? saved1
: (char *) "");
2291 disp2
= edit_replace_cmd__conv_to_display (saved2
? saved2
: (char *) "");
2293 edit_push_undo_action (edit
, KEY_PRESS
+ edit
->start_display
);
2295 editcmd_dialog_replace_show (edit
, disp1
, disp2
, &input1
, &input2
);
2300 if (input1
== NULL
|| *input1
== '\0')
2302 edit
->force
= REDRAW_COMPLETELY
;
2308 input1
= edit_replace_cmd__conv_to_input (input1
);
2309 input2
= edit_replace_cmd__conv_to_input (input2
);
2313 g_free (saved1
), saved1
= g_strdup (input1
);
2314 g_free (saved2
), saved2
= g_strdup (input2
);
2316 mc_search_free (edit
->search
);
2317 edit
->search
= NULL
;
2322 edit
->search
= mc_search_new (input1
, -1);
2323 if (edit
->search
== NULL
)
2325 edit
->search_start
= edit
->curs1
;
2328 edit
->search
->search_type
= edit_search_options
.type
;
2329 edit
->search
->is_all_charsets
= edit_search_options
.all_codepages
;
2330 edit
->search
->is_case_sensitive
= edit_search_options
.case_sens
;
2331 edit
->search
->whole_words
= edit_search_options
.whole_words
;
2332 edit
->search
->search_fn
= edit_search_cmd_callback
;
2335 if (edit
->found_len
&& edit
->search_start
== edit
->found_start
+ 1
2336 && edit_search_options
.backwards
)
2337 edit
->search_start
--;
2339 if (edit
->found_len
&& edit
->search_start
== edit
->found_start
- 1
2340 && !edit_search_options
.backwards
)
2341 edit
->search_start
++;
2347 if (!editcmd_find (edit
, &len
))
2349 if (!(edit
->search
->error
== MC_SEARCH_E_OK
||
2350 (once_found
&& edit
->search
->error
== MC_SEARCH_E_NOTFOUND
)))
2352 edit_error_dialog (_("Search"), edit
->search
->error_str
);
2358 edit
->search_start
= edit
->search
->normal_offset
;
2359 /*returns negative on not found or error in pattern */
2361 if ((edit
->search_start
>= 0) && (edit
->search_start
< edit
->last_byte
))
2364 GString
*tmp_str
, *repl_str
;
2366 edit
->found_start
= edit
->search_start
;
2367 i
= edit
->found_len
= len
;
2369 edit_cursor_move (edit
, edit
->search_start
- edit
->curs1
);
2370 edit_scroll_screen_over_cursor (edit
);
2372 if (edit
->replace_mode
== 0)
2377 l
= edit
->curs_row
- edit
->widget
.lines
/ 3;
2379 edit_scroll_downward (edit
, l
);
2381 edit_scroll_upward (edit
, -l
);
2383 edit_scroll_screen_over_cursor (edit
);
2384 edit
->force
|= REDRAW_PAGE
;
2385 edit_render_keypress (edit
);
2387 /*so that undo stops at each query */
2388 edit_push_key_press (edit
);
2389 /* and prompt 2/3 down */
2390 disp1
= edit_replace_cmd__conv_to_display (saved1
);
2391 disp2
= edit_replace_cmd__conv_to_display (saved2
);
2392 prompt
= editcmd_dialog_replace_prompt_show (edit
, disp1
, disp2
, -1, -1);
2396 if (prompt
== B_REPLACE_ALL
)
2397 edit
->replace_mode
= 1;
2398 else if (prompt
== B_SKIP_REPLACE
)
2400 if (edit_search_options
.backwards
)
2401 edit
->search_start
--;
2403 edit
->search_start
++;
2404 continue; /* loop */
2406 else if (prompt
== B_CANCEL
)
2408 edit
->replace_mode
= -1;
2413 /* don't process string each time */
2414 tmp_str
= g_string_new (input2
);
2415 repl_str
= mc_search_prepare_replace_str (edit
->search
, tmp_str
);
2416 g_string_free (tmp_str
, TRUE
);
2418 if (edit
->search
->error
!= MC_SEARCH_E_OK
)
2420 edit_error_dialog (_("Replace"), edit
->search
->error_str
);
2421 g_string_free (repl_str
, TRUE
);
2425 /* delete then insert new */
2426 for (i
= 0; i
< len
; i
++)
2427 edit_delete (edit
, 1);
2429 for (i
= 0; i
< repl_str
->len
; i
++)
2430 edit_insert (edit
, repl_str
->str
[i
]);
2432 edit
->found_len
= repl_str
->len
;
2433 g_string_free (repl_str
, TRUE
);
2436 /* so that we don't find the same string again */
2437 if (edit_search_options
.backwards
)
2438 edit
->search_start
--;
2441 edit
->search_start
+= edit
->found_len
;
2443 if (edit
->search_start
>= edit
->last_byte
)
2447 edit_scroll_screen_over_cursor (edit
);
2451 /* try and find from right here for next search */
2452 edit
->search_start
= edit
->curs1
;
2453 edit_update_curs_col (edit
);
2455 edit
->force
|= REDRAW_PAGE
;
2456 edit_render_keypress (edit
);
2458 if (times_replaced
== 0)
2459 query_dialog (_("Replace"), _("Search string not found"), D_NORMAL
, 1, _("&OK"));
2463 while (edit
->replace_mode
>= 0);
2465 edit_scroll_screen_over_cursor (edit
);
2466 edit
->force
|= REDRAW_COMPLETELY
;
2467 edit_render_keypress (edit
);
2469 if ((edit
->replace_mode
== 1) && (times_replaced
!= 0))
2470 message (D_NORMAL
, _("Replace"), _("%ld replacements made"), times_replaced
);
2477 /* --------------------------------------------------------------------------------------------- */
2480 edit_search_cmd_callback (const void *user_data
, gsize char_offset
)
2482 return edit_get_byte ((WEdit
*) user_data
, (long) char_offset
);
2485 /* --------------------------------------------------------------------------------------------- */
2488 edit_search_cmd (WEdit
* edit
, gboolean again
)
2495 else if (edit
->last_search_string
!= NULL
)
2496 edit_do_search (edit
);
2499 /* find last search string in history */
2502 history
= history_get (MC_HISTORY_SHARED_SEARCH
);
2503 if (history
!= NULL
&& history
->data
!= NULL
)
2505 edit
->last_search_string
= (char *) history
->data
;
2506 history
->data
= NULL
;
2507 history
= g_list_first (history
);
2508 g_list_foreach (history
, (GFunc
) g_free
, NULL
);
2509 g_list_free (history
);
2511 edit
->search
= mc_search_new (edit
->last_search_string
, -1);
2512 if (edit
->search
== NULL
)
2514 /* if not... then ask for an expression */
2515 g_free (edit
->last_search_string
);
2516 edit
->last_search_string
= NULL
;
2521 edit
->search
->search_type
= edit_search_options
.type
;
2522 edit
->search
->is_all_charsets
= edit_search_options
.all_codepages
;
2523 edit
->search
->is_case_sensitive
= edit_search_options
.case_sens
;
2524 edit
->search
->whole_words
= edit_search_options
.whole_words
;
2525 edit
->search
->search_fn
= edit_search_cmd_callback
;
2526 edit_do_search (edit
);
2531 /* if not... then ask for an expression */
2532 g_free (edit
->last_search_string
);
2533 edit
->last_search_string
= NULL
;
2540 /* --------------------------------------------------------------------------------------------- */
2542 * Check if it's OK to close the editor. If there are unsaved changes,
2543 * ask user. Return 1 if it's OK to exit, 0 to continue editing.
2547 edit_ok_to_exit (WEdit
* edit
)
2551 if (!edit
->modified
)
2554 if (!mc_global
.midnight_shutdown
)
2556 if (!edit_check_newline (edit
))
2560 act
= edit_query_dialog3 (_("Quit"), _("File was modified. Save with exit?"),
2561 _("&Yes"), _("&No"), _("&Cancel quit"));
2566 edit_query_dialog2 (_("Quit"),
2567 _("Midnight Commander is being shut down.\nSave modified file?"),
2568 _("&Yes"), _("&No"));
2578 edit_push_markers (edit
);
2579 edit_set_markers (edit
, 0, 0, 0, 0);
2580 if (!edit_save_cmd (edit
) || mc_global
.midnight_shutdown
)
2581 return mc_global
.midnight_shutdown
;
2585 case 2: /* Cancel quit */
2593 /* --------------------------------------------------------------------------------------------- */
2594 /** save block, returns 1 on success */
2597 edit_save_block (WEdit
* edit
, const char *filename
, long start
, long finish
)
2601 file
= mc_open (filename
, O_CREAT
| O_WRONLY
| O_TRUNC
,
2602 S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IROTH
| O_BINARY
);
2606 if (edit
->column_highlight
)
2609 r
= mc_write (file
, VERTICAL_MAGIC
, sizeof (VERTICAL_MAGIC
));
2612 unsigned char *block
, *p
;
2613 p
= block
= edit_get_block (edit
, start
, finish
, &len
);
2616 r
= mc_write (file
, p
, len
);
2629 len
= finish
- start
;
2630 buf
= g_malloc0 (TEMP_BUF_LEN
);
2631 while (start
!= finish
)
2633 end
= min (finish
, start
+ TEMP_BUF_LEN
);
2634 for (; i
< end
; i
++)
2635 buf
[i
- start
] = edit_get_byte (edit
, i
);
2636 len
-= mc_write (file
, (char *) buf
, end
- start
);
2647 /* --------------------------------------------------------------------------------------------- */
2650 edit_paste_from_history (WEdit
* edit
)
2653 edit_error_dialog (_("Error"), _("This function is not implemented"));
2656 /* --------------------------------------------------------------------------------------------- */
2659 edit_copy_to_X_buf_cmd (WEdit
* edit
)
2661 long start_mark
, end_mark
;
2662 if (eval_marks (edit
, &start_mark
, &end_mark
))
2664 if (!edit_save_block_to_clip_file (edit
, start_mark
, end_mark
))
2666 edit_error_dialog (_("Copy to clipboard"), get_sys_error (_("Unable to save to file")));
2669 /* try use external clipboard utility */
2670 mc_event_raise (MCEVENT_GROUP_CORE
, "clipboard_file_to_ext_clip", NULL
);
2675 /* --------------------------------------------------------------------------------------------- */
2678 edit_cut_to_X_buf_cmd (WEdit
* edit
)
2680 long start_mark
, end_mark
;
2681 if (eval_marks (edit
, &start_mark
, &end_mark
))
2683 if (!edit_save_block_to_clip_file (edit
, start_mark
, end_mark
))
2685 edit_error_dialog (_("Cut to clipboard"), _("Unable to save to file"));
2688 /* try use external clipboard utility */
2689 mc_event_raise (MCEVENT_GROUP_CORE
, "clipboard_file_to_ext_clip", NULL
);
2691 edit_block_delete_cmd (edit
);
2692 edit_mark_cmd (edit
, 1);
2696 /* --------------------------------------------------------------------------------------------- */
2699 edit_paste_from_X_buf_cmd (WEdit
* edit
)
2702 /* try use external clipboard utility */
2703 mc_event_raise (MCEVENT_GROUP_CORE
, "clipboard_file_from_ext_clip", NULL
);
2704 tmp
= mc_config_get_full_path (EDIT_CLIP_FILE
);
2705 edit_insert_file (edit
, tmp
);
2710 /* --------------------------------------------------------------------------------------------- */
2712 * Ask user for the line and go to that line.
2713 * Negative numbers mean line from the end (i.e. -1 is the last line).
2717 edit_goto_cmd (WEdit
* edit
)
2720 static long line
= 0; /* line as typed, saved as default */
2725 g_snprintf (s
, sizeof (s
), "%ld", line
);
2726 f
= input_dialog (_("Goto line"), _("Enter line:"), MC_HISTORY_EDIT_GOTO_LINE
, line
? s
: "");
2736 l
= strtol (f
, &error
, 0);
2745 l
= edit
->total_lines
+ l
+ 2;
2746 edit_move_display (edit
, l
- edit
->widget
.lines
/ 2 - 1);
2747 edit_move_to_line (edit
, l
- 1);
2748 edit
->force
|= REDRAW_COMPLETELY
;
2753 /* --------------------------------------------------------------------------------------------- */
2754 /** Return 1 on success */
2757 edit_save_block_cmd (WEdit
* edit
)
2759 long start_mark
, end_mark
;
2762 if (eval_marks (edit
, &start_mark
, &end_mark
))
2765 tmp
= mc_config_get_full_path (EDIT_CLIP_FILE
);
2767 input_expand_dialog (_("Save block"), _("Enter file name:"),
2768 MC_HISTORY_EDIT_SAVE_BLOCK
, tmp
);
2770 edit_push_undo_action (edit
, KEY_PRESS
+ edit
->start_display
);
2780 if (edit_save_block (edit
, exp
, start_mark
, end_mark
))
2783 edit
->force
|= REDRAW_COMPLETELY
;
2789 edit_error_dialog (_("Save block"), get_sys_error (_("Cannot save file")));
2793 edit
->force
|= REDRAW_COMPLETELY
;
2798 /* --------------------------------------------------------------------------------------------- */
2799 /** returns TRUE on success */
2802 edit_insert_file_cmd (WEdit
* edit
)
2806 gboolean ret
= FALSE
;
2808 tmp
= mc_config_get_full_path (EDIT_CLIP_FILE
);
2809 exp
= input_expand_dialog (_("Insert file"), _("Enter file name:"),
2810 MC_HISTORY_EDIT_INSERT_FILE
, tmp
);
2813 edit_push_undo_action (edit
, KEY_PRESS
+ edit
->start_display
);
2815 if (exp
!= NULL
&& *exp
!= '\0')
2817 ret
= (edit_insert_file (edit
, exp
) >= 0);
2819 edit_error_dialog (_("Insert file"), get_sys_error (_("Cannot insert file")));
2824 edit
->force
|= REDRAW_COMPLETELY
;
2828 /* --------------------------------------------------------------------------------------------- */
2829 /** sorts a block, returns -1 on system fail, 1 on cancel and 0 on success */
2832 edit_sort_cmd (WEdit
* edit
)
2834 static char *old
= 0;
2835 char *exp
, *tmp
, *tmp_edit_block_name
, *tmp_edit_temp_name
;
2836 long start_mark
, end_mark
;
2839 if (eval_marks (edit
, &start_mark
, &end_mark
))
2841 edit_error_dialog (_("Sort block"), _("You must first highlight a block of text"));
2845 tmp
= mc_config_get_full_path (EDIT_BLOCK_FILE
);
2846 edit_save_block (edit
, tmp
, start_mark
, end_mark
);
2849 exp
= input_dialog (_("Run sort"),
2850 _("Enter sort options (see manpage) separated by whitespace:"),
2851 MC_HISTORY_EDIT_SORT
, (old
!= NULL
) ? old
: "");
2857 tmp_edit_block_name
= mc_config_get_full_path (EDIT_BLOCK_FILE
);
2858 tmp_edit_temp_name
= mc_config_get_full_path (EDIT_TEMP_FILE
);
2860 g_strconcat (" sort ", exp
, " ", tmp_edit_block_name
,
2861 " > ", tmp_edit_temp_name
, (char *) NULL
);
2862 g_free (tmp_edit_temp_name
);
2863 g_free (tmp_edit_block_name
);
2869 if (e
== -1 || e
== 127)
2871 edit_error_dialog (_("Sort"), get_sys_error (_("Cannot execute sort command")));
2876 sprintf (q
, "%d ", e
);
2877 tmp
= g_strdup_printf (_("Sort returned non-zero: %s"), q
);
2878 edit_error_dialog (_("Sort"), tmp
);
2884 edit
->force
|= REDRAW_COMPLETELY
;
2886 if (edit_block_delete_cmd (edit
))
2888 tmp
= mc_config_get_full_path (EDIT_TEMP_FILE
);
2889 edit_insert_file (edit
, tmp
);
2894 /* --------------------------------------------------------------------------------------------- */
2896 * Ask user for a command, execute it and paste its output back to the
2901 edit_ext_cmd (WEdit
* edit
)
2903 char *exp
, *tmp
, *tmp_edit_temp_file
;
2907 input_dialog (_("Paste output of external command"),
2908 _("Enter shell command(s):"), MC_HISTORY_EDIT_PASTE_EXTCMD
, NULL
);
2913 tmp_edit_temp_file
= mc_config_get_full_path (EDIT_TEMP_FILE
);
2914 tmp
= g_strconcat (exp
, " > ", tmp_edit_temp_file
, (char *) NULL
);
2915 g_free (tmp_edit_temp_file
);
2922 edit_error_dialog (_("External command"), get_sys_error (_("Cannot execute command")));
2926 edit
->force
|= REDRAW_COMPLETELY
;
2927 tmp
= mc_config_get_full_path (EDIT_TEMP_FILE
);
2928 edit_insert_file (edit
, tmp
);
2933 /* --------------------------------------------------------------------------------------------- */
2934 /** if block is 1, a block must be highlighted and the shell command
2935 processes it. If block is 0 the shell command is a straight system
2936 command, that just produces some output which is to be inserted */
2939 edit_block_process_cmd (WEdit
* edit
, int macro_number
)
2942 char *macros_fname
= NULL
;
2944 fname
= g_strdup_printf ("%s.%i.sh", MC_EXTMACRO_FILE
, macro_number
);
2945 macros_fname
= g_build_filename (mc_config_get_data_path (), fname
, (char *) NULL
);
2946 user_menu (edit
, macros_fname
, 0);
2948 g_free (macros_fname
);
2949 edit
->force
|= REDRAW_COMPLETELY
;
2952 /* --------------------------------------------------------------------------------------------- */
2955 edit_mail_dialog (WEdit
* edit
)
2958 char *tmail_subject
;
2961 static char *mail_cc_last
= 0;
2962 static char *mail_subject_last
= 0;
2963 static char *mail_to_last
= 0;
2965 QuickWidget quick_widgets
[] = {
2966 /* 0 */ QUICK_BUTTON (6, 10, 9, MAIL_DLG_HEIGHT
, N_("&Cancel"), B_CANCEL
, NULL
),
2967 /* 1 */ QUICK_BUTTON (2, 10, 9, MAIL_DLG_HEIGHT
, N_("&OK"), B_ENTER
, NULL
),
2968 /* 2 */ QUICK_INPUT (3, 50, 8, MAIL_DLG_HEIGHT
, "", 44, 0, "mail-dlg-input", &tmail_cc
),
2969 /* 3 */ QUICK_LABEL (3, 50, 7, MAIL_DLG_HEIGHT
, N_("Copies to")),
2970 /* 4 */ QUICK_INPUT (3, 50, 6, MAIL_DLG_HEIGHT
, "", 44, 0, "mail-dlg-input-2",
2972 /* 5 */ QUICK_LABEL (3, 50, 5, MAIL_DLG_HEIGHT
, N_("Subject")),
2973 /* 6 */ QUICK_INPUT (3, 50, 4, MAIL_DLG_HEIGHT
, "", 44, 0, "mail-dlg-input-3", &tmail_to
),
2974 /* 7 */ QUICK_LABEL (3, 50, 3, MAIL_DLG_HEIGHT
, N_("To")),
2975 /* 8 */ QUICK_LABEL (3, 50, 2, MAIL_DLG_HEIGHT
, N_("mail -s <subject> -c <cc> <to>")),
2979 QuickDialog Quick_input
= {
2980 50, MAIL_DLG_HEIGHT
, -1, -1, N_("Mail"),
2981 "[Input Line Keys]", quick_widgets
, NULL
, FALSE
2984 quick_widgets
[2].u
.input
.text
= mail_cc_last
? mail_cc_last
: "";
2985 quick_widgets
[4].u
.input
.text
= mail_subject_last
? mail_subject_last
: "";
2986 quick_widgets
[6].u
.input
.text
= mail_to_last
? mail_to_last
: "";
2988 if (quick_dialog (&Quick_input
) != B_CANCEL
)
2990 g_free (mail_cc_last
);
2991 g_free (mail_subject_last
);
2992 g_free (mail_to_last
);
2993 mail_cc_last
= tmail_cc
;
2994 mail_subject_last
= tmail_subject
;
2995 mail_to_last
= tmail_to
;
2996 pipe_mail (edit
, mail_to_last
, mail_subject_last
, mail_cc_last
);
3001 /*******************/
3002 /* Word Completion */
3003 /*******************/
3005 /* --------------------------------------------------------------------------------------------- */
3007 * Complete current word using regular expression search
3008 * backwards beginning at the current cursor position.
3012 edit_complete_word_cmd (WEdit
* edit
)
3014 gsize i
, max_len
, word_len
= 0, num_compl
= 0;
3015 long word_start
= 0;
3016 unsigned char *bufpos
;
3018 struct selection
compl[MAX_WORD_COMPLETIONS
]; /* completions */
3020 /* search start of word to be completed */
3021 if (!edit_find_word_start (edit
, &word_start
, &word_len
))
3024 /* prepare match expression */
3025 bufpos
= &edit
->buffers1
[word_start
>> S_EDIT_BUF_SIZE
][word_start
& M_EDIT_BUF_SIZE
];
3027 /* match_expr = g_strdup_printf ("\\b%.*s[a-zA-Z_0-9]+", word_len, bufpos); */
3030 ("(^|\\s+|\\b)%.*s[^\\s\\.=\\+\\[\\]\\(\\)\\,\\;\\:\\\"\\'\\-\\?\\/\\|\\\\\\{\\}\\*\\&\\^\\%%\\$#@\\!]+",
3031 (int) word_len
, bufpos
);
3033 /* collect the possible completions */
3034 /* start search from begin to end of file */
3036 edit_collect_completions (edit
, word_start
, word_len
, match_expr
,
3037 (struct selection
*) &compl, &num_compl
);
3041 /* insert completed word if there is only one match */
3044 for (i
= word_len
; i
< compl[0].len
; i
++)
3045 edit_insert (edit
, *(compl[0].text
+ i
));
3047 /* more than one possible completion => ask the user */
3050 /* !!! usually only a beep is expected and when <ALT-TAB> is !!! */
3051 /* !!! pressed again the selection dialog pops up, but that !!! */
3052 /* !!! seems to require a further internal state !!! */
3055 /* let the user select the preferred completion */
3056 editcmd_dialog_completion_show (edit
, max_len
, word_len
,
3057 (struct selection
*) &compl, num_compl
);
3061 g_free (match_expr
);
3062 /* release memory before return */
3063 for (i
= 0; i
< num_compl
; i
++)
3064 g_free (compl[i
].text
);
3067 /* --------------------------------------------------------------------------------------------- */
3070 edit_select_codepage_cmd (WEdit
* edit
)
3073 if (do_select_codepage ())
3074 edit_set_codeset (edit
);
3076 edit
->force
= REDRAW_COMPLETELY
;
3077 edit_refresh_cmd (edit
);
3083 /* --------------------------------------------------------------------------------------------- */
3086 edit_insert_literal_cmd (WEdit
* edit
)
3088 int char_for_insertion
= editcmd_dialog_raw_key_query (_("Insert literal"),
3089 _("Press any key:"), 0);
3090 edit_execute_key_command (edit
, -1, ascii_alpha_to_cntrl (char_for_insertion
));
3093 /* --------------------------------------------------------------------------------------------- */
3096 edit_begin_end_macro_cmd (WEdit
* edit
)
3098 /* edit is a pointer to the widget */
3101 unsigned long command
= macro_index
< 0 ? CK_MacroStartRecord
: CK_MacroStopRecord
;
3102 edit_execute_key_command (edit
, command
, -1);
3106 /* --------------------------------------------------------------------------------------------- */
3109 edit_begin_end_repeat_cmd (WEdit
* edit
)
3111 /* edit is a pointer to the widget */
3114 unsigned long command
= macro_index
< 0 ? CK_RepeatStartRecord
: CK_RepeatStopRecord
;
3115 edit_execute_key_command (edit
, command
, -1);
3119 /* --------------------------------------------------------------------------------------------- */
3122 edit_load_forward_cmd (WEdit
* edit
)
3126 if (edit_query_dialog2
3128 _("Current text was modified without a file save\n"
3129 "Continue discards these changes"), _("C&ontinue"), _("&Cancel")))
3131 edit
->force
|= REDRAW_COMPLETELY
;
3135 if (edit_stack_iterator
+ 1 < MAX_HISTORY_MOVETO
)
3137 if (edit_history_moveto
[edit_stack_iterator
+ 1].line
< 1)
3141 edit_stack_iterator
++;
3142 if (edit_history_moveto
[edit_stack_iterator
].filename
)
3144 edit_reload_line (edit
, edit_history_moveto
[edit_stack_iterator
].filename
,
3145 edit_history_moveto
[edit_stack_iterator
].line
);
3159 /* --------------------------------------------------------------------------------------------- */
3162 edit_load_back_cmd (WEdit
* edit
)
3166 if (edit_query_dialog2
3168 _("Current text was modified without a file save\n"
3169 "Continue discards these changes"), _("C&ontinue"), _("&Cancel")))
3171 edit
->force
|= REDRAW_COMPLETELY
;
3175 if (edit_stack_iterator
> 0)
3177 edit_stack_iterator
--;
3178 if (edit_history_moveto
[edit_stack_iterator
].filename
)
3180 edit_reload_line (edit
, edit_history_moveto
[edit_stack_iterator
].filename
,
3181 edit_history_moveto
[edit_stack_iterator
].line
);
3195 /* --------------------------------------------------------------------------------------------- */
3198 edit_get_match_keyword_cmd (WEdit
* edit
)
3200 gsize word_len
= 0, max_len
= 0;
3203 long word_start
= 0;
3204 unsigned char *bufpos
;
3208 char *tagfile
= NULL
;
3210 etags_hash_t def_hash
[MAX_DEFINITIONS
];
3212 for (i
= 0; i
< MAX_DEFINITIONS
; i
++)
3214 def_hash
[i
].filename
= NULL
;
3217 /* search start of word to be completed */
3218 if (!edit_find_word_start (edit
, &word_start
, &word_len
))
3221 /* prepare match expression */
3222 bufpos
= &edit
->buffers1
[word_start
>> S_EDIT_BUF_SIZE
][word_start
& M_EDIT_BUF_SIZE
];
3223 match_expr
= g_strdup_printf ("%.*s", (int) word_len
, bufpos
);
3225 ptr
= g_get_current_dir ();
3226 path
= g_strconcat (ptr
, G_DIR_SEPARATOR_S
, (char *) NULL
);
3229 /* Recursive search file 'TAGS' in parent dirs */
3232 ptr
= g_path_get_dirname (path
);
3236 tagfile
= mc_build_filename (path
, TAGS_NAME
, (char *) NULL
);
3237 if (exist_file (tagfile
))
3240 while (strcmp (path
, G_DIR_SEPARATOR_S
) != 0);
3245 etags_set_definition_hash (tagfile
, path
, match_expr
, (etags_hash_t
*) & def_hash
);
3250 max_len
= MAX_WIDTH_DEF_DIALOG
;
3254 editcmd_dialog_select_definition_show (edit
, match_expr
, max_len
, word_len
,
3255 (etags_hash_t
*) & def_hash
, num_def
);
3257 g_free (match_expr
);
3260 /* --------------------------------------------------------------------------------------------- */