1 /* editor high level editing commands
3 Copyright (C) 1996, 1997, 1998, 2001, 2002, 2003, 2004, 2005, 2006,
4 2007 Free Software Foundation, Inc.
6 Authors: 1996, 1997 Paul Sheer
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
26 * \brief Source: editor high level editing commands
31 /* #define PIPE_BLOCKS_SO_READ_BYTE_BY_BYTE */
40 #include <sys/types.h>
48 #include "../src/global.h"
49 #include "../src/history.h"
51 #include "../src/tty.h" /* LINES */
52 #include "../src/widget.h" /* listbox_new() */
53 #include "../src/layout.h" /* clr_scr() */
54 #include "../src/main.h" /* mc_home source_codepage */
55 #include "../src/help.h" /* interactive_display() */
56 #include "../src/key.h" /* XCTRL */
57 #include "../src/wtools.h" /* message() */
58 #include "../src/charsets.h"
59 #include "../src/selcodepage.h"
60 #include "../src/strutil.h" /* utf string functions */
62 #include "../edit/edit-impl.h"
63 #include "../edit/edit.h"
64 #include "../edit/editlock.h"
65 #include "../edit/editcmddef.h"
66 #include "../edit/edit-widget.h"
67 #include "../edit/editcmd_dialogs.h"
68 #include "../edit/etags.h"
72 /* search and replace: */
73 static int search_create_bookmark
= 0;
74 /* static int search_in_all_charsets = 0; */
76 /* queries on a save */
77 int edit_confirm_save
= 1;
79 static int edit_save_cmd (WEdit
*edit
);
80 static unsigned char *edit_get_block (WEdit
*edit
, long start
,
84 edit_search_cmd_search_create_bookmark(WEdit
* edit
)
86 int found
= 0, books
= 0;
87 int l
= 0, l_last
= -1;
92 if (!mc_search_run(edit
->search
, (void *) edit
, q
, edit
->last_byte
, &len
))
96 l
+= edit_count_lines (edit
, q
, edit
->search
->normal_offset
);
98 book_mark_insert (edit
, l
, BOOK_MARK_FOUND_COLOR
);
102 q
= edit
->search
->normal_offset
+ 1;
106 /* in response to number of bookmarks added because of string being found %d times */
107 message (D_NORMAL
, _("Search"), _(" %d items found, %d bookmarks added "), found
, books
);
109 edit_error_dialog (_ ("Search"), _ (" Search string not found "));
114 edit_search_cmd_callback(const void *user_data
, gsize char_offset
)
116 return edit_get_byte ((WEdit
* )user_data
, (long) char_offset
);
119 void edit_help_cmd (WEdit
* edit
)
121 interactive_display (NULL
, "[Internal File Editor]");
122 edit
->force
|= REDRAW_COMPLETELY
;
125 void edit_refresh_cmd (WEdit
* edit
)
133 edit_get_syntax_color (edit
, -1, &color
);
136 #endif /* !HAVE_SLANG */
141 /* If 0 (quick save) then a) create/truncate <filename> file,
142 b) save to <filename>;
143 if 1 (safe save) then a) save to <tempnam>,
144 b) rename <tempnam> to <filename>;
145 if 2 (do backups) then a) save to <tempnam>,
146 b) rename <filename> to <filename.backup_ext>,
147 c) rename <tempnam> to <filename>. */
149 /* returns 0 on error, -1 on abort */
151 edit_save_file (WEdit
*edit
, const char *filename
)
157 gchar
*real_filename
;
158 int this_save_mode
, fd
= -1;
165 if (*filename
!= PATH_SEP
&& edit
->dir
) {
166 real_filename
= concat_dir_and_file (edit
->dir
, filename
);
168 real_filename
= g_strdup(filename
);
171 this_save_mode
= option_save_mode
;
172 if (this_save_mode
!= EDIT_QUICK_SAVE
) {
173 if (!vfs_file_is_local (real_filename
) ||
174 (fd
= mc_open (real_filename
, O_RDONLY
| O_BINARY
)) == -1) {
176 * The file does not exists yet, so no safe save or
177 * backup are necessary.
179 this_save_mode
= EDIT_QUICK_SAVE
;
185 if (this_save_mode
== EDIT_QUICK_SAVE
&&
186 !edit
->skip_detach_prompt
) {
190 rv
= mc_stat (real_filename
, &sb
);
191 if (rv
== 0 && sb
.st_nlink
> 1) {
192 rv
= edit_query_dialog3 (_("Warning"),
193 _(" File has hard-links. Detach before saving? "),
194 _("&Yes"), _("&No"), _("&Cancel"));
197 this_save_mode
= EDIT_SAFE_SAVE
;
200 edit
->skip_detach_prompt
= 1;
203 g_free(real_filename
);
208 /* Prevent overwriting changes from other editor sessions. */
209 if (rv
== 0 && edit
->stat1
.st_mtime
!= 0 && edit
->stat1
.st_mtime
!= sb
.st_mtime
) {
211 /* The default action is "Cancel". */
214 rv
= edit_query_dialog2 (
216 _("The file has been modified in the meantime. Save anyway?"),
220 g_free(real_filename
);
226 if (this_save_mode
!= EDIT_QUICK_SAVE
) {
227 char *savedir
, *saveprefix
;
228 const char *slashpos
;
229 slashpos
= strrchr (real_filename
, PATH_SEP
);
231 savedir
= g_strdup (real_filename
);
232 savedir
[slashpos
- real_filename
+ 1] = '\0';
234 savedir
= g_strdup (".");
235 saveprefix
= concat_dir_and_file (savedir
, "cooledit");
237 fd
= mc_mkstemps (&savename
, saveprefix
, NULL
);
240 g_free(real_filename
);
244 * Close for now because mc_mkstemps use pure open system call
245 * to create temporary file and it needs to be reopened by
246 * VFS-aware mc_open().
250 savename
= g_strdup (real_filename
);
252 mc_chown (savename
, edit
->stat1
.st_uid
, edit
->stat1
.st_gid
);
253 mc_chmod (savename
, edit
->stat1
.st_mode
);
256 mc_open (savename
, O_CREAT
| O_WRONLY
| O_TRUNC
| O_BINARY
,
257 edit
->stat1
.st_mode
)) == -1)
261 if ((p
= edit_get_write_filter (savename
, real_filename
))) {
265 file
= (FILE *) popen (p
, "w");
268 filelen
= edit_write_stream (edit
, file
);
272 if (pclose (file
) != 0) {
273 tmp
= g_strconcat (_(" Error writing to pipe: "),
274 p
, " ", (char *) NULL
);
275 edit_error_dialog (_("Error"), tmp
);
282 tmp
= g_strconcat (_(" Cannot open pipe for writing: "),
283 p
, " ", (char *) NULL
);
285 edit_error_dialog (_("Error"),
286 get_sys_error (tmp
));
295 filelen
= edit
->last_byte
;
296 while (buf
<= (edit
->curs1
>> S_EDIT_BUF_SIZE
) - 1) {
297 if (mc_write (fd
, (char *) edit
->buffers1
[buf
], EDIT_BUF_SIZE
)
305 (fd
, (char *) edit
->buffers1
[buf
],
306 edit
->curs1
& M_EDIT_BUF_SIZE
) !=
307 (edit
->curs1
& M_EDIT_BUF_SIZE
)) {
309 } else if (edit
->curs2
) {
311 buf
= (edit
->curs2
>> S_EDIT_BUF_SIZE
);
314 (char *) edit
->buffers2
[buf
] + EDIT_BUF_SIZE
-
315 (edit
->curs2
& M_EDIT_BUF_SIZE
) - 1,
316 1 + (edit
->curs2
& M_EDIT_BUF_SIZE
)) !=
317 1 + (edit
->curs2
& M_EDIT_BUF_SIZE
)) {
322 (fd
, (char *) edit
->buffers2
[buf
],
323 EDIT_BUF_SIZE
) != EDIT_BUF_SIZE
) {
334 /* Update the file information, especially the mtime. */
335 if (mc_stat (savename
, &edit
->stat1
) == -1)
339 if (filelen
!= edit
->last_byte
)
342 if (this_save_mode
== EDIT_DO_BACKUP
) {
343 assert (option_backup_ext
!= NULL
);
344 tmp
= g_strconcat (real_filename
, option_backup_ext
,(char *) NULL
);
345 if (mc_rename (real_filename
, tmp
) == -1){
351 if (this_save_mode
!= EDIT_QUICK_SAVE
)
352 if (mc_rename (savename
, real_filename
) == -1)
355 g_free(real_filename
);
358 /* FIXME: Is this safe ?
359 * if (this_save_mode != EDIT_QUICK_SAVE)
360 * mc_unlink (savename);
362 g_free(real_filename
);
367 void menu_save_mode_cmd (void)
371 static char *str_result
;
372 static int save_mode_new
;
373 static const char *str
[] =
377 N_("Do backups -->")};
379 static QuickWidget widgets
[] =
381 {quick_button
, 18, DLG_X
, 7, DLG_Y
, N_("&Cancel"), 0,
382 B_CANCEL
, 0, 0, NULL
, NULL
, NULL
},
383 {quick_button
, 6, DLG_X
, 7, DLG_Y
, N_("&OK"), 0,
384 B_ENTER
, 0, 0, NULL
, NULL
, NULL
},
385 {quick_input
, 23, DLG_X
, 5, DLG_Y
, 0, 9,
386 0, 0, &str_result
, "edit-backup-ext", NULL
, NULL
},
387 {quick_label
, 22, DLG_X
, 4, DLG_Y
, N_("Extension:"), 0,
388 0, 0, 0, NULL
, NULL
, NULL
},
389 {quick_radio
, 4, DLG_X
, 3, DLG_Y
, "", 3,
390 0, &save_mode_new
, (char **) str
, NULL
, NULL
, NULL
},
392 static QuickDialog dialog
=
393 {DLG_X
, DLG_Y
, -1, -1, N_(" Edit Save Mode "), "[Edit Save Mode]",
395 static int i18n_flag
= 0;
403 /* OK/Cancel buttons */
404 l1
= str_term_width1 (_(widgets
[0].text
)) + str_term_width1 (_(widgets
[1].text
)) + 5;
405 maxlen
= max (maxlen
, l1
);
407 for (i
= 0; i
< 3; i
++ ) {
409 maxlen
= max (maxlen
, (size_t) str_term_width1 (str
[i
]) + 7);
413 dlg_x
= maxlen
+ str_term_width1 (_(widgets
[3].text
)) + 5 + 1;
414 widgets
[2].hotkey_pos
= str_term_width1 (_(widgets
[3].text
)); /* input field length */
415 dlg_x
= min (COLS
, dlg_x
);
419 widgets
[1].relative_x
= i
;
420 widgets
[0].relative_x
= i
+ str_term_width1 (_(widgets
[1].text
)) + i
+ 4;
422 widgets
[2].relative_x
= widgets
[3].relative_x
= maxlen
+ 2;
424 for (i
= 0; i
< sizeof (widgets
)/sizeof (widgets
[0]); i
++)
425 widgets
[i
].x_divisions
= dlg_x
;
428 assert (option_backup_ext
!= NULL
);
429 widgets
[2].text
= option_backup_ext
;
430 widgets
[4].value
= option_save_mode
;
431 if (quick_dialog (&dialog
) != B_ENTER
)
433 option_save_mode
= save_mode_new
;
435 g_free (option_backup_ext
);
436 option_backup_ext
= str_result
;
441 edit_set_filename (WEdit
*edit
, const char *f
)
443 g_free (edit
->filename
);
446 edit
->filename
= g_strdup (f
);
447 if (edit
->dir
== NULL
&& *f
!= PATH_SEP
)
449 edit
->dir
= g_strdup (vfs_get_current_dir ());
451 edit
->dir
= g_get_current_dir ();
455 /* Here we want to warn the users of overwriting an existing file,
456 but only if they have made a change to the filename */
457 /* returns 1 on success */
459 edit_save_as_cmd (WEdit
*edit
)
461 /* This heads the 'Save As' dialog box */
464 int different_filename
= 0;
466 exp
= input_expand_dialog (
467 _(" Save As "), _(" Enter file name: "),MC_HISTORY_EDIT_SAVE_AS
, edit
->filename
);
468 edit_push_action (edit
, KEY_PRESS
+ edit
->start_display
);
473 edit
->force
|= REDRAW_COMPLETELY
;
477 if (strcmp (edit
->filename
, exp
)) {
479 different_filename
= 1;
480 if ((file
= mc_open (exp
, O_RDONLY
| O_BINARY
)) != -1) {
481 /* the file exists */
483 /* Overwrite the current file or cancel the operation */
484 if (edit_query_dialog2
486 _(" A file already exists with this name. "),
487 _("&Overwrite"), _("&Cancel"))) {
488 edit
->force
|= REDRAW_COMPLETELY
;
493 save_lock
= edit_lock_file (exp
);
495 /* filenames equal, check if already locked */
496 if (!edit
->locked
&& !edit
->delete_file
)
497 save_lock
= edit_lock_file (exp
);
500 rv
= edit_save_file (edit
, exp
);
503 /* Succesful, so unlock both files */
504 if (different_filename
) {
506 edit_unlock_file (exp
);
508 edit
->locked
= edit_unlock_file (edit
->filename
);
510 if (edit
->locked
|| save_lock
)
511 edit
->locked
= edit_unlock_file (edit
->filename
);
514 edit_set_filename (edit
, exp
);
517 edit
->delete_file
= 0;
518 if (different_filename
)
519 edit_load_syntax (edit
, NULL
, option_syntax_type
);
520 edit
->force
|= REDRAW_COMPLETELY
;
523 edit_error_dialog (_(" Save As "),
525 (" Cannot save file. ")));
528 /* Failed, so maintain modify (not save) lock */
530 edit_unlock_file (exp
);
532 edit
->force
|= REDRAW_COMPLETELY
;
537 edit
->force
|= REDRAW_COMPLETELY
;
541 /* {{{ Macro stuff starts here */
543 /* creates a macro file if it doesn't exist */
544 static FILE *edit_open_macro_file (const char *r
)
549 filename
= concat_dir_and_file (home_dir
, EDIT_MACRO_FILE
);
550 if ((file
= open (filename
, O_CREAT
| O_RDWR
, S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IROTH
)) == -1){
555 fd
= fopen (filename
, r
);
560 #define MAX_MACROS 1024
561 static int saved_macro
[MAX_MACROS
+ 1];
562 static int saved_macros_loaded
= 0;
565 This is just to stop the macro file be loaded over and over for keys
566 that aren't defined to anything. On slow systems this could be annoying.
572 for (i
= 0; i
< MAX_MACROS
&& saved_macro
[i
]; i
++)
573 if (saved_macro
[i
] == k
)
578 /* returns 1 on error */
580 edit_delete_macro (WEdit
* edit
, int k
)
583 struct macro macro
[MAX_MACRO_LENGTH
];
589 if (saved_macros_loaded
)
590 if ((j
= macro_exists (k
)) < 0)
592 tmp
= concat_dir_and_file (home_dir
, EDIT_TEMP_FILE
);
593 g
= fopen (tmp
, "w");
596 edit_error_dialog (_(" Delete macro "),
597 get_sys_error (_(" Cannot open temp file ")));
600 f
= edit_open_macro_file ("r");
602 edit_error_dialog (_(" Delete macro "),
603 get_sys_error (_(" Cannot open macro file ")));
608 n
= fscanf (f
, ("key '%d 0': "), &s
);
612 while (fscanf (f
, "%hd %hd, ", ¯o
[n
].command
, ¯o
[n
].ch
))
616 fprintf (g
, ("key '%d 0': "), s
);
617 for (i
= 0; i
< n
; i
++)
618 fprintf (g
, "%hd %hd, ", macro
[i
].command
, macro
[i
].ch
);
624 tmp
= concat_dir_and_file (home_dir
, EDIT_TEMP_FILE
);
625 tmp2
= concat_dir_and_file (home_dir
, EDIT_MACRO_FILE
);
626 if (rename ( tmp
, tmp2
) == -1) {
627 edit_error_dialog (_(" Delete macro "),
628 get_sys_error (_(" Cannot overwrite macro file ")));
636 if (saved_macros_loaded
)
637 memmove (saved_macro
+ j
, saved_macro
+ j
+ 1, sizeof (int) * (MAX_MACROS
- j
- 1));
641 /* returns 0 on error */
642 int edit_save_macro_cmd (WEdit
* edit
, struct macro macro
[], int n
)
647 edit_push_action (edit
, KEY_PRESS
+ edit
->start_display
);
648 s
= editcmd_dialog_raw_key_query (_(" Save macro "),
649 _(" Press the macro's new hotkey: "), 1);
650 edit
->force
|= REDRAW_COMPLETELY
;
652 if (edit_delete_macro (edit
, s
))
654 f
= edit_open_macro_file ("a+");
656 fprintf (f
, ("key '%d 0': "), s
);
657 for (i
= 0; i
< n
; i
++)
658 fprintf (f
, "%hd %hd, ", macro
[i
].command
, macro
[i
].ch
);
661 if (saved_macros_loaded
) {
662 for (i
= 0; i
< MAX_MACROS
&& saved_macro
[i
]; i
++);
667 edit_error_dialog (_(" Save macro "), get_sys_error (_(" Cannot open macro file ")));
672 void edit_delete_macro_cmd (WEdit
* edit
)
676 command
= editcmd_dialog_raw_key_query (_ (" Delete macro "),
677 _ (" Press macro hotkey: "), 1);
682 edit_delete_macro (edit
, command
);
685 /* return 0 on error */
686 int edit_load_macro_cmd (WEdit
* edit
, struct macro macro
[], int *n
, int k
)
689 int s
, i
= 0, found
= 0;
693 if (saved_macros_loaded
)
694 if (macro_exists (k
) < 0)
697 if ((f
= edit_open_macro_file ("r"))) {
701 u
= fscanf (f
, ("key '%d 0': "), &s
);
704 if (!saved_macros_loaded
)
705 saved_macro
[i
++] = s
;
708 while (*n
< MAX_MACRO_LENGTH
&& 2 == fscanf (f
, "%hd %hd, ", ¯o
[*n
].command
, ¯o
[*n
].ch
))
711 while (2 == fscanf (f
, "%hd %hd, ", &dummy
.command
, &dummy
.ch
));
716 } while (!found
|| !saved_macros_loaded
);
717 if (!saved_macros_loaded
) {
719 saved_macros_loaded
= 1;
724 edit_error_dialog (_(" Load macro "),
725 get_sys_error (_(" Cannot open macro file ")));
729 /* }}} Macro stuff starts here */
731 /* returns 1 on success */
732 int edit_save_confirm_cmd (WEdit
* edit
)
736 if (edit_confirm_save
) {
737 f
= g_strconcat (_(" Confirm save file? : "), edit
->filename
, " ", NULL
);
738 if (edit_query_dialog2 (_(" Save file "), f
, _("&Save"), _("&Cancel"))){
744 return edit_save_cmd (edit
);
748 /* returns 1 on success */
750 edit_save_cmd (WEdit
*edit
)
752 int res
, save_lock
= 0;
754 if (!edit
->locked
&& !edit
->delete_file
)
755 save_lock
= edit_lock_file (edit
->filename
);
756 res
= edit_save_file (edit
, edit
->filename
);
758 /* Maintain modify (not save) lock on failure */
759 if ((res
> 0 && edit
->locked
) || save_lock
)
760 edit
->locked
= edit_unlock_file (edit
->filename
);
762 /* On failure try 'save as', it does locking on its own */
764 return edit_save_as_cmd (edit
);
765 edit
->force
|= REDRAW_COMPLETELY
;
767 edit
->delete_file
= 0;
775 /* returns 1 on success */
776 int edit_new_cmd (WEdit
* edit
)
778 if (edit
->modified
) {
779 if (edit_query_dialog2 (_ ("Warning"), _ (" Current text was modified without a file save. \n Continue discards these changes. "), _ ("C&ontinue"), _ ("&Cancel"))) {
780 edit
->force
|= REDRAW_COMPLETELY
;
784 edit
->force
|= REDRAW_COMPLETELY
;
786 return edit_renew (edit
); /* if this gives an error, something has really screwed up */
789 /* returns 1 on error */
791 edit_load_file_from_filename (WEdit
* edit
, char *exp
)
793 int prev_locked
= edit
->locked
;
794 char *prev_filename
= g_strdup (edit
->filename
);
796 if (!edit_reload (edit
, exp
)) {
797 g_free (prev_filename
);
802 edit_unlock_file (prev_filename
);
803 g_free (prev_filename
);
808 edit_load_syntax_file (WEdit
* edit
)
813 if (geteuid () == 0) {
814 dir
= query_dialog (_("Syntax file edit"),
815 _(" Which syntax file you want to edit? "), D_NORMAL
, 2,
816 _("&User"), _("&System Wide"));
819 extdir
= concat_dir_and_file (mc_home
, "syntax" PATH_SEP_STR
"Syntax");
820 if (!exist_file(extdir
)) {
822 extdir
= concat_dir_and_file (mc_home_alt
, "syntax" PATH_SEP_STR
"Syntax");
828 buffer
= concat_dir_and_file (home_dir
, EDIT_SYNTAX_FILE
);
829 check_for_default (extdir
, buffer
);
830 edit_load_file_from_filename (edit
, buffer
);
833 edit_load_file_from_filename (edit
, extdir
);
839 edit_load_menu_file (WEdit
* edit
)
847 _(" Which menu file do you want to edit? "), D_NORMAL
,
848 geteuid() ? 2 : 3, _("&Local"), _("&User"), _("&System Wide")
851 menufile
= concat_dir_and_file (mc_home
, EDIT_GLOBAL_MENU
);
853 if (!exist_file (menufile
)) {
855 menufile
= concat_dir_and_file (mc_home_alt
, EDIT_GLOBAL_MENU
);
860 buffer
= g_strdup (EDIT_LOCAL_MENU
);
861 check_for_default (menufile
, buffer
);
862 chmod (buffer
, 0600);
866 buffer
= concat_dir_and_file (home_dir
, EDIT_HOME_MENU
);
867 check_for_default (menufile
, buffer
);
871 buffer
= concat_dir_and_file (mc_home
, EDIT_GLOBAL_MENU
);
872 if (!exist_file (buffer
)) {
874 buffer
= concat_dir_and_file (mc_home_alt
, EDIT_GLOBAL_MENU
);
883 edit_load_file_from_filename (edit
, buffer
);
890 edit_load_cmd (WEdit
*edit
, edit_current_file_t what
)
895 && (edit_query_dialog2
897 _(" Current text was modified without a file save. \n"
898 " Continue discards these changes. "),
899 _("C&ontinue"), _("&Cancel")) == 1)) {
900 edit
->force
|= REDRAW_COMPLETELY
;
905 case EDIT_FILE_COMMON
:
906 exp
= input_expand_dialog (_(" Load "), _(" Enter file name: "),
907 MC_HISTORY_EDIT_LOAD
, edit
->filename
);
911 edit_load_file_from_filename (edit
, exp
);
916 case EDIT_FILE_SYNTAX
:
917 edit_load_syntax_file (edit
);
921 edit_load_menu_file (edit
);
928 edit
->force
|= REDRAW_COMPLETELY
;
933 if mark2 is -1 then marking is from mark1 to the cursor.
934 Otherwise its between the markers. This handles this.
935 Returns 1 if no text is marked.
937 int eval_marks (WEdit
* edit
, long *start_mark
, long *end_mark
)
939 if (edit
->mark1
!= edit
->mark2
) {
940 if (edit
->mark2
>= 0) {
941 *start_mark
= min (edit
->mark1
, edit
->mark2
);
942 *end_mark
= max (edit
->mark1
, edit
->mark2
);
944 *start_mark
= min (edit
->mark1
, edit
->curs1
);
945 *end_mark
= max (edit
->mark1
, edit
->curs1
);
946 edit
->column2
= edit
->curs_col
;
950 *start_mark
= *end_mark
= 0;
951 edit
->column2
= edit
->column1
= 0;
956 #define space_width 1
959 edit_insert_column_of_text (WEdit
* edit
, unsigned char *data
, int size
, int width
)
963 cursor
= edit
->curs1
;
964 col
= edit_get_col (edit
);
965 for (i
= 0; i
< size
; i
++) {
966 if (data
[i
] == '\n') { /* fill in and move to next line */
969 if (edit_get_byte (edit
, edit
->curs1
) != '\n') {
970 l
= width
- (edit_get_col (edit
) - col
);
972 edit_insert (edit
, ' ');
976 for (p
= edit
->curs1
;; p
++) {
977 if (p
== edit
->last_byte
) {
978 edit_cursor_move (edit
, edit
->last_byte
- edit
->curs1
);
979 edit_insert_ahead (edit
, '\n');
983 if (edit_get_byte (edit
, p
) == '\n') {
988 edit_cursor_move (edit
, edit_move_forward3 (edit
, p
, col
, 0) - edit
->curs1
);
989 l
= col
- edit_get_col (edit
);
990 while (l
>= space_width
) {
991 edit_insert (edit
, ' ');
996 edit_insert (edit
, data
[i
]);
998 edit_cursor_move (edit
, cursor
- edit
->curs1
);
1001 #define TEMP_BUF_LEN 1024
1004 edit_insert_column_of_text_from_file (WEdit
* edit
, int file
)
1008 int blocklen
= -1, width
;
1009 unsigned char *data
;
1010 cursor
= edit
->curs1
;
1011 col
= edit_get_col (edit
);
1012 data
= g_malloc (TEMP_BUF_LEN
);
1013 while ((blocklen
= mc_read (file
, (char *) data
, TEMP_BUF_LEN
)) > 0) {
1014 for (width
= 0; width
< blocklen
; width
++) {
1015 if (data
[width
] == '\n')
1018 for (i
= 0; i
< blocklen
; i
++) {
1019 if (data
[i
] == '\n') { /* fill in and move to next line */
1022 if (edit_get_byte (edit
, edit
->curs1
) != '\n') {
1023 l
= width
- (edit_get_col (edit
) - col
);
1025 edit_insert (edit
, ' ');
1029 for (p
= edit
->curs1
;; p
++) {
1030 if (p
== edit
->last_byte
) {
1031 edit_cursor_move (edit
, edit
->last_byte
- edit
->curs1
);
1032 edit_insert_ahead (edit
, '\n');
1036 if (edit_get_byte (edit
, p
) == '\n') {
1041 edit_cursor_move (edit
, edit_move_forward3 (edit
, p
, col
, 0) - edit
->curs1
);
1042 l
= col
- edit_get_col (edit
);
1043 while (l
>= space_width
) {
1044 edit_insert (edit
, ' ');
1049 edit_insert (edit
, data
[i
]);
1052 edit_cursor_move (edit
, cursor
- edit
->curs1
);
1054 edit
->force
|= REDRAW_PAGE
;
1059 edit_block_copy_cmd (WEdit
*edit
)
1061 long start_mark
, end_mark
, current
= edit
->curs1
;
1063 unsigned char *copy_buf
;
1065 edit_update_curs_col (edit
);
1066 if (eval_marks (edit
, &start_mark
, &end_mark
))
1069 copy_buf
= edit_get_block (edit
, start_mark
, end_mark
, &size
);
1071 /* all that gets pushed are deletes hence little space is used on the stack */
1073 edit_push_markers (edit
);
1075 if (column_highlighting
) {
1076 edit_insert_column_of_text (edit
, copy_buf
, size
,
1077 abs (edit
->column2
- edit
->column1
));
1080 edit_insert_ahead (edit
, copy_buf
[size
]);
1084 edit_scroll_screen_over_cursor (edit
);
1086 if (column_highlighting
) {
1087 edit_set_markers (edit
, 0, 0, 0, 0);
1088 edit_push_action (edit
, COLUMN_ON
);
1089 column_highlighting
= 0;
1090 } else if (start_mark
< current
&& end_mark
> current
)
1091 edit_set_markers (edit
, start_mark
,
1092 end_mark
+ end_mark
- start_mark
, 0, 0);
1094 edit
->force
|= REDRAW_PAGE
;
1099 edit_block_move_cmd (WEdit
*edit
)
1103 unsigned char *copy_buf
;
1104 long start_mark
, end_mark
;
1108 if (eval_marks (edit
, &start_mark
, &end_mark
))
1110 if (column_highlighting
) {
1111 edit_update_curs_col (edit
);
1113 if (start_mark
<= edit
->curs1
&& end_mark
>= edit
->curs1
)
1114 if ((x
> edit
->column1
&& x
< edit
->column2
)
1115 || (x
> edit
->column2
&& x
< edit
->column1
))
1117 } else if (start_mark
<= edit
->curs1
&& end_mark
>= edit
->curs1
)
1120 if ((end_mark
- start_mark
) > option_max_undo
/ 2)
1121 if (edit_query_dialog2
1124 (" Block is large, you may not be able to undo this action. "),
1125 _("C&ontinue"), _("&Cancel")))
1128 edit_push_markers (edit
);
1129 current
= edit
->curs1
;
1130 if (column_highlighting
) {
1131 int size
, c1
, c2
, line
;
1132 line
= edit
->curs_line
;
1133 if (edit
->mark2
< 0)
1134 edit_mark_cmd (edit
, 0);
1135 c1
= min (edit
->column1
, edit
->column2
);
1136 c2
= max (edit
->column1
, edit
->column2
);
1137 copy_buf
= edit_get_block (edit
, start_mark
, end_mark
, &size
);
1139 edit_block_delete_cmd (edit
);
1142 edit_move_to_line (edit
, line
);
1143 edit_cursor_move (edit
,
1144 edit_move_forward3 (edit
,
1145 edit_bol (edit
, edit
->curs1
),
1146 x
, 0) - edit
->curs1
);
1147 edit_insert_column_of_text (edit
, copy_buf
, size
, c2
- c1
);
1149 line
= edit
->curs_line
;
1150 edit_update_curs_col (edit
);
1152 edit_block_delete_cmd (edit
);
1153 edit_move_to_line (edit
, line
);
1154 edit_cursor_move (edit
,
1155 edit_move_forward3 (edit
,
1158 x
, 0) - edit
->curs1
);
1160 edit_set_markers (edit
, 0, 0, 0, 0);
1161 edit_push_action (edit
, COLUMN_ON
);
1162 column_highlighting
= 0;
1164 copy_buf
= g_malloc (end_mark
- start_mark
);
1165 edit_cursor_move (edit
, start_mark
- edit
->curs1
);
1166 edit_scroll_screen_over_cursor (edit
);
1168 while (count
< end_mark
) {
1169 copy_buf
[end_mark
- count
- 1] = edit_delete (edit
, 1);
1172 edit_scroll_screen_over_cursor (edit
);
1173 edit_cursor_move (edit
,
1174 current
- edit
->curs1
-
1175 (((current
- edit
->curs1
) >
1176 0) ? end_mark
- start_mark
: 0));
1177 edit_scroll_screen_over_cursor (edit
);
1178 while (count
-- > start_mark
)
1179 edit_insert_ahead (edit
, copy_buf
[end_mark
- count
- 1]);
1180 edit_set_markers (edit
, edit
->curs1
,
1181 edit
->curs1
+ end_mark
- start_mark
, 0, 0);
1183 edit_scroll_screen_over_cursor (edit
);
1185 edit
->force
|= REDRAW_PAGE
;
1189 edit_delete_column_of_text (WEdit
* edit
)
1191 long p
, q
, r
, m1
, m2
;
1195 eval_marks (edit
, &m1
, &m2
);
1196 n
= edit_move_forward (edit
, m1
, 0, m2
) + 1;
1197 c
= edit_move_forward3 (edit
, edit_bol (edit
, m1
), 0, m1
);
1198 d
= edit_move_forward3 (edit
, edit_bol (edit
, m2
), 0, m2
);
1204 r
= edit_bol (edit
, edit
->curs1
);
1205 p
= edit_move_forward3 (edit
, r
, b
, 0);
1206 q
= edit_move_forward3 (edit
, r
, c
, 0);
1211 edit_cursor_move (edit
, p
- edit
->curs1
);
1212 while (q
> p
) { /* delete line between margins */
1213 if (edit_get_byte (edit
, edit
->curs1
) != '\n')
1214 edit_delete (edit
, 1);
1217 if (n
) /* move to next line except on the last delete */
1218 edit_cursor_move (edit
, edit_move_forward (edit
, edit
->curs1
, 1, 0) - edit
->curs1
);
1222 /* if success return 0 */
1224 edit_block_delete (WEdit
*edit
)
1227 long start_mark
, end_mark
;
1228 if (eval_marks (edit
, &start_mark
, &end_mark
))
1230 if (column_highlighting
&& edit
->mark2
< 0)
1231 edit_mark_cmd (edit
, 0);
1232 if ((end_mark
- start_mark
) > option_max_undo
/ 2) {
1233 /* Warning message with a query to continue or cancel the operation */
1234 if (edit_query_dialog2
1237 (" Block is large, you may not be able to undo this action. "),
1238 _("C&ontinue"), _("&Cancel"))) {
1242 edit_push_markers (edit
);
1243 edit_cursor_move (edit
, start_mark
- edit
->curs1
);
1244 edit_scroll_screen_over_cursor (edit
);
1246 if (start_mark
< end_mark
) {
1247 if (column_highlighting
) {
1248 if (edit
->mark2
< 0)
1249 edit_mark_cmd (edit
, 0);
1250 edit_delete_column_of_text (edit
);
1252 while (count
< end_mark
) {
1253 edit_delete (edit
, 1);
1258 edit_set_markers (edit
, 0, 0, 0, 0);
1259 edit
->force
|= REDRAW_PAGE
;
1263 /* returns 1 if canceelled by user */
1264 int edit_block_delete_cmd (WEdit
* edit
)
1266 long start_mark
, end_mark
;
1267 if (eval_marks (edit
, &start_mark
, &end_mark
)) {
1268 edit_delete_line (edit
);
1271 return edit_block_delete (edit
);
1274 #define INPUT_INDEX 9
1277 editcmd_find (WEdit
*edit
, gsize
*len
)
1279 gsize search_start
= edit
->search_start
;
1282 if (edit
->replace_backwards
) {
1283 search_end
= edit
->curs1
-1;
1284 while ((int) search_start
>= 0) {
1285 if (search_end
- search_start
> edit
->search
->original_len
&& mc_search_is_fixed_search_str(edit
->search
))
1286 search_end
= search_start
+ edit
->search
->original_len
+1;
1287 if ( mc_search_run(edit
->search
, (void *) edit
, search_start
, search_end
, len
))
1293 edit
->search
->error_str
= g_strdup(_(" Search string not found "));
1295 return mc_search_run(edit
->search
, (void *) edit
, edit
->search_start
, edit
->last_byte
, len
);
1301 /* thanks to Liviu Daia <daia@stoilow.imar.ro> for getting this
1302 (and the above) routines to work properly - paul */
1304 #define is_digit(x) ((x) >= '0' && (x) <= '9')
1307 edit_replace_cmd__conv_to_display(char *str
)
1311 tmp
= str_convert_to_display (str
);
1313 if (tmp
&& tmp
->len
){
1317 g_string_free (tmp
, FALSE
);
1320 return g_strdup(str
);
1325 edit_replace_cmd__conv_to_input(char *str
)
1329 tmp
= str_convert_to_input (str
);
1331 if (tmp
&& tmp
->len
){
1335 g_string_free (tmp
, FALSE
);
1338 return g_strdup(str
);
1341 /* call with edit = 0 before shutdown to close memory leaks */
1343 edit_replace_cmd (WEdit
*edit
, int again
)
1345 /* 1 = search string, 2 = replace with */
1346 static char *saved1
= NULL
; /* saved default[123] */
1347 static char *saved2
= NULL
;
1348 char *input1
= NULL
; /* user input from the dialog */
1349 char *input2
= NULL
;
1350 char *str_for_prompt_dialog
= NULL
;
1352 long times_replaced
= 0, last_search
;
1353 gboolean once_found
= FALSE
;
1356 g_free (saved1
), saved1
= NULL
;
1357 g_free (saved2
), saved2
= NULL
;
1361 last_search
= edit
->last_byte
;
1363 edit
->force
|= REDRAW_COMPLETELY
;
1365 if (again
&& !saved1
&& !saved2
)
1369 input1
= g_strdup (saved1
? saved1
: "");
1370 input2
= g_strdup (saved2
? saved2
: "");
1372 char *disp1
= edit_replace_cmd__conv_to_display(g_strdup (saved1
? saved1
: ""));
1373 char *disp2
= edit_replace_cmd__conv_to_display(g_strdup (saved2
? saved2
: ""));
1375 edit_push_action (edit
, KEY_PRESS
+ edit
->start_display
);
1377 editcmd_dialog_replace_show (edit
, disp1
, disp2
, &input1
, &input2
);
1382 str_for_prompt_dialog
= g_strdup(input2
);
1384 if (input1
== NULL
|| *input1
== '\0') {
1385 edit
->force
= REDRAW_COMPLETELY
;
1389 input1
= edit_replace_cmd__conv_to_input(input1
);
1390 input2
= edit_replace_cmd__conv_to_input(input2
);
1392 g_free (saved1
), saved1
= g_strdup (input1
);
1393 g_free (saved2
), saved2
= g_strdup (input2
);
1397 mc_search_free(edit
->search
);
1398 edit
->search
= NULL
;
1404 edit
->search
= mc_search_new(input1
, -1);
1405 if (edit
->search
== NULL
)
1407 edit
->search_start
= edit
->curs1
;
1410 edit
->search
->search_type
= edit
->search_type
;
1411 edit
->search
->is_all_charsets
= edit
->all_codepages
;
1412 edit
->search
->is_case_sentitive
= edit
->replace_case
;
1413 edit
->search
->search_fn
= edit_search_cmd_callback
;
1416 if (edit
->found_len
&& edit
->search_start
== edit
->found_start
+ 1
1417 && edit
->replace_backwards
)
1418 edit
->search_start
--;
1420 if (edit
->found_len
&& edit
->search_start
== edit
->found_start
- 1
1421 && !edit
->replace_backwards
)
1422 edit
->search_start
++;
1428 if (! editcmd_find(edit
, &len
))
1430 if (!(edit
->search
->error
== MC_SEARCH_E_OK
|| (once_found
&& edit
->search
->error
== MC_SEARCH_E_NOTFOUND
)))
1432 edit_error_dialog (_ ("Search"), edit
->search
->error_str
);
1437 new_start
= edit
->search
->normal_offset
;
1439 edit
->search_start
= new_start
= edit
->search
->normal_offset
;
1440 /*returns negative on not found or error in pattern */
1442 if (edit
->search_start
>= 0) {
1445 edit
->found_start
= edit
->search_start
;
1446 i
= edit
->found_len
= len
;
1448 edit_cursor_move (edit
, edit
->search_start
- edit
->curs1
);
1449 edit_scroll_screen_over_cursor (edit
);
1453 if (edit
->replace_mode
==0) {
1455 l
= edit
->curs_row
- edit
->num_widget_lines
/ 3;
1457 edit_scroll_downward (edit
, l
);
1459 edit_scroll_upward (edit
, -l
);
1461 edit_scroll_screen_over_cursor (edit
);
1462 edit
->force
|= REDRAW_PAGE
;
1463 edit_render_keypress (edit
);
1465 /*so that undo stops at each query */
1466 edit_push_key_press (edit
);
1468 switch (editcmd_dialog_replace_prompt_show (edit
, str_for_prompt_dialog
, /* and prompt 2/3 down */
1473 case B_SKIP_REPLACE
:
1477 edit
->replace_mode
=1;
1486 if (replace_yes
) { /* delete then insert new */
1487 GString
*repl_str
, *tmp_str
;
1488 tmp_str
= g_string_new(input2
);
1490 repl_str
= mc_search_prepare_replace_str (edit
->search
, tmp_str
);
1491 g_string_free(tmp_str
, TRUE
);
1492 if (edit
->search
->error
!= MC_SEARCH_E_OK
)
1494 edit_error_dialog (_ ("Replace"), edit
->search
->error_str
);
1499 edit_delete (edit
, 1);
1501 while (++i
< repl_str
->len
)
1502 edit_insert (edit
, repl_str
->str
[i
]);
1504 g_string_free(repl_str
, TRUE
);
1505 edit
->found_len
= i
;
1507 /* so that we don't find the same string again */
1508 if (edit
->replace_backwards
) {
1509 last_search
= edit
->search_start
;
1510 edit
->search_start
--;
1512 edit
->search_start
+= i
;
1513 last_search
= edit
->last_byte
;
1515 edit_scroll_screen_over_cursor (edit
);
1517 const char *msg
= _(" Replace ");
1518 /* try and find from right here for next search */
1519 edit
->search_start
= edit
->curs1
;
1520 edit_update_curs_col (edit
);
1522 edit
->force
|= REDRAW_PAGE
;
1523 edit_render_keypress (edit
);
1524 if (times_replaced
) {
1525 message (D_NORMAL
, msg
, _(" %ld replacements made. "),
1528 query_dialog (msg
, _(" Search string not found "),
1529 D_NORMAL
, 1, _("&OK"));
1530 edit
->replace_mode
= -1;
1532 } while (edit
->replace_mode
>0);
1534 edit
->force
= REDRAW_COMPLETELY
;
1535 edit_scroll_screen_over_cursor (edit
);
1537 g_free (str_for_prompt_dialog
);
1543 void edit_search_cmd (WEdit
* edit
, int again
)
1545 char *search_string
= NULL
, *search_string_dup
= NULL
;
1552 if (edit
->search
!= NULL
)
1553 search_string_dup
= search_string
= g_strndup(edit
->search
->original
, edit
->search
->original_len
);
1559 if (search_string
&& *search_string
)
1561 tmp
= str_convert_to_display (search_string
);
1563 g_free(search_string_dup
);
1564 search_string_dup
= NULL
;
1566 if (tmp
&& tmp
->len
)
1567 search_string
= search_string_dup
= tmp
->str
;
1568 g_string_free (tmp
, FALSE
);
1570 #endif /* HAVE_CHARSET */
1571 editcmd_dialog_search_show (edit
, &search_string
);
1573 if (search_string
&& *search_string
)
1575 tmp
= str_convert_to_input (search_string
);
1576 if (tmp
&& tmp
->len
)
1577 search_string
= tmp
->str
;
1579 g_string_free (tmp
, FALSE
);
1581 if (search_string_dup
)
1582 g_free(search_string_dup
);
1584 #endif /* HAVE_CHARSET */
1586 edit_push_action (edit
, KEY_PRESS
+ edit
->start_display
);
1590 edit
->force
|= REDRAW_COMPLETELY
;
1591 edit_scroll_screen_over_cursor (edit
);
1597 mc_search_free(edit
->search
);
1598 edit
->search
= NULL
;
1604 edit
->search
= mc_search_new(search_string
, -1);
1605 if (edit
->search
== NULL
)
1607 edit
->search_start
= edit
->curs1
;
1610 edit
->search
->search_type
= edit
->search_type
;
1611 edit
->search
->is_all_charsets
= edit
->all_codepages
;
1612 edit
->search
->is_case_sentitive
= edit
->replace_case
;
1613 edit
->search
->search_fn
= edit_search_cmd_callback
;
1616 if (search_create_bookmark
)
1618 edit_search_cmd_search_create_bookmark(edit
);
1622 if (edit
->found_len
&& edit
->search_start
== edit
->found_start
+ 1 && edit
->replace_backwards
)
1623 edit
->search_start
--;
1625 if (edit
->found_len
&& edit
->search_start
== edit
->found_start
- 1 && !edit
->replace_backwards
)
1626 edit
->search_start
++;
1629 if (editcmd_find(edit
, &len
))
1631 edit
->found_start
= edit
->search_start
= edit
->search
->normal_offset
;
1632 edit
->found_len
= len
;
1634 edit_cursor_move (edit
, edit
->search_start
- edit
->curs1
);
1635 edit_scroll_screen_over_cursor (edit
);
1636 if (edit
->replace_backwards
)
1637 edit
->search_start
--;
1639 edit
->search_start
++;
1643 edit
->search_start
= edit
->curs1
;
1644 if (edit
->search
->error_str
)
1645 edit_error_dialog (_ ("Search"), edit
->search
->error_str
);
1649 edit
->force
|= REDRAW_COMPLETELY
;
1650 edit_scroll_screen_over_cursor (edit
);
1655 * Check if it's OK to close the editor. If there are unsaved changes,
1656 * ask user. Return 1 if it's OK to exit, 0 to continue editing.
1659 edit_ok_to_exit (WEdit
*edit
)
1661 if (!edit
->modified
)
1664 switch (edit_query_dialog3
1665 (_("Quit"), _(" File was modified, Save with exit? "),
1666 _("&Cancel quit"), _("&Yes"), _("&No"))) {
1668 edit_push_markers (edit
);
1669 edit_set_markers (edit
, 0, 0, 0, 0);
1670 if (!edit_save_cmd (edit
))
1683 /* Return a null terminated length of text. Result must be g_free'd */
1684 static unsigned char *
1685 edit_get_block (WEdit
*edit
, long start
, long finish
, int *l
)
1687 unsigned char *s
, *r
;
1688 r
= s
= g_malloc (finish
- start
+ 1);
1689 if (column_highlighting
) {
1691 /* copy from buffer, excluding chars that are out of the column 'margins' */
1692 while (start
< finish
) {
1694 x
= edit_move_forward3 (edit
, edit_bol (edit
, start
), 0,
1696 c
= edit_get_byte (edit
, start
);
1697 if ((x
>= edit
->column1
&& x
< edit
->column2
)
1698 || (x
>= edit
->column2
&& x
< edit
->column1
) || c
== '\n') {
1705 *l
= finish
- start
;
1706 while (start
< finish
)
1707 *s
++ = edit_get_byte (edit
, start
++);
1713 /* save block, returns 1 on success */
1715 edit_save_block (WEdit
* edit
, const char *filename
, long start
,
1721 mc_open (filename
, O_CREAT
| O_WRONLY
| O_TRUNC
,
1722 S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IROTH
| O_BINARY
)) == -1)
1725 if (column_highlighting
) {
1727 r
= mc_write (file
, VERTICAL_MAGIC
, sizeof(VERTICAL_MAGIC
));
1729 unsigned char *block
, *p
;
1730 p
= block
= edit_get_block (edit
, start
, finish
, &len
);
1732 r
= mc_write (file
, p
, len
);
1743 len
= finish
- start
;
1744 buf
= g_malloc (TEMP_BUF_LEN
);
1745 while (start
!= finish
) {
1746 end
= min (finish
, start
+ TEMP_BUF_LEN
);
1747 for (; i
< end
; i
++)
1748 buf
[i
- start
] = edit_get_byte (edit
, i
);
1749 len
-= mc_write (file
, (char *) buf
, end
- start
);
1760 /* copies a block to clipboard file */
1761 static int edit_save_block_to_clip_file (WEdit
* edit
, long start
, long finish
)
1765 tmp
= concat_dir_and_file (home_dir
, EDIT_CLIP_FILE
);
1766 ret
= edit_save_block (edit
, tmp
, start
, finish
);
1772 void edit_paste_from_history (WEdit
*edit
)
1775 edit_error_dialog (_(" Error "), _(" This function is not implemented. "));
1778 int edit_copy_to_X_buf_cmd (WEdit
* edit
)
1780 long start_mark
, end_mark
;
1781 if (eval_marks (edit
, &start_mark
, &end_mark
))
1783 if (!edit_save_block_to_clip_file (edit
, start_mark
, end_mark
)) {
1784 edit_error_dialog (_(" Copy to clipboard "), get_sys_error (_(" Unable to save to file. ")));
1787 edit_mark_cmd (edit
, 1);
1791 int edit_cut_to_X_buf_cmd (WEdit
* edit
)
1793 long start_mark
, end_mark
;
1794 if (eval_marks (edit
, &start_mark
, &end_mark
))
1796 if (!edit_save_block_to_clip_file (edit
, start_mark
, end_mark
)) {
1797 edit_error_dialog (_(" Cut to clipboard "), _(" Unable to save to file. "));
1800 edit_block_delete_cmd (edit
);
1801 edit_mark_cmd (edit
, 1);
1805 void edit_paste_from_X_buf_cmd (WEdit
* edit
)
1808 tmp
= concat_dir_and_file (home_dir
, EDIT_CLIP_FILE
);
1809 edit_insert_file (edit
, tmp
);
1815 * Ask user for the line and go to that line.
1816 * Negative numbers mean line from the end (i.e. -1 is the last line).
1819 edit_goto_cmd (WEdit
*edit
)
1822 static long line
= 0; /* line as typed, saved as default */
1827 g_snprintf (s
, sizeof (s
), "%ld", line
);
1828 f
= input_dialog (_(" Goto line "), _(" Enter line: "), MC_HISTORY_EDIT_GOTO_LINE
,
1838 l
= strtol (f
, &error
, 0);
1846 l
= edit
->total_lines
+ l
+ 2;
1847 edit_move_display (edit
, l
- edit
->num_widget_lines
/ 2 - 1);
1848 edit_move_to_line (edit
, l
- 1);
1849 edit
->force
|= REDRAW_COMPLETELY
;
1854 /* Return 1 on success */
1856 edit_save_block_cmd (WEdit
*edit
)
1858 long start_mark
, end_mark
;
1861 if (eval_marks (edit
, &start_mark
, &end_mark
))
1864 tmp
= concat_dir_and_file (home_dir
, EDIT_CLIP_FILE
);
1866 input_expand_dialog (_(" Save Block "), _(" Enter file name: "),
1867 MC_HISTORY_EDIT_SAVE_BLOCK
,
1870 edit_push_action (edit
, KEY_PRESS
+ edit
->start_display
);
1876 if (edit_save_block (edit
, exp
, start_mark
, end_mark
)) {
1878 edit
->force
|= REDRAW_COMPLETELY
;
1882 edit_error_dialog (_(" Save Block "),
1884 (" Cannot save file. ")));
1888 edit
->force
|= REDRAW_COMPLETELY
;
1893 /* returns 1 on success */
1895 edit_insert_file_cmd (WEdit
*edit
)
1900 tmp
= concat_dir_and_file (home_dir
, EDIT_CLIP_FILE
);
1901 exp
= input_expand_dialog (_(" Insert File "), _(" Enter file name: "),
1902 MC_HISTORY_EDIT_INSERT_FILE
,
1905 edit_push_action (edit
, KEY_PRESS
+ edit
->start_display
);
1911 if (edit_insert_file (edit
, exp
)) {
1913 edit
->force
|= REDRAW_COMPLETELY
;
1917 edit_error_dialog (_(" Insert File "),
1919 (" Cannot insert file. ")));
1923 edit
->force
|= REDRAW_COMPLETELY
;
1927 /* sorts a block, returns -1 on system fail, 1 on cancel and 0 on success */
1928 int edit_sort_cmd (WEdit
* edit
)
1930 static char *old
= 0;
1932 long start_mark
, end_mark
;
1935 if (eval_marks (edit
, &start_mark
, &end_mark
)) {
1936 edit_error_dialog (_(" Sort block "), _(" You must first highlight a block of text. "));
1940 tmp
= concat_dir_and_file (home_dir
, EDIT_BLOCK_FILE
);
1941 edit_save_block (edit
, tmp
, start_mark
, end_mark
);
1944 exp
= input_dialog (_(" Run Sort "),
1945 _(" Enter sort options (see manpage) separated by whitespace: "),
1946 MC_HISTORY_EDIT_SORT
, (old
!= NULL
) ? old
: "");
1952 tmp
= g_strconcat (" sort ", exp
, " ", home_dir
, PATH_SEP_STR EDIT_BLOCK_FILE
, " > ",
1953 home_dir
, PATH_SEP_STR EDIT_TEMP_FILE
, (char *) NULL
);
1957 if (e
== -1 || e
== 127) {
1958 edit_error_dialog (_(" Sort "),
1959 get_sys_error (_(" Cannot execute sort command ")));
1962 sprintf (q
, "%d ", e
);
1963 tmp
= g_strconcat (_(" Sort returned non-zero: "), q
, (char *) NULL
);
1964 edit_error_dialog (_(" Sort "), tmp
);
1970 edit
->force
|= REDRAW_COMPLETELY
;
1972 if (edit_block_delete_cmd (edit
))
1974 tmp
= concat_dir_and_file (home_dir
, EDIT_TEMP_FILE
);
1975 edit_insert_file (edit
, tmp
);
1981 * Ask user for a command, execute it and paste its output back to the
1985 edit_ext_cmd (WEdit
*edit
)
1991 input_dialog (_("Paste output of external command"),
1992 _("Enter shell command(s):"),
1993 MC_HISTORY_EDIT_PASTE_EXTCMD
, NULL
);
1998 tmp
= g_strconcat (exp
, " > ", home_dir
, PATH_SEP_STR EDIT_TEMP_FILE
, (char *) NULL
);
2004 edit_error_dialog (_("External command"),
2005 get_sys_error (_("Cannot execute command")));
2009 edit
->force
|= REDRAW_COMPLETELY
;
2010 tmp
= concat_dir_and_file (home_dir
, EDIT_TEMP_FILE
);
2011 edit_insert_file (edit
, tmp
);
2016 /* if block is 1, a block must be highlighted and the shell command
2017 processes it. If block is 0 the shell command is a straight system
2018 command, that just produces some output which is to be inserted */
2020 edit_block_process_cmd (WEdit
*edit
, const char *shell_cmd
, int block
)
2022 long start_mark
, end_mark
;
2024 FILE *script_home
= NULL
;
2025 FILE *script_src
= NULL
;
2026 FILE *block_file
= NULL
;
2027 gchar
*o
, *h
, *b
, *tmp
;
2028 char *quoted_name
= NULL
;
2030 o
= g_strconcat (mc_home
, shell_cmd
, (char *) NULL
); /* original source script */
2031 h
= g_strconcat (home_dir
, PATH_SEP_STR EDIT_DIR
, shell_cmd
, (char *) NULL
); /* home script */
2032 b
= concat_dir_and_file (home_dir
, EDIT_BLOCK_FILE
); /* block file */
2034 if (!(script_home
= fopen (h
, "r"))) {
2035 if (!(script_home
= fopen (h
, "w"))) {
2036 tmp
= g_strconcat (_("Error creating script:"), h
, (char *) NULL
);
2037 edit_error_dialog ("", get_sys_error (tmp
));
2039 goto edit_block_process_cmd__EXIT
;
2041 if (!(script_src
= fopen (o
, "r"))) {
2042 o
= g_strconcat (mc_home_alt
, shell_cmd
, (char *) NULL
);
2043 if (!(script_src
= fopen (o
, "r"))) {
2044 fclose (script_home
);
2046 tmp
= g_strconcat (_("Error reading script:"), o
, (char *) NULL
);
2047 edit_error_dialog ("", get_sys_error (tmp
));
2049 goto edit_block_process_cmd__EXIT
;
2052 while (fgets (buf
, sizeof (buf
), script_src
))
2053 fputs (buf
, script_home
);
2054 if (fclose (script_home
)) {
2055 tmp
= g_strconcat (_("Error closing script:"), h
, (char *) NULL
);
2056 edit_error_dialog ("", get_sys_error (tmp
));
2058 goto edit_block_process_cmd__EXIT
;
2061 tmp
= g_strconcat (_("Script created:"), h
, (char *) NULL
);
2062 edit_error_dialog ("", get_sys_error (tmp
));
2068 if (block
) { /* for marked block run indent formatter */
2069 if (eval_marks (edit
, &start_mark
, &end_mark
)) {
2070 edit_error_dialog (_("Process block"),
2072 (" You must first highlight a block of text. "));
2073 goto edit_block_process_cmd__EXIT
;
2075 edit_save_block (edit
, b
, start_mark
, end_mark
);
2076 quoted_name
= name_quote (edit
->filename
, 0);
2079 * Initial space is to avoid polluting bash history.
2081 * $1 - name of the edited file (to check its extension etc).
2082 * $2 - file containing the current block.
2083 * $3 - file where error messages should be put
2084 * (for compatibility with old scripts).
2086 tmp
= g_strconcat (" ", home_dir
, PATH_SEP_STR EDIT_DIR
, shell_cmd
, " ", quoted_name
,
2087 " ", home_dir
, PATH_SEP_STR EDIT_BLOCK_FILE
" /dev/null", (char *) NULL
);
2092 * No block selected, just execute the command for the file.
2094 * $1 - name of the edited file.
2096 tmp
= g_strconcat (" ", home_dir
, PATH_SEP_STR EDIT_DIR
, shell_cmd
, " ",
2097 quoted_name
, (char *) NULL
);
2101 g_free (quoted_name
);
2102 close_error_pipe (D_NORMAL
, NULL
);
2104 edit_refresh_cmd (edit
);
2105 edit
->force
|= REDRAW_COMPLETELY
;
2107 /* insert result block */
2109 if (edit_block_delete_cmd (edit
))
2110 goto edit_block_process_cmd__EXIT
;
2111 edit_insert_file (edit
, b
);
2112 if ((block_file
= fopen (b
, "w")))
2113 fclose (block_file
);
2114 goto edit_block_process_cmd__EXIT
;
2116 edit_block_process_cmd__EXIT
:
2123 /* prints at the cursor */
2124 /* returns the number of chars printed */
2125 int edit_print_string (WEdit
* e
, const char *s
)
2129 edit_execute_cmd (e
, -1, (unsigned char) s
[i
++]);
2130 e
->force
|= REDRAW_COMPLETELY
;
2131 edit_update_screen (e
);
2136 static void pipe_mail (WEdit
*edit
, char *to
, char *subject
, char *cc
)
2141 to
= name_quote (to
, 0);
2142 subject
= name_quote (subject
, 0);
2143 cc
= name_quote (cc
, 0);
2144 s
= g_strconcat ("mail -s ", subject
, *cc
? " -c " : "" , cc
, " ", to
, (char *) NULL
);
2156 for (i
= 0; i
< edit
->last_byte
; i
++)
2157 fputc (edit_get_byte (edit
, i
), p
);
2162 #define MAIL_DLG_HEIGHT 12
2164 void edit_mail_dialog (WEdit
* edit
)
2167 char *tmail_subject
;
2170 static char *mail_cc_last
= 0;
2171 static char *mail_subject_last
= 0;
2172 static char *mail_to_last
= 0;
2174 QuickDialog Quick_input
=
2175 {50, MAIL_DLG_HEIGHT
, -1, 0, N_(" Mail "),
2176 "[Input Line Keys]", 0, 0};
2178 QuickWidget quick_widgets
[] =
2180 {quick_button
, 6, 10, 9, MAIL_DLG_HEIGHT
, N_("&Cancel"), 0, B_CANCEL
, 0,
2181 0, NULL
, NULL
, NULL
},
2182 {quick_button
, 2, 10, 9, MAIL_DLG_HEIGHT
, N_("&OK"), 0, B_ENTER
, 0,
2183 0, NULL
, NULL
, NULL
},
2184 {quick_input
, 3, 50, 8, MAIL_DLG_HEIGHT
, "", 44, 0, 0,
2185 0, "mail-dlg-input", NULL
, NULL
},
2186 {quick_label
, 2, 50, 7, MAIL_DLG_HEIGHT
, N_(" Copies to"), 0, 0, 0,
2188 {quick_input
, 3, 50, 6, MAIL_DLG_HEIGHT
, "", 44, 0, 0,
2189 0, "mail-dlg-input-2", NULL
, NULL
},
2190 {quick_label
, 2, 50, 5, MAIL_DLG_HEIGHT
, N_(" Subject"), 0, 0, 0,
2192 {quick_input
, 3, 50, 4, MAIL_DLG_HEIGHT
, "", 44, 0, 0,
2193 0, "mail-dlg-input-3", NULL
, NULL
},
2194 {quick_label
, 2, 50, 3, MAIL_DLG_HEIGHT
, N_(" To"), 0, 0, 0,
2196 {quick_label
, 2, 50, 2, MAIL_DLG_HEIGHT
, N_(" mail -s <subject> -c <cc> <to>"), 0, 0, 0,
2200 quick_widgets
[2].str_result
= &tmail_cc
;
2201 quick_widgets
[2].text
= mail_cc_last
? mail_cc_last
: "";
2202 quick_widgets
[4].str_result
= &tmail_subject
;
2203 quick_widgets
[4].text
= mail_subject_last
? mail_subject_last
: "";
2204 quick_widgets
[6].str_result
= &tmail_to
;
2205 quick_widgets
[6].text
= mail_to_last
? mail_to_last
: "";
2207 Quick_input
.widgets
= quick_widgets
;
2209 if (quick_dialog (&Quick_input
) != B_CANCEL
) {
2210 g_free (mail_cc_last
);
2211 g_free (mail_subject_last
);
2212 g_free (mail_to_last
);
2213 mail_cc_last
= tmail_cc
;
2214 mail_subject_last
= tmail_subject
;
2215 mail_to_last
= tmail_to
;
2216 pipe_mail (edit
, mail_to_last
, mail_subject_last
, mail_cc_last
);
2221 /*******************/
2222 /* Word Completion */
2223 /*******************/
2225 static gboolean
is_break_char(char c
)
2227 return (isspace(c
) || strchr("{}[]()<>=|/\\!?~'\",.;:#$%^&*", c
));
2230 /* find first character of current word */
2231 static int edit_find_word_start (WEdit
*edit
, long *word_start
, int *word_len
)
2235 /* return if at begin of file */
2236 if (edit
->curs1
<= 0)
2239 c
= (unsigned char) edit_get_byte (edit
, edit
->curs1
- 1);
2240 /* return if not at end or in word */
2241 if (is_break_char(c
))
2244 /* search start of word to be completed */
2246 /* return if at begin of file */
2247 if (edit
->curs1
- i
< 0)
2251 c
= (unsigned char) edit_get_byte (edit
, edit
->curs1
- i
);
2253 if (is_break_char(c
)) {
2254 /* return if word starts with digit */
2258 *word_start
= edit
->curs1
- (i
- 1); /* start found */
2267 #define MAX_WORD_COMPLETIONS 100 /* in listbox */
2269 /* collect the possible completions */
2271 edit_collect_completions (WEdit
*edit
, long start
, int word_len
,
2272 char *match_expr
, struct selection
*compl,
2275 int max_len
= 0, i
, skip
;
2280 srch
= mc_search_new(match_expr
, -1);
2284 srch
->search_type
= MC_SEARCH_T_REGEX
;
2285 srch
->is_case_sentitive
= TRUE
;
2286 srch
->search_fn
= edit_search_cmd_callback
;
2288 /* collect max MAX_WORD_COMPLETIONS completions */
2290 while (*num
< MAX_WORD_COMPLETIONS
) {
2291 /* get next match */
2292 if (mc_search_run (srch
, (void *) edit
, start
+1, edit
->last_byte
, &len
) == FALSE
)
2294 start
= srch
->normal_offset
;
2296 /* add matched completion if not yet added */
2297 temp
= g_string_new("");
2298 for (i
= 0; i
< len
; i
++){
2299 skip
= edit_get_byte(edit
, start
+i
);
2302 g_string_append_c (temp
, skip
);
2307 for (i
= 0; i
< *num
; i
++) {
2310 (char *) &compl[i
].text
[word_len
],
2311 (char *) &temp
->str
[word_len
],
2312 max (len
, compl[i
].len
) - word_len
2315 break; /* skip it, already added */
2319 g_string_free(temp
, TRUE
);
2323 compl[*num
].text
= temp
->str
;
2324 compl[*num
].len
= temp
->len
;
2326 g_string_free(temp
, FALSE
);
2328 /* note the maximal length needed for the completion dialog */
2332 mc_search_free(srch
);
2337 * Complete current word using regular expression search
2338 * backwards beginning at the current cursor position.
2341 edit_complete_word_cmd (WEdit
*edit
)
2343 int word_len
= 0, i
, num_compl
= 0, max_len
;
2344 long word_start
= 0;
2345 unsigned char *bufpos
;
2347 struct selection
compl[MAX_WORD_COMPLETIONS
]; /* completions */
2349 /* search start of word to be completed */
2350 if (!edit_find_word_start (edit
, &word_start
, &word_len
))
2353 /* prepare match expression */
2354 bufpos
= &edit
->buffers1
[word_start
>> S_EDIT_BUF_SIZE
]
2355 [word_start
& M_EDIT_BUF_SIZE
];
2357 match_expr
= g_strdup_printf ("(^|\\s)%.*s[^\\s\\.=\\+\\{\\}\\[\\]\\(\\)\\\\\\!\\,<>\\?\\/@#\\$%\\^&\\*\\~\\|\\\"'\\:\\;]+", word_len
, bufpos
);
2359 /* collect the possible completions */
2360 /* start search from begin to end of file */
2362 edit_collect_completions (edit
, 0, word_len
, match_expr
,
2363 (struct selection
*) &compl, &num_compl
);
2365 if (num_compl
> 0) {
2366 /* insert completed word if there is only one match */
2367 if (num_compl
== 1) {
2368 for (i
= word_len
; i
< compl[0].len
; i
++)
2369 edit_insert (edit
, *(compl[0].text
+ i
));
2371 /* more than one possible completion => ask the user */
2373 /* !!! usually only a beep is expected and when <ALT-TAB> is !!! */
2374 /* !!! pressed again the selection dialog pops up, but that !!! */
2375 /* !!! seems to require a further internal state !!! */
2378 /* let the user select the preferred completion */
2379 editcmd_dialog_completion_show (edit
, max_len
, word_len
,
2380 (struct selection
*) &compl,
2385 g_free (match_expr
);
2386 /* release memory before return */
2387 for (i
= 0; i
< num_compl
; i
++)
2388 g_free (compl[i
].text
);
2393 edit_select_codepage_cmd (WEdit
*edit
)
2396 if (do_select_codepage ()) {
2399 cp_id
= get_codepage_id (source_codepage
>= 0 ?
2400 source_codepage
: display_codepage
);
2403 edit
->utf8
= str_isutf8 (cp_id
);
2406 edit
->force
= REDRAW_COMPLETELY
;
2407 edit_refresh_cmd (edit
);
2412 edit_insert_literal_cmd (WEdit
*edit
)
2414 int char_for_insertion
=
2415 editcmd_dialog_raw_key_query (_(" Insert Literal "),
2416 _(" Press any key: "), 0);
2417 edit_execute_key_command (edit
, -1,
2418 ascii_alpha_to_cntrl (char_for_insertion
));
2422 edit_execute_macro_cmd (WEdit
*edit
)
2425 CK_Macro (editcmd_dialog_raw_key_query
2426 (_(" Execute Macro "), _(" Press macro hotkey: "),
2428 if (command
== CK_Macro (0))
2429 command
= CK_Insert_Char
;
2431 edit_execute_key_command (edit
, command
, -1);
2435 edit_begin_end_macro_cmd(WEdit
*edit
)
2439 /* edit is a pointer to the widget */
2443 0 ? CK_Begin_Record_Macro
: CK_End_Record_Macro
;
2444 edit_execute_key_command (edit
, command
, -1);
2449 edit_load_forward_cmd (WEdit
*edit
)
2451 if (edit
->modified
) {
2452 if (edit_query_dialog2
2454 _(" Current text was modified without a file save. \n"
2455 " Continue discards these changes. "), _("C&ontinue"),
2457 edit
->force
|= REDRAW_COMPLETELY
;
2461 if ( edit_stack_iterator
+ 1 < MAX_HISTORY_MOVETO
) {
2462 if ( edit_history_moveto
[edit_stack_iterator
+ 1].line
< 1 ) {
2465 edit_stack_iterator
++;
2466 if ( edit_history_moveto
[edit_stack_iterator
].filename
) {
2467 edit_reload_line (edit
, edit_history_moveto
[edit_stack_iterator
].filename
,
2468 edit_history_moveto
[edit_stack_iterator
].line
);
2479 edit_load_back_cmd (WEdit
*edit
)
2481 if (edit
->modified
) {
2482 if (edit_query_dialog2
2484 _(" Current text was modified without a file save. \n"
2485 " Continue discards these changes. "), _("C&ontinue"),
2487 edit
->force
|= REDRAW_COMPLETELY
;
2491 if ( edit_stack_iterator
> 0 ) {
2492 edit_stack_iterator
--;
2493 if ( edit_history_moveto
[edit_stack_iterator
].filename
) {
2494 edit_reload_line (edit
, edit_history_moveto
[edit_stack_iterator
].filename
,
2495 edit_history_moveto
[edit_stack_iterator
].line
);
2506 edit_get_match_keyword_cmd (WEdit
*edit
)
2512 long word_start
= 0;
2513 unsigned char *bufpos
;
2517 char *tagfile
= NULL
;
2519 etags_hash_t def_hash
[MAX_DEFINITIONS
];
2521 for ( i
= 0; i
< MAX_DEFINITIONS
; i
++) {
2522 def_hash
[i
].filename
= NULL
;
2525 /* search start of word to be completed */
2526 if (!edit_find_word_start (edit
, &word_start
, &word_len
))
2529 /* prepare match expression */
2530 bufpos
= &edit
->buffers1
[word_start
>> S_EDIT_BUF_SIZE
]
2531 [word_start
& M_EDIT_BUF_SIZE
];
2532 match_expr
= g_strdup_printf ("%.*s", word_len
, bufpos
);
2534 ptr
= g_get_current_dir ();
2535 path
= g_strconcat (ptr
, G_DIR_SEPARATOR_S
, (char *) NULL
);
2538 /* Recursive search file 'TAGS' in parent dirs */
2540 ptr
= g_path_get_dirname (path
);
2541 g_free(path
); path
= ptr
;
2543 tagfile
= g_build_filename (path
, TAGS_NAME
, (char *) NULL
);
2544 if ( exist_file (tagfile
) )
2546 } while (strcmp( path
, G_DIR_SEPARATOR_S
) != 0);
2549 num_def
= etags_set_definition_hash(tagfile
, path
, match_expr
, (etags_hash_t
*) &def_hash
);
2554 max_len
= MAX_WIDTH_DEF_DIALOG
;
2556 if ( num_def
> 0 ) {
2557 editcmd_dialog_select_definition_show (edit
, match_expr
, max_len
, word_len
,
2558 (etags_hash_t
*) &def_hash
,
2561 g_free (match_expr
);
2565 edit_move_block_to_right (WEdit
* edit
)
2567 long start_mark
, end_mark
;
2568 long cur_bol
, start_bol
;
2570 if ( eval_marks (edit
, &start_mark
, &end_mark
) )
2573 start_bol
= edit_bol (edit
, start_mark
);
2574 cur_bol
= edit_bol (edit
, end_mark
- 1);
2576 edit_cursor_move (edit
, cur_bol
- edit
->curs1
);
2577 if ( option_fill_tabs_with_spaces
) {
2578 if ( option_fake_half_tabs
) {
2579 insert_spaces_tab (edit
, 1);
2581 insert_spaces_tab (edit
, 0);
2584 edit_insert (edit
, '\t');
2586 edit_cursor_move (edit
, edit_bol (edit
, cur_bol
) - edit
->curs1
);
2587 if ( cur_bol
== 0 ) {
2590 cur_bol
= edit_bol (edit
, cur_bol
- 1);
2591 } while (cur_bol
>= start_bol
) ;
2592 edit
->force
|= REDRAW_PAGE
;
2596 edit_move_block_to_left (WEdit
* edit
)
2598 long start_mark
, end_mark
;
2599 long cur_bol
, start_bol
;
2600 int i
, del_tab_width
;
2603 if ( eval_marks (edit
, &start_mark
, &end_mark
) )
2606 start_bol
= edit_bol (edit
, start_mark
);
2607 cur_bol
= edit_bol (edit
, end_mark
- 1);
2609 edit_cursor_move (edit
, cur_bol
- edit
->curs1
);
2610 if (option_fake_half_tabs
) {
2611 del_tab_width
= HALF_TAB_SIZE
;
2613 del_tab_width
= option_tab_spacing
;
2615 next_char
= edit_get_byte (edit
, edit
->curs1
);
2616 if ( next_char
== '\t' ) {
2617 edit_delete (edit
, 1);
2618 } else if ( next_char
== ' ' ) {
2619 for (i
= 1; i
<= del_tab_width
; i
++) {
2620 if ( next_char
== ' ' ) {
2621 edit_delete (edit
, 1);
2623 next_char
= edit_get_byte (edit
, edit
->curs1
);
2626 if ( cur_bol
== 0 ) {
2629 cur_bol
= edit_bol (edit
, cur_bol
- 1);
2630 } while (cur_bol
>= start_bol
) ;
2631 edit
->force
|= REDRAW_PAGE
;