2 Editor high level editing commands
4 Copyright (C) 1996, 1997, 1998, 2001, 2002, 2003, 2004, 2005, 2006,
6 The Free Software Foundation, Inc.
10 Andrew Borodin <aborodin@vmail.ru> 2012
11 Ilia Maslakov <il.smind@gmail.com> 2012
13 This file is part of the Midnight Commander.
15 The Midnight Commander is free software: you can redistribute it
16 and/or modify it under the terms of the GNU General Public License as
17 published by the Free Software Foundation, either version 3 of the License,
18 or (at your option) any later version.
20 The Midnight Commander is distributed in the hope that it will be useful,
21 but WITHOUT ANY WARRANTY; without even the implied warranty of
22 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 GNU General Public License for more details.
25 You should have received a copy of the GNU General Public License
26 along with this program. If not, see <http://www.gnu.org/licenses/>.
30 * \brief Source: editor high level editing commands
35 /* #define PIPE_BLOCKS_SO_READ_BYTE_BY_BYTE */
46 #include <sys/types.h>
54 #include "lib/global.h"
55 #include "lib/tty/tty.h"
56 #include "lib/tty/key.h" /* XCTRL */
57 #include "lib/mcconfig.h"
59 #include "lib/strutil.h" /* utf string functions */
61 #include "lib/util.h" /* tilde_expand() */
62 #include "lib/vfs/vfs.h"
63 #include "lib/widget.h"
64 #include "lib/event.h" /* mc_event_raise() */
66 #include "lib/charsets.h"
69 #include "src/history.h"
70 #include "src/setup.h" /* option_tab_spacing */
71 #include "src/main.h" /* mactos_t */
73 #include "src/selcodepage.h"
75 #include "src/keybind-defaults.h"
76 #include "src/util.h" /* check_for_default() */
77 #include "src/filemanager/layout.h" /* mc_refresh() */
79 #include "edit-impl.h"
80 #include "editwidget.h"
81 #include "editcmd_dialogs.h"
84 #include "spell_dialogs.h"
88 /*** global variables ****************************************************************************/
90 /* search and replace: */
91 int search_create_bookmark
= FALSE
;
93 /* queries on a save */
94 int edit_confirm_save
= 1;
96 /*** file scope macro definitions ****************************************************************/
100 #define TEMP_BUF_LEN 1024
102 #define INPUT_INDEX 9
104 /* thanks to Liviu Daia <daia@stoilow.imar.ro> for getting this
105 (and the above) routines to work properly - paul */
107 #define is_digit(x) ((x) >= '0' && (x) <= '9')
109 #define MAIL_DLG_HEIGHT 12
111 #define MAX_WORD_COMPLETIONS 100 /* in listbox */
113 /*** file scope type declarations ****************************************************************/
115 /*** file scope variables ************************************************************************/
117 /*** file scope functions ************************************************************************/
118 /* --------------------------------------------------------------------------------------------- */
120 /* If 0 (quick save) then a) create/truncate <filename> file,
121 b) save to <filename>;
122 if 1 (safe save) then a) save to <tempnam>,
123 b) rename <tempnam> to <filename>;
124 if 2 (do backups) then a) save to <tempnam>,
125 b) rename <filename> to <filename.backup_ext>,
126 c) rename <tempnam> to <filename>. */
128 /* returns 0 on error, -1 on abort */
131 edit_save_file (WEdit
* edit
, const vfs_path_t
* filename_vpath
)
136 int this_save_mode
, fd
= -1;
137 vfs_path_t
*real_filename_vpath
;
138 vfs_path_t
*savename_vpath
= NULL
;
139 const char *start_filename
;
140 const vfs_path_element_t
*vpath_element
;
142 vpath_element
= vfs_path_get_by_index (filename_vpath
, 0);
143 if (vpath_element
== NULL
)
146 start_filename
= vpath_element
->path
;
147 if (*start_filename
== '\0')
150 if (*start_filename
!= PATH_SEP
&& edit
->dir_vpath
!= NULL
)
152 real_filename_vpath
= vfs_path_append_vpath_new (edit
->dir_vpath
, filename_vpath
, NULL
);
156 real_filename_vpath
= vfs_path_clone (filename_vpath
);
159 this_save_mode
= option_save_mode
;
160 if (this_save_mode
!= EDIT_QUICK_SAVE
)
162 if (!vfs_file_is_local (real_filename_vpath
)
163 || (fd
= mc_open (real_filename_vpath
, O_RDONLY
| O_BINARY
)) == -1)
166 * The file does not exists yet, so no safe save or
167 * backup are necessary.
169 this_save_mode
= EDIT_QUICK_SAVE
;
175 if (this_save_mode
== EDIT_QUICK_SAVE
&& !edit
->skip_detach_prompt
)
180 rv
= mc_stat (real_filename_vpath
, &sb
);
181 if (rv
== 0 && sb
.st_nlink
> 1)
183 rv
= edit_query_dialog3 (_("Warning"),
184 _("File has hard-links. Detach before saving?"),
185 _("&Yes"), _("&No"), _("&Cancel"));
189 this_save_mode
= EDIT_SAFE_SAVE
;
192 edit
->skip_detach_prompt
= 1;
195 vfs_path_free (real_filename_vpath
);
200 /* Prevent overwriting changes from other editor sessions. */
201 if (rv
== 0 && edit
->stat1
.st_mtime
!= 0 && edit
->stat1
.st_mtime
!= sb
.st_mtime
)
203 /* The default action is "Cancel". */
206 rv
= edit_query_dialog2 (_("Warning"),
207 _("The file has been modified in the meantime. Save anyway?"),
208 _("&Yes"), _("&Cancel"));
211 vfs_path_free (real_filename_vpath
);
217 if (this_save_mode
!= EDIT_QUICK_SAVE
)
219 char *savedir
, *saveprefix
;
221 savedir
= vfs_path_tokens_get (real_filename_vpath
, 0, -1);
223 savedir
= g_strdup (".");
225 /* Token-related function never return leading slash, so we need add it manually */
226 saveprefix
= mc_build_filename ("/", savedir
, "cooledit", NULL
);
228 fd
= mc_mkstemps (&savename_vpath
, saveprefix
, NULL
);
230 if (savename_vpath
== NULL
)
232 vfs_path_free (real_filename_vpath
);
236 * Close for now because mc_mkstemps use pure open system call
237 * to create temporary file and it needs to be reopened by
238 * VFS-aware mc_open().
243 savename_vpath
= vfs_path_clone (real_filename_vpath
);
245 (void) mc_chown (savename_vpath
, edit
->stat1
.st_uid
, edit
->stat1
.st_gid
);
246 (void) mc_chmod (savename_vpath
, edit
->stat1
.st_mode
);
248 fd
= mc_open (savename_vpath
, O_CREAT
| O_WRONLY
| O_TRUNC
| O_BINARY
, edit
->stat1
.st_mode
);
253 p
= edit_get_write_filter (savename_vpath
, real_filename_vpath
);
259 file
= (FILE *) popen (p
, "w");
263 filelen
= edit_write_stream (edit
, file
);
267 if (pclose (file
) != 0)
269 tmp
= g_strdup_printf (_("Error writing to pipe: %s"), p
);
270 edit_error_dialog (_("Error"), tmp
);
279 tmp
= g_strdup_printf (_("Cannot open pipe for writing: %s"), p
);
280 edit_error_dialog (_("Error"), get_sys_error (tmp
));
287 else if (edit
->lb
== LB_ASIS
)
288 { /* do not change line breaks */
291 filelen
= edit
->last_byte
;
292 while (buf
<= (edit
->curs1
>> S_EDIT_BUF_SIZE
) - 1)
294 if (mc_write (fd
, (char *) edit
->buffers1
[buf
], EDIT_BUF_SIZE
) != EDIT_BUF_SIZE
)
302 (fd
, (char *) edit
->buffers1
[buf
],
303 edit
->curs1
& M_EDIT_BUF_SIZE
) != (edit
->curs1
& M_EDIT_BUF_SIZE
))
307 else if (edit
->curs2
)
310 buf
= (edit
->curs2
>> S_EDIT_BUF_SIZE
);
313 (char *) edit
->buffers2
[buf
] + EDIT_BUF_SIZE
-
314 (edit
->curs2
& M_EDIT_BUF_SIZE
) - 1,
315 1 + (edit
->curs2
& M_EDIT_BUF_SIZE
)) != 1 + (edit
->curs2
& M_EDIT_BUF_SIZE
))
323 if (mc_write (fd
, (char *) edit
->buffers2
[buf
], EDIT_BUF_SIZE
) != EDIT_BUF_SIZE
)
335 /* Update the file information, especially the mtime. */
336 if (mc_stat (savename_vpath
, &edit
->stat1
) == -1)
340 { /* change line breaks */
342 const vfs_path_element_t
*path_element
;
346 path_element
= vfs_path_get_by_index (savename_vpath
, -1);
347 file
= (FILE *) fopen (path_element
->path
, "w");
350 filelen
= edit_write_stream (edit
, file
);
357 msg
= g_strdup_printf (_("Cannot open file for writing: %s"), path_element
->path
);
358 edit_error_dialog (_("Error"), msg
);
364 if (filelen
!= edit
->last_byte
)
367 if (this_save_mode
== EDIT_DO_BACKUP
)
369 char *tmp_store_filename
;
370 vfs_path_element_t
*last_vpath_element
;
371 vfs_path_t
*tmp_vpath
;
375 assert (option_backup_ext
!= NULL
);
377 /* add backup extention to the path */
378 tmp_vpath
= vfs_path_clone (real_filename_vpath
);
379 last_vpath_element
= (vfs_path_element_t
*) vfs_path_get_by_index (tmp_vpath
, -1);
380 tmp_store_filename
= last_vpath_element
->path
;
381 last_vpath_element
->path
= g_strdup_printf ("%s%s", tmp_store_filename
, option_backup_ext
);
382 g_free (tmp_store_filename
);
384 ok
= (mc_rename (real_filename_vpath
, tmp_vpath
) != -1);
385 vfs_path_free (tmp_vpath
);
390 if (this_save_mode
!= EDIT_QUICK_SAVE
)
391 if (mc_rename (savename_vpath
, real_filename_vpath
) == -1)
394 vfs_path_free (real_filename_vpath
);
395 vfs_path_free (savename_vpath
);
398 /* FIXME: Is this safe ?
399 * if (this_save_mode != EDIT_QUICK_SAVE)
400 * mc_unlink (savename);
402 vfs_path_free (real_filename_vpath
);
403 vfs_path_free (savename_vpath
);
407 /* --------------------------------------------------------------------------------------------- */
410 edit_check_newline (WEdit
* edit
)
412 return !(option_check_nl_at_eof
&& edit
->last_byte
> 0
413 && edit_get_byte (edit
, edit
->last_byte
- 1) != '\n'
414 && edit_query_dialog2 (_("Warning"),
415 _("The file you are saving is not finished with a newline"),
416 _("C&ontinue"), _("&Cancel")));
419 /* --------------------------------------------------------------------------------------------- */
422 edit_get_save_file_as (WEdit
* edit
)
425 #define DLG_HEIGHT 14
427 static LineBreaks cur_lb
= LB_ASIS
;
429 char *filename
= vfs_path_to_str (edit
->filename_vpath
);
432 const char *lb_names
[LB_NAMES
] = {
433 N_("&Do not change"),
434 N_("&Unix format (LF)"),
435 N_("&Windows/DOS format (CR LF)"),
436 N_("&Macintosh format (CR)")
439 QuickWidget quick_widgets
[] = {
440 QUICK_BUTTON (6, 10, DLG_HEIGHT
- 3, DLG_HEIGHT
, N_("&Cancel"), B_CANCEL
, NULL
),
441 QUICK_BUTTON (2, 10, DLG_HEIGHT
- 3, DLG_HEIGHT
, N_("&OK"), B_ENTER
, NULL
),
442 QUICK_RADIO (5, DLG_WIDTH
, DLG_HEIGHT
- 8, DLG_HEIGHT
, LB_NAMES
, lb_names
, (int *) &cur_lb
),
443 QUICK_LABEL (3, DLG_WIDTH
, DLG_HEIGHT
- 9, DLG_HEIGHT
, N_("Change line breaks to:")),
444 QUICK_INPUT (3, DLG_WIDTH
, DLG_HEIGHT
- 11, DLG_HEIGHT
, filename
, DLG_WIDTH
- 6, 0,
445 "save-as", &filename_res
),
446 QUICK_LABEL (3, DLG_WIDTH
, DLG_HEIGHT
- 12, DLG_HEIGHT
, N_("Enter file name:")),
450 QuickDialog Quick_options
= {
451 DLG_WIDTH
, DLG_HEIGHT
, -1, -1,
452 N_("Save As"), "[Save File As]",
453 quick_widgets
, NULL
, NULL
, FALSE
456 if (quick_dialog (&Quick_options
) != B_CANCEL
)
459 vfs_path_t
*ret_vpath
;
462 fname
= tilde_expand (filename_res
);
463 g_free (filename_res
);
464 ret_vpath
= vfs_path_from_str (fname
);
476 /* --------------------------------------------------------------------------------------------- */
478 /** returns TRUE on success */
481 edit_save_cmd (WEdit
* edit
)
483 int res
, save_lock
= 0;
485 if (!edit
->locked
&& !edit
->delete_file
)
486 save_lock
= lock_file (edit
->filename_vpath
);
487 res
= edit_save_file (edit
, edit
->filename_vpath
);
489 /* Maintain modify (not save) lock on failure */
490 if ((res
> 0 && edit
->locked
) || save_lock
)
491 edit
->locked
= unlock_file (edit
->filename_vpath
);
493 /* On failure try 'save as', it does locking on its own */
495 return edit_save_as_cmd (edit
);
496 edit
->force
|= REDRAW_COMPLETELY
;
499 edit
->delete_file
= 0;
506 /* --------------------------------------------------------------------------------------------- */
510 * @param h screen the owner of editor window
511 * @param vpath vfs file path
512 * @return TRUE if file content was successfully loaded, FALSE otherwise
515 static inline gboolean
516 edit_load_file_from_filename (Dlg_head
* h
, const vfs_path_t
* vpath
)
518 return edit_add_window (h
, h
->y
+ 1, h
->x
, h
->lines
- 2, h
->cols
, vpath
, 0);
521 /* --------------------------------------------------------------------------------------------- */
524 edit_delete_column_of_text (WEdit
* edit
)
531 eval_marks (edit
, &m1
, &m2
);
532 n
= edit_move_forward (edit
, m1
, 0, m2
) + 1;
533 c
= (long) edit_move_forward3 (edit
, edit_bol (edit
, m1
), 0, m1
);
534 d
= (long) edit_move_forward3 (edit
, edit_bol (edit
, m2
), 0, m2
);
535 b
= max (min (c
, d
), min (edit
->column1
, edit
->column2
));
536 c
= max (c
, max (edit
->column1
, edit
->column2
));
540 r
= edit_bol (edit
, edit
->curs1
);
541 p
= edit_move_forward3 (edit
, r
, b
, 0);
542 q
= edit_move_forward3 (edit
, r
, c
, 0);
547 edit_cursor_move (edit
, p
- edit
->curs1
);
550 /* delete line between margins */
551 if (edit_get_byte (edit
, edit
->curs1
) != '\n')
552 edit_delete (edit
, 1);
556 /* move to next line except on the last delete */
557 edit_cursor_move (edit
, edit_move_forward (edit
, edit
->curs1
, 1, 0) - edit
->curs1
);
561 /* --------------------------------------------------------------------------------------------- */
562 /** if success return 0 */
565 edit_block_delete (WEdit
* edit
)
568 off_t start_mark
, end_mark
;
570 long curs_line
, c1
, c2
;
572 if (eval_marks (edit
, &start_mark
, &end_mark
))
574 if (edit
->column_highlight
&& edit
->mark2
< 0)
575 edit_mark_cmd (edit
, FALSE
);
576 if ((end_mark
- start_mark
) > option_max_undo
/ 2)
578 /* Warning message with a query to continue or cancel the operation */
579 if (edit_query_dialog2
582 ("Block is large, you may not be able to undo this action"),
583 _("C&ontinue"), _("&Cancel")))
588 c1
= min (edit
->column1
, edit
->column2
);
589 c2
= max (edit
->column1
, edit
->column2
);
593 edit_push_markers (edit
);
595 curs_line
= edit
->curs_line
;
597 curs_pos
= edit
->curs_col
+ edit
->over_col
;
599 /* move cursor to start of selection */
600 edit_cursor_move (edit
, start_mark
- edit
->curs1
);
601 edit_scroll_screen_over_cursor (edit
);
603 if (start_mark
< end_mark
)
605 if (edit
->column_highlight
)
610 edit_mark_cmd (edit
, FALSE
);
611 edit_delete_column_of_text (edit
);
612 /* move cursor to the saved position */
613 edit_move_to_line (edit
, curs_line
);
614 /* calculate line width and cursor position before cut */
615 line_width
= (long) edit_move_forward3 (edit
, edit_bol (edit
, edit
->curs1
), 0,
616 edit_eol (edit
, edit
->curs1
));
617 if (option_cursor_beyond_eol
&& curs_pos
> line_width
)
618 edit
->over_col
= curs_pos
- line_width
;
622 while (count
< end_mark
)
624 edit_delete (edit
, 1);
629 edit_set_markers (edit
, 0, 0, 0, 0);
630 edit
->force
|= REDRAW_PAGE
;
634 /* --------------------------------------------------------------------------------------------- */
636 * Get EOL symbol for searching.
638 * @param edit editor object
643 edit_search_get_current_end_line_char (const WEdit
* edit
)
654 /* --------------------------------------------------------------------------------------------- */
656 * Checking if search condition have BOL(^) or EOL ($) regexp special characters.
658 * @param search search object
659 * @return result of checks.
662 static edit_search_line_t
663 edit_get_search_line_type (mc_search_t
* search
)
665 edit_search_line_t search_line_type
= 0;
667 if (search
->search_type
!= MC_SEARCH_T_REGEX
)
668 return search_line_type
;
670 if (*search
->original
== '^')
671 search_line_type
|= AT_START_LINE
;
673 if (search
->original
[search
->original_len
- 1] == '$')
674 search_line_type
|= AT_END_LINE
;
675 return search_line_type
;
678 /* --------------------------------------------------------------------------------------------- */
680 * Calculating the start position of next line.
682 * @param edit editor object
683 * @param current_pos current position
684 * @param max_pos max position
685 * @param end_string_symbol end of line symbol
686 * @return start position of next line
690 edit_calculate_start_of_next_line (WEdit
* edit
, off_t current_pos
, off_t max_pos
,
691 char end_string_symbol
)
695 for (i
= current_pos
; i
< max_pos
; i
++)
698 if (edit_get_byte (edit
, i
) == end_string_symbol
)
705 /* --------------------------------------------------------------------------------------------- */
707 * Calculating the end position of previous line.
709 * @param edit editor object
710 * @param current_pos current position
711 * @param end_string_symbol end of line symbol
712 * @return end position of previous line
716 edit_calculate_end_of_previous_line (WEdit
* edit
, off_t current_pos
, char end_string_symbol
)
720 for (i
= current_pos
- 1; i
>= 0; i
--)
721 if (edit_get_byte (edit
, i
) == end_string_symbol
)
727 /* --------------------------------------------------------------------------------------------- */
729 * Calculating the start position of previous line.
731 * @param edit editor object
732 * @param current_pos current position
733 * @param end_string_symbol end of line symbol
734 * @return start position of previous line
738 edit_calculate_start_of_previous_line (WEdit
* edit
, off_t current_pos
, char end_string_symbol
)
740 current_pos
= edit_calculate_end_of_previous_line (edit
, current_pos
, end_string_symbol
);
741 current_pos
= edit_calculate_end_of_previous_line (edit
, current_pos
, end_string_symbol
);
743 return (current_pos
+ 1);
746 /* --------------------------------------------------------------------------------------------- */
748 * Calculating the start position of current line.
750 * @param edit editor object
751 * @param current_pos current position
752 * @param end_string_symbol end of line symbol
753 * @return start position of current line
757 edit_calculate_start_of_current_line (WEdit
* edit
, off_t current_pos
, char end_string_symbol
)
759 current_pos
= edit_calculate_end_of_previous_line (edit
, current_pos
, end_string_symbol
);
761 return (current_pos
+ 1);
764 /* --------------------------------------------------------------------------------------------- */
766 * Fixing (if needed) search start position if 'only in selection' option present.
768 * @param edit editor object
772 edit_search_fix_search_start_if_selection (WEdit
* edit
)
774 off_t start_mark
= 0;
777 if (!edit_search_options
.only_in_selection
)
780 if (eval_marks (edit
, &start_mark
, &end_mark
) != 0)
783 if (edit_search_options
.backwards
)
785 if (edit
->search_start
> end_mark
|| edit
->search_start
<= start_mark
)
786 edit
->search_start
= end_mark
;
790 if (edit
->search_start
< start_mark
|| edit
->search_start
>= end_mark
)
791 edit
->search_start
= start_mark
;
795 /* --------------------------------------------------------------------------------------------- */
798 editcmd_find (WEdit
* edit
, gsize
* len
)
800 off_t search_start
= edit
->search_start
;
802 off_t start_mark
= 0;
803 off_t end_mark
= edit
->last_byte
;
805 char end_string_symbol
;
807 end_string_symbol
= edit_search_get_current_end_line_char (edit
);
809 /* prepare for search */
810 if (edit_search_options
.only_in_selection
)
812 mark_res
= eval_marks (edit
, &start_mark
, &end_mark
);
815 edit
->search
->error
= MC_SEARCH_E_NOTFOUND
;
816 edit
->search
->error_str
= g_strdup (_("Search string not found"));
820 /* fix the start and the end of search block positions */
821 if ((edit
->search_line_type
& AT_START_LINE
) != 0
822 && (start_mark
!= 0 || edit_get_byte (edit
, start_mark
- 1) != end_string_symbol
))
825 edit_calculate_start_of_next_line (edit
, start_mark
, edit
->last_byte
,
828 if ((edit
->search_line_type
& AT_END_LINE
) != 0
829 && (end_mark
- 1 != edit
->last_byte
830 || edit_get_byte (edit
, end_mark
) != end_string_symbol
))
832 end_mark
= edit_calculate_end_of_previous_line (edit
, end_mark
, end_string_symbol
);
834 if (start_mark
>= end_mark
)
836 edit
->search
->error
= MC_SEARCH_E_NOTFOUND
;
837 edit
->search
->error_str
= g_strdup (_("Search string not found"));
843 if (edit_search_options
.backwards
)
844 end_mark
= max (1, edit
->curs1
) - 1;
848 if (edit_search_options
.backwards
)
850 /* backward search */
851 search_end
= end_mark
;
853 if ((edit
->search_line_type
& AT_START_LINE
) != 0)
855 edit_calculate_start_of_current_line (edit
, search_start
, end_string_symbol
);
857 while ((int) search_start
>= start_mark
)
859 if (search_end
> (off_t
) (search_start
+ edit
->search
->original_len
)
860 && mc_search_is_fixed_search_str (edit
->search
))
862 search_end
= search_start
+ edit
->search
->original_len
;
864 if (mc_search_run (edit
->search
, (void *) edit
, search_start
, search_end
, len
)
865 && edit
->search
->normal_offset
== search_start
)
871 if ((edit
->search_line_type
& AT_START_LINE
) != 0)
873 edit_calculate_start_of_previous_line (edit
, search_start
, end_string_symbol
);
877 edit
->search
->error_str
= g_strdup (_("Search string not found"));
882 if ((edit
->search_line_type
& AT_START_LINE
) != 0 && search_start
!= start_mark
)
884 edit_calculate_start_of_next_line (edit
, search_start
, end_mark
, end_string_symbol
);
885 return mc_search_run (edit
->search
, (void *) edit
, search_start
, end_mark
, len
);
890 /* --------------------------------------------------------------------------------------------- */
893 edit_replace_cmd__conv_to_display (char *str
)
898 tmp
= str_convert_to_display (str
);
902 return g_string_free (tmp
, FALSE
);
903 g_string_free (tmp
, TRUE
);
906 return g_strdup (str
);
909 /* --------------------------------------------------------------------------------------------- */
912 edit_replace_cmd__conv_to_input (char *str
)
917 tmp
= str_convert_to_input (str
);
921 return g_string_free (tmp
, FALSE
);
922 g_string_free (tmp
, TRUE
);
925 return g_strdup (str
);
928 /* --------------------------------------------------------------------------------------------- */
931 edit_do_search (WEdit
* edit
)
935 if (edit
->search
== NULL
)
936 edit
->search_start
= edit
->curs1
;
938 edit_push_undo_action (edit
, KEY_PRESS
+ edit
->start_display
);
940 if (search_create_bookmark
)
942 int found
= 0, books
= 0;
943 long l
= 0, l_last
= -1;
946 search_create_bookmark
= FALSE
;
947 book_mark_flush (edit
, -1);
951 if (!mc_search_run (edit
->search
, (void *) edit
, q
, edit
->last_byte
, &len
))
954 edit
->search_start
= edit
->search
->normal_offset
;
956 l
+= edit_count_lines (edit
, q
, edit
->search
->normal_offset
);
959 book_mark_insert (edit
, l
, BOOK_MARK_FOUND_COLOR
);
963 q
= edit
->search
->normal_offset
+ 1;
967 edit_error_dialog (_("Search"), _("Search string not found"));
969 edit_cursor_move (edit
, edit
->search_start
- edit
->curs1
);
973 if (edit
->found_len
!= 0 && edit
->search_start
== edit
->found_start
+ 1
974 && edit_search_options
.backwards
)
975 edit
->search_start
--;
977 if (edit
->found_len
!= 0 && edit
->search_start
== edit
->found_start
- 1
978 && !edit_search_options
.backwards
)
979 edit
->search_start
++;
981 if (editcmd_find (edit
, &len
))
983 edit
->found_start
= edit
->search_start
= edit
->search
->normal_offset
;
984 edit
->found_len
= len
;
986 edit_cursor_move (edit
, edit
->search_start
- edit
->curs1
);
987 edit_scroll_screen_over_cursor (edit
);
988 if (edit_search_options
.backwards
)
989 edit
->search_start
--;
991 edit
->search_start
++;
995 edit
->search_start
= edit
->curs1
;
996 if (edit
->search
->error_str
!= NULL
)
997 edit_error_dialog (_("Search"), edit
->search
->error_str
);
1001 edit
->force
|= REDRAW_COMPLETELY
;
1002 edit_scroll_screen_over_cursor (edit
);
1005 /* --------------------------------------------------------------------------------------------- */
1008 edit_search (WEdit
* edit
)
1010 if (editcmd_dialog_search_show (edit
))
1012 edit
->search_line_type
= edit_get_search_line_type (edit
->search
);
1013 edit_search_fix_search_start_if_selection (edit
);
1014 edit_do_search (edit
);
1018 /* --------------------------------------------------------------------------------------------- */
1019 /** Return a null terminated length of text. Result must be g_free'd */
1021 static unsigned char *
1022 edit_get_block (WEdit
* edit
, off_t start
, off_t finish
, int *l
)
1024 unsigned char *s
, *r
;
1025 r
= s
= g_malloc0 (finish
- start
+ 1);
1026 if (edit
->column_highlight
)
1029 /* copy from buffer, excluding chars that are out of the column 'margins' */
1030 while (start
< finish
)
1035 x
= edit_move_forward3 (edit
, edit_bol (edit
, start
), 0, start
);
1036 c
= edit_get_byte (edit
, start
);
1037 if ((x
>= edit
->column1
&& x
< edit
->column2
)
1038 || (x
>= edit
->column2
&& x
< edit
->column1
) || c
== '\n')
1048 *l
= finish
- start
;
1049 while (start
< finish
)
1050 *s
++ = edit_get_byte (edit
, start
++);
1056 /* --------------------------------------------------------------------------------------------- */
1057 /** copies a block to clipboard file */
1060 edit_save_block_to_clip_file (WEdit
* edit
, off_t start
, off_t finish
)
1065 tmp
= mc_config_get_full_path (EDIT_CLIP_FILE
);
1066 ret
= edit_save_block (edit
, tmp
, start
, finish
);
1071 /* --------------------------------------------------------------------------------------------- */
1074 pipe_mail (WEdit
* edit
, char *to
, char *subject
, char *cc
)
1079 to
= name_quote (to
, 0);
1080 subject
= name_quote (subject
, 0);
1081 cc
= name_quote (cc
, 0);
1082 s
= g_strconcat ("mail -s ", subject
, *cc
? " -c " : "", cc
, " ", to
, (char *) NULL
);
1096 for (i
= 0; i
< edit
->last_byte
; i
++)
1097 fputc (edit_get_byte (edit
, i
), p
);
1102 /* --------------------------------------------------------------------------------------------- */
1103 /** find first character of current word */
1106 edit_find_word_start (WEdit
* edit
, off_t
* word_start
, gsize
* word_len
)
1111 /* return if at begin of file */
1112 if (edit
->curs1
<= 0)
1115 c
= edit_get_byte (edit
, edit
->curs1
- 1);
1116 /* return if not at end or in word */
1117 if (is_break_char (c
))
1120 /* search start of word to be completed */
1123 /* return if at begin of file */
1124 if ((gsize
) edit
->curs1
< i
)
1128 c
= edit_get_byte (edit
, edit
->curs1
- i
);
1130 if (is_break_char (c
))
1132 /* return if word starts with digit */
1136 *word_start
= edit
->curs1
- (i
- 1); /* start found */
1145 /* --------------------------------------------------------------------------------------------- */
1147 * Get current word under cursor
1149 * @param edit editor object
1150 * @param srch mc_search object
1151 * @param word_start start word position
1153 * @return newly allocated string or NULL if no any words under cursor
1157 edit_collect_completions_get_current_word (WEdit
* edit
, mc_search_t
* srch
, off_t word_start
)
1163 if (!mc_search_run (srch
, (void *) edit
, word_start
, edit
->last_byte
, &len
))
1166 temp
= g_string_sized_new (len
);
1168 for (i
= 0; i
< (off_t
) len
; i
++)
1172 chr
= edit_get_byte (edit
, word_start
+ i
);
1174 g_string_append_c (temp
, chr
);
1177 return g_string_free (temp
, temp
->len
== 0);
1180 /* --------------------------------------------------------------------------------------------- */
1181 /** collect the possible completions */
1184 edit_collect_completions (WEdit
* edit
, off_t word_start
, gsize word_len
,
1185 char *match_expr
, struct selection
*compl, gsize
* num
)
1193 off_t last_byte
, start
= -1;
1196 srch
= mc_search_new (match_expr
, -1);
1200 if (mc_config_get_bool
1201 (mc_main_config
, CONFIG_APP_SECTION
, "editor_wordcompletion_collect_entire_file", 0))
1203 last_byte
= edit
->last_byte
;
1207 last_byte
= word_start
;
1210 srch
->search_type
= MC_SEARCH_T_REGEX
;
1211 srch
->is_case_sensitive
= TRUE
;
1212 srch
->search_fn
= edit_search_cmd_callback
;
1214 current_word
= edit_collect_completions_get_current_word (edit
, srch
, word_start
);
1216 temp
= g_string_new ("");
1218 /* collect max MAX_WORD_COMPLETIONS completions */
1219 while (mc_search_run (srch
, (void *) edit
, start
+ 1, last_byte
, &len
))
1221 g_string_set_size (temp
, 0);
1222 start
= srch
->normal_offset
;
1224 /* add matched completion if not yet added */
1225 for (i
= 0; i
< len
; i
++)
1227 skip
= edit_get_byte (edit
, start
+ i
);
1231 /* skip current word */
1232 if (start
+ (off_t
) i
== word_start
)
1235 g_string_append_c (temp
, skip
);
1241 if (current_word
!= NULL
&& strcmp (current_word
, temp
->str
) == 0)
1246 for (i
= 0; i
< *num
; i
++)
1249 ((char *) &compl[i
].text
[word_len
],
1250 (char *) &temp
->str
[word_len
], max (len
, compl[i
].len
) - word_len
) == 0)
1252 struct selection
this = compl[i
];
1253 for (++i
; i
< *num
; i
++)
1255 compl[i
- 1] = compl[i
];
1257 compl[*num
- 1] = this;
1259 break; /* skip it, already added */
1265 if (*num
== MAX_WORD_COMPLETIONS
)
1267 g_free (compl[0].text
);
1268 for (i
= 1; i
< *num
; i
++)
1270 compl[i
- 1] = compl[i
];
1277 recoded
= str_convert_to_display (temp
->str
);
1279 if (recoded
&& recoded
->len
)
1280 g_string_assign (temp
, recoded
->str
);
1282 g_string_free (recoded
, TRUE
);
1285 compl[*num
].text
= g_strdup (temp
->str
);
1286 compl[*num
].len
= temp
->len
;
1290 /* note the maximal length needed for the completion dialog */
1295 mc_search_free (srch
);
1296 g_string_free (temp
, TRUE
);
1297 g_free (current_word
);
1302 /* --------------------------------------------------------------------------------------------- */
1305 edit_insert_column_of_text (WEdit
* edit
, unsigned char *data
, int size
, int width
,
1306 off_t
* start_pos
, off_t
* end_pos
, int *col1
, int *col2
)
1311 cursor
= edit
->curs1
;
1312 col
= edit_get_col (edit
);
1314 for (i
= 0; i
< size
; i
++)
1316 if (data
[i
] != '\n')
1317 edit_insert (edit
, data
[i
]);
1319 { /* fill in and move to next line */
1323 if (edit_get_byte (edit
, edit
->curs1
) != '\n')
1325 l
= width
- (edit_get_col (edit
) - col
);
1328 edit_insert (edit
, ' ');
1332 for (p
= edit
->curs1
;; p
++)
1334 if (p
== edit
->last_byte
)
1336 edit_cursor_move (edit
, edit
->last_byte
- edit
->curs1
);
1337 edit_insert_ahead (edit
, '\n');
1341 if (edit_get_byte (edit
, p
) == '\n')
1347 edit_cursor_move (edit
, edit_move_forward3 (edit
, p
, col
, 0) - edit
->curs1
);
1348 l
= col
- edit_get_col (edit
);
1349 while (l
>= space_width
)
1351 edit_insert (edit
, ' ');
1358 *col2
= col
+ width
;
1359 *start_pos
= cursor
;
1360 *end_pos
= edit
->curs1
;
1361 edit_cursor_move (edit
, cursor
- edit
->curs1
);
1364 /* --------------------------------------------------------------------------------------------- */
1367 edit_macro_comparator (gconstpointer
* macro1
, gconstpointer
* macro2
)
1369 const macros_t
*m1
= (const macros_t
*) macro1
;
1370 const macros_t
*m2
= (const macros_t
*) macro2
;
1372 return m1
->hotkey
- m2
->hotkey
;
1375 /* --------------------------------------------------------------------------------------------- */
1378 edit_macro_sort_by_hotkey (void)
1380 if (macros_list
!= NULL
&& macros_list
->len
!= 0)
1381 g_array_sort (macros_list
, (GCompareFunc
) edit_macro_comparator
);
1384 /* --------------------------------------------------------------------------------------------- */
1387 edit_get_macro (WEdit
* edit
, int hotkey
, const macros_t
** macros
, guint
* indx
)
1389 const macros_t
*array_start
= &g_array_index (macros_list
, struct macros_t
, 0);
1391 macros_t search_macro
;
1395 search_macro
.hotkey
= hotkey
;
1396 result
= bsearch (&search_macro
, macros_list
->data
, macros_list
->len
,
1397 sizeof (macros_t
), (GCompareFunc
) edit_macro_comparator
);
1399 if (result
!= NULL
&& result
->macro
!= NULL
)
1401 *indx
= (result
- array_start
);
1409 /* --------------------------------------------------------------------------------------------- */
1410 /** returns FALSE on error */
1413 edit_delete_macro (WEdit
* edit
, int hotkey
)
1415 mc_config_t
*macros_config
= NULL
;
1416 const char *section_name
= "editor";
1417 gchar
*macros_fname
;
1420 const macros_t
*macros
= NULL
;
1422 /* clear array of actions for current hotkey */
1423 while (edit_get_macro (edit
, hotkey
, ¯os
, &indx
))
1425 if (macros
->macro
!= NULL
)
1426 g_array_free (macros
->macro
, TRUE
);
1428 g_array_remove_index (macros_list
, indx
);
1429 edit_macro_sort_by_hotkey ();
1432 macros_fname
= mc_config_get_full_path (MC_MACRO_FILE
);
1433 macros_config
= mc_config_init (macros_fname
, FALSE
);
1434 g_free (macros_fname
);
1436 if (macros_config
== NULL
)
1439 skeyname
= lookup_key_by_code (hotkey
);
1440 while (mc_config_del_key (macros_config
, section_name
, skeyname
))
1443 mc_config_save_file (macros_config
, NULL
);
1444 mc_config_deinit (macros_config
);
1448 /* --------------------------------------------------------------------------------------------- */
1450 * Callback for the iteration of objects in the 'editors' array.
1451 * Toggle syntax highlighting in editor object.
1453 * @param data probably WEdit object
1454 * @param user_data unused
1458 edit_syntax_onoff_cb (void *data
, void *user_data
)
1462 if (edit_widget_is_editor ((const Widget
*) data
))
1464 WEdit
*edit
= (WEdit
*) data
;
1466 if (option_syntax_highlighting
)
1467 edit_load_syntax (edit
, NULL
, edit
->syntax_type
);
1468 edit
->force
|= REDRAW_PAGE
;
1472 /* --------------------------------------------------------------------------------------------- */
1474 * Callback for the iteration of objects in the 'editors' array.
1475 * Redraw editor object.
1477 * @param data probably WEdit object
1478 * @param user_data unused
1482 edit_redraw_page_cb (void *data
, void *user_data
)
1486 if (edit_widget_is_editor ((const Widget
*) data
))
1487 ((WEdit
*) data
)->force
|= REDRAW_PAGE
;
1490 /* --------------------------------------------------------------------------------------------- */
1491 /*** public functions ****************************************************************************/
1492 /* --------------------------------------------------------------------------------------------- */
1495 edit_refresh_cmd (void)
1502 /* --------------------------------------------------------------------------------------------- */
1504 * Toggle syntax highlighting in all editor windows.
1506 * @param h root widget for all windows
1510 edit_syntax_onoff_cmd (Dlg_head
* h
)
1512 option_syntax_highlighting
= !option_syntax_highlighting
;
1513 g_list_foreach (h
->widgets
, edit_syntax_onoff_cb
, NULL
);
1517 /* --------------------------------------------------------------------------------------------- */
1519 * Toggle tabs showing in all editor windows.
1521 * @param h root widget for all windows
1525 edit_show_tabs_tws_cmd (Dlg_head
* h
)
1527 enable_show_tabs_tws
= !enable_show_tabs_tws
;
1528 g_list_foreach (h
->widgets
, edit_redraw_page_cb
, NULL
);
1532 /* --------------------------------------------------------------------------------------------- */
1534 * Toggle right margin showing in all editor windows.
1536 * @param h root widget for all windows
1540 edit_show_margin_cmd (Dlg_head
* h
)
1542 show_right_margin
= !show_right_margin
;
1543 g_list_foreach (h
->widgets
, edit_redraw_page_cb
, NULL
);
1547 /* --------------------------------------------------------------------------------------------- */
1549 * Toggle line numbers showing in all editor windows.
1551 * @param h root widget for all windows
1555 edit_show_numbers_cmd (Dlg_head
* h
)
1557 option_line_state
= !option_line_state
;
1558 option_line_state_width
= option_line_state
? LINE_STATE_WIDTH
: 0;
1559 g_list_foreach (h
->widgets
, edit_redraw_page_cb
, NULL
);
1563 /* --------------------------------------------------------------------------------------------- */
1566 edit_save_mode_cmd (void)
1569 const int DLG_X
= 38;
1570 const int DLG_Y
= 13;
1574 const char *str
[] = {
1577 N_("&Do backups with following extension:")
1580 QuickWidget widgets
[] = {
1582 QUICK_BUTTON (18, DLG_X
, DLG_Y
- 3, DLG_Y
, N_("&Cancel"), B_CANCEL
, NULL
),
1584 QUICK_BUTTON (6, DLG_X
, DLG_Y
- 3, DLG_Y
, N_("&OK"), B_ENTER
, NULL
),
1586 QUICK_CHECKBOX (4, DLG_X
, 8, DLG_Y
, N_("Check &POSIX new line"), &option_check_nl_at_eof
),
1588 QUICK_INPUT (8, DLG_X
, 6, DLG_Y
, option_backup_ext
, 9, 0, "edit-backup-ext", &str_result
),
1590 QUICK_RADIO (4, DLG_X
, 3, DLG_Y
, 3, str
, &option_save_mode
),
1594 QuickDialog dialog
= {
1595 DLG_X
, DLG_Y
, -1, -1, N_("Edit Save Mode"),
1596 "[Edit Save Mode]", widgets
, NULL
, NULL
, FALSE
1601 size_t w0
, w1
, b_len
, w3
;
1603 #ifdef HAVE_ASSERT_H
1604 assert (option_backup_ext
!= NULL
);
1607 /* OK/Cancel buttons */
1608 w0
= str_term_width1 (_(widgets
[0].u
.button
.text
)) + 3;
1609 w1
= str_term_width1 (_(widgets
[1].u
.button
.text
)) + 5; /* default button */
1610 b_len
= w0
+ w1
+ 3;
1612 maxlen
= max (b_len
, (size_t) str_term_width1 (_(dialog
.title
)) + 2);
1615 for (i
= 0; i
< 3; i
++)
1620 w3
= max (w3
, (size_t) str_term_width1 (str
[i
]));
1623 maxlen
= max (maxlen
, w3
+ 4);
1625 dialog
.xlen
= min ((size_t) COLS
, maxlen
+ 8);
1627 widgets
[3].u
.input
.len
= w3
;
1628 widgets
[1].relative_x
= (dialog
.xlen
- b_len
) / 2;
1629 widgets
[0].relative_x
= widgets
[1].relative_x
+ w0
+ 2;
1631 for (i
= 0; i
< sizeof (widgets
) / sizeof (widgets
[0]); i
++)
1632 widgets
[i
].x_divisions
= dialog
.xlen
;
1634 if (quick_dialog (&dialog
) != B_CANCEL
)
1636 g_free (option_backup_ext
);
1637 option_backup_ext
= str_result
;
1641 /* --------------------------------------------------------------------------------------------- */
1644 edit_set_filename (WEdit
* edit
, const vfs_path_t
* name_vpath
)
1646 vfs_path_free (edit
->filename_vpath
);
1647 edit
->filename_vpath
= vfs_path_clone (name_vpath
);
1649 if (edit
->dir_vpath
== NULL
)
1650 edit
->dir_vpath
= vfs_path_clone (vfs_get_raw_current_dir ());
1653 /* --------------------------------------------------------------------------------------------- */
1654 /* Here we want to warn the users of overwriting an existing file,
1655 but only if they have made a change to the filename */
1656 /* returns TRUE on success */
1658 edit_save_as_cmd (WEdit
* edit
)
1660 /* This heads the 'Save As' dialog box */
1661 vfs_path_t
*exp_vpath
;
1663 int different_filename
= 0;
1665 if (!edit_check_newline (edit
))
1668 exp_vpath
= edit_get_save_file_as (edit
);
1669 edit_push_undo_action (edit
, KEY_PRESS
+ edit
->start_display
);
1671 if (exp_vpath
!= NULL
)
1673 if (vfs_path_len (exp_vpath
) == 0)
1679 if (vfs_path_cmp (edit
->filename_vpath
, exp_vpath
) != 0)
1684 if (mc_stat (exp_vpath
, &sb
) == 0 && !S_ISREG (sb
.st_mode
))
1686 edit_error_dialog (_("Save as"),
1688 ("Cannot save: destination is not a regular file")));
1692 different_filename
= 1;
1693 file
= mc_open (exp_vpath
, O_RDONLY
| O_BINARY
);
1697 /* the file exists */
1699 /* Overwrite the current file or cancel the operation */
1700 if (edit_query_dialog2
1702 _("A file already exists with this name"), _("&Overwrite"), _("&Cancel")))
1707 edit
->stat1
.st_mode
|= S_IWUSR
;
1709 save_lock
= lock_file (exp_vpath
);
1713 /* filenames equal, check if already locked */
1714 if (!edit
->locked
&& !edit
->delete_file
)
1715 save_lock
= lock_file (exp_vpath
);
1718 if (different_filename
)
1721 * Allow user to write into saved (under another name) file
1722 * even if original file had r/o user permissions.
1724 edit
->stat1
.st_mode
|= S_IWRITE
;
1727 rv
= edit_save_file (edit
, exp_vpath
);
1731 /* Succesful, so unlock both files */
1732 if (different_filename
)
1735 unlock_file (exp_vpath
);
1737 edit
->locked
= unlock_file (edit
->filename_vpath
);
1741 if (edit
->locked
|| save_lock
)
1742 edit
->locked
= unlock_file (edit
->filename_vpath
);
1745 edit_set_filename (edit
, exp_vpath
);
1746 if (edit
->lb
!= LB_ASIS
)
1747 edit_reload (edit
, exp_vpath
);
1749 edit
->delete_file
= 0;
1750 if (different_filename
)
1751 edit_load_syntax (edit
, NULL
, edit
->syntax_type
);
1752 vfs_path_free (exp_vpath
);
1753 edit
->force
|= REDRAW_COMPLETELY
;
1756 edit_error_dialog (_("Save as"), get_sys_error (_("Cannot save file")));
1759 /* Failed, so maintain modify (not save) lock */
1761 unlock_file (exp_vpath
);
1768 vfs_path_free (exp_vpath
);
1769 edit
->force
|= REDRAW_COMPLETELY
;
1773 /* {{{ Macro stuff starts here */
1774 /* --------------------------------------------------------------------------------------------- */
1777 edit_delete_macro_cmd (WEdit
* edit
)
1781 hotkey
= editcmd_dialog_raw_key_query (_("Delete macro"), _("Press macro hotkey:"), TRUE
);
1783 if (hotkey
!= 0 && !edit_delete_macro (edit
, hotkey
))
1784 message (D_ERROR
, _("Delete macro"), _("Macro not deleted"));
1787 /* --------------------------------------------------------------------------------------------- */
1789 /** returns FALSE on error */
1791 edit_execute_macro (WEdit
* edit
, int hotkey
)
1793 gboolean res
= FALSE
;
1797 const macros_t
*macros
;
1800 if (edit_get_macro (edit
, hotkey
, ¯os
, &indx
) &&
1801 macros
->macro
!= NULL
&& macros
->macro
->len
!= 0)
1805 edit
->force
|= REDRAW_PAGE
;
1807 for (i
= 0; i
< macros
->macro
->len
; i
++)
1809 const macro_action_t
*m_act
;
1811 m_act
= &g_array_index (macros
->macro
, struct macro_action_t
, i
);
1812 edit_execute_cmd (edit
, m_act
->action
, m_act
->ch
);
1821 /* --------------------------------------------------------------------------------------------- */
1823 /** returns FALSE on error */
1825 edit_store_macro_cmd (WEdit
* edit
)
1829 GString
*marcros_string
;
1830 mc_config_t
*macros_config
= NULL
;
1831 const char *section_name
= "editor";
1832 gchar
*macros_fname
;
1833 GArray
*macros
; /* current macro */
1835 gboolean have_macro
= FALSE
;
1836 char *skeyname
= NULL
;
1839 editcmd_dialog_raw_key_query (_("Save macro"), _("Press the macro's new hotkey:"), TRUE
);
1840 if (hotkey
== ESC_CHAR
)
1843 tmp_act
= keybind_lookup_keymap_command (editor_map
, hotkey
);
1845 /* return FALSE if try assign macro into restricted hotkeys */
1846 if (tmp_act
== CK_MacroStartRecord
1847 || tmp_act
== CK_MacroStopRecord
|| tmp_act
== CK_MacroStartStopRecord
)
1850 edit_delete_macro (edit
, hotkey
);
1852 macros_fname
= mc_config_get_full_path (MC_MACRO_FILE
);
1853 macros_config
= mc_config_init (macros_fname
, FALSE
);
1854 g_free (macros_fname
);
1856 if (macros_config
== NULL
)
1859 edit_push_undo_action (edit
, KEY_PRESS
+ edit
->start_display
);
1861 marcros_string
= g_string_sized_new (250);
1862 macros
= g_array_new (TRUE
, FALSE
, sizeof (macro_action_t
));
1864 skeyname
= lookup_key_by_code (hotkey
);
1866 for (i
= 0; i
< macro_index
; i
++)
1868 macro_action_t m_act
;
1869 const char *action_name
;
1871 action_name
= keybind_lookup_actionname (record_macro_buf
[i
].action
);
1873 if (action_name
== NULL
)
1876 m_act
.action
= record_macro_buf
[i
].action
;
1877 m_act
.ch
= record_macro_buf
[i
].ch
;
1878 g_array_append_val (macros
, m_act
);
1880 g_string_append_printf (marcros_string
, "%s:%i;", action_name
,
1881 (int) record_macro_buf
[i
].ch
);
1886 macro
.hotkey
= hotkey
;
1887 macro
.macro
= macros
;
1888 g_array_append_val (macros_list
, macro
);
1889 mc_config_set_string (macros_config
, section_name
, skeyname
, marcros_string
->str
);
1892 mc_config_del_key (macros_config
, section_name
, skeyname
);
1895 edit_macro_sort_by_hotkey ();
1897 g_string_free (marcros_string
, TRUE
);
1898 mc_config_save_file (macros_config
, NULL
);
1899 mc_config_deinit (macros_config
);
1903 /* --------------------------------------------------------------------------------------------- */
1906 edit_repeat_macro_cmd (WEdit
* edit
)
1913 f
= input_dialog (_("Repeat last commands"), _("Repeat times:"), MC_HISTORY_EDIT_REPEAT
, NULL
);
1914 if (f
== NULL
|| *f
== '\0')
1920 count_repeat
= strtol (f
, &error
, 0);
1930 edit_push_undo_action (edit
, KEY_PRESS
+ edit
->start_display
);
1931 edit
->force
|= REDRAW_PAGE
;
1933 for (j
= 0; j
< count_repeat
; j
++)
1934 for (i
= 0; i
< macro_index
; i
++)
1935 edit_execute_cmd (edit
, record_macro_buf
[i
].action
, record_macro_buf
[i
].ch
);
1936 edit_update_screen (edit
);
1940 /* --------------------------------------------------------------------------------------------- */
1941 /** return FALSE on error */
1944 edit_load_macro_cmd (WEdit
* edit
)
1946 mc_config_t
*macros_config
= NULL
;
1947 gchar
**profile_keys
, **keys
;
1948 gchar
**values
, **curr_values
;
1949 gsize len
, values_len
;
1950 const char *section_name
= "editor";
1951 gchar
*macros_fname
;
1956 macros_fname
= mc_config_get_full_path (MC_MACRO_FILE
);
1957 macros_config
= mc_config_init (macros_fname
, TRUE
);
1958 g_free (macros_fname
);
1960 if (macros_config
== NULL
)
1963 profile_keys
= keys
= mc_config_get_keys (macros_config
, section_name
, &len
);
1964 while (*profile_keys
!= NULL
)
1966 gboolean have_macro
;
1970 macros
= g_array_new (TRUE
, FALSE
, sizeof (macro_action_t
));
1972 curr_values
= values
= mc_config_get_string_list (macros_config
, section_name
,
1973 *profile_keys
, &values_len
);
1974 hotkey
= lookup_key (*profile_keys
, NULL
);
1977 while (*curr_values
!= NULL
&& *curr_values
[0] != '\0')
1979 char **macro_pair
= NULL
;
1981 macro_pair
= g_strsplit (*curr_values
, ":", 2);
1983 if (macro_pair
!= NULL
)
1985 macro_action_t m_act
;
1986 if (macro_pair
[0] == NULL
|| macro_pair
[0][0] == '\0')
1990 m_act
.action
= keybind_lookup_action (macro_pair
[0]);
1991 g_free (macro_pair
[0]);
1992 macro_pair
[0] = NULL
;
1994 if (macro_pair
[1] == NULL
|| macro_pair
[1][0] == '\0')
1998 m_act
.ch
= strtol (macro_pair
[1], NULL
, 0);
1999 g_free (macro_pair
[1]);
2000 macro_pair
[1] = NULL
;
2002 if (m_act
.action
!= 0)
2004 /* a shell command */
2005 if ((m_act
.action
/ CK_PipeBlock (0)) == 1)
2007 m_act
.action
= CK_PipeBlock (0) + (m_act
.ch
> 0 ? m_act
.ch
: 0);
2010 g_array_append_val (macros
, m_act
);
2013 g_strfreev (macro_pair
);
2020 macro
.hotkey
= hotkey
;
2021 macro
.macro
= macros
;
2022 g_array_append_val (macros_list
, macro
);
2025 g_strfreev (values
);
2028 mc_config_deinit (macros_config
);
2029 edit_macro_sort_by_hotkey ();
2033 /* }}} Macro stuff end here */
2035 /* --------------------------------------------------------------------------------------------- */
2036 /** returns TRUE on success */
2039 edit_save_confirm_cmd (WEdit
* edit
)
2043 if (edit
->filename_vpath
== NULL
)
2044 return edit_save_as_cmd (edit
);
2046 if (!edit_check_newline (edit
))
2049 if (edit_confirm_save
)
2054 filename
= vfs_path_to_str (edit
->filename_vpath
);
2055 f
= g_strdup_printf (_("Confirm save file: \"%s\""), filename
);
2057 ok
= (edit_query_dialog2 (_("Save file"), f
, _("&Save"), _("&Cancel")) == 0);
2062 return edit_save_cmd (edit
);
2065 /* --------------------------------------------------------------------------------------------- */
2067 * Ask file to edit and load it.
2069 * @returns TRUE on success or cancel of ask.
2073 edit_load_cmd (Dlg_head
* h
)
2076 gboolean ret
= TRUE
; /* possible cancel */
2078 exp
= input_expand_dialog (_("Load"), _("Enter file name:"),
2079 MC_HISTORY_EDIT_LOAD
, INPUT_LAST_TEXT
);
2081 if (exp
!= NULL
&& *exp
!= '\0')
2083 vfs_path_t
*exp_vpath
;
2085 exp_vpath
= vfs_path_from_str (exp
);
2086 ret
= edit_load_file_from_filename (h
, exp_vpath
);
2087 vfs_path_free (exp_vpath
);
2095 /* --------------------------------------------------------------------------------------------- */
2097 * Load syntax file to edit.
2099 * @returns TRUE on success
2103 edit_load_syntax_file (Dlg_head
* h
)
2105 vfs_path_t
*extdir_vpath
;
2107 gboolean ret
= FALSE
;
2109 if (geteuid () == 0)
2110 dir
= query_dialog (_("Syntax file edit"),
2111 _("Which syntax file you want to edit?"), D_NORMAL
, 2,
2112 _("&User"), _("&System wide"));
2115 vfs_path_build_filename (mc_global
.sysconfig_dir
, "syntax", "Syntax", (char *) NULL
);
2116 if (!exist_file (vfs_path_get_last_path_str (extdir_vpath
)))
2118 vfs_path_free (extdir_vpath
);
2120 vfs_path_build_filename (mc_global
.share_data_dir
, "syntax", "Syntax", (char *) NULL
);
2125 vfs_path_t
*user_syntax_file_vpath
;
2127 user_syntax_file_vpath
= mc_config_get_full_vpath (EDIT_SYNTAX_FILE
);
2128 check_for_default (extdir_vpath
, user_syntax_file_vpath
);
2129 ret
= edit_load_file_from_filename (h
, user_syntax_file_vpath
);
2130 vfs_path_free (user_syntax_file_vpath
);
2133 ret
= edit_load_file_from_filename (h
, extdir_vpath
);
2135 vfs_path_free (extdir_vpath
);
2140 /* --------------------------------------------------------------------------------------------- */
2142 * Load menu file to edit.
2144 * @returns TRUE on success
2148 edit_load_menu_file (Dlg_head
* h
)
2150 vfs_path_t
*buffer_vpath
;
2151 vfs_path_t
*menufile_vpath
;
2155 dir
= query_dialog (_("Menu edit"),
2156 _("Which menu file do you want to edit?"), D_NORMAL
,
2157 geteuid () != 0 ? 2 : 3, _("&Local"), _("&User"), _("&System wide"));
2159 menufile_vpath
= vfs_path_build_filename (mc_global
.sysconfig_dir
, EDIT_GLOBAL_MENU
, NULL
);
2160 if (!exist_file (vfs_path_get_last_path_str (menufile_vpath
)))
2162 vfs_path_free (menufile_vpath
);
2163 menufile_vpath
= vfs_path_build_filename (mc_global
.share_data_dir
, EDIT_GLOBAL_MENU
, NULL
);
2169 buffer_vpath
= vfs_path_from_str (EDIT_LOCAL_MENU
);
2170 check_for_default (menufile_vpath
, buffer_vpath
);
2171 chmod (vfs_path_get_last_path_str (buffer_vpath
), 0600);
2175 buffer_vpath
= mc_config_get_full_vpath (EDIT_HOME_MENU
);
2176 check_for_default (menufile_vpath
, buffer_vpath
);
2180 buffer_vpath
= vfs_path_build_filename (mc_global
.sysconfig_dir
, EDIT_GLOBAL_MENU
, NULL
);
2181 if (!exist_file (vfs_path_get_last_path_str (buffer_vpath
)))
2183 vfs_path_free (buffer_vpath
);
2185 vfs_path_build_filename (mc_global
.share_data_dir
, EDIT_GLOBAL_MENU
, NULL
);
2190 vfs_path_free (menufile_vpath
);
2194 ret
= edit_load_file_from_filename (h
, buffer_vpath
);
2196 vfs_path_free (buffer_vpath
);
2197 vfs_path_free (menufile_vpath
);
2202 /* --------------------------------------------------------------------------------------------- */
2204 * Close window with opened file.
2206 * @returns TRUE if file was closed.
2210 edit_close_cmd (WEdit
* edit
)
2214 ret
= (edit
!= NULL
) && edit_ok_to_exit (edit
);
2218 Dlg_head
*h
= ((Widget
*) edit
)->owner
;
2220 if (edit
->locked
!= 0)
2221 unlock_file (edit
->filename_vpath
);
2225 if (edit_widget_is_editor ((Widget
*) h
->current
->data
))
2226 edit
= (WEdit
*) h
->current
->data
;
2229 edit
= find_editor (h
);
2231 dlg_set_top_widget (edit
);
2236 edit
->force
|= REDRAW_COMPLETELY
;
2241 /* --------------------------------------------------------------------------------------------- */
2243 if mark2 is -1 then marking is from mark1 to the cursor.
2244 Otherwise its between the markers. This handles this.
2245 Returns 1 if no text is marked.
2249 eval_marks (WEdit
* edit
, off_t
* start_mark
, off_t
* end_mark
)
2251 if (edit
->mark1
!= edit
->mark2
)
2253 off_t start_bol
, start_eol
;
2254 off_t end_bol
, end_eol
;
2259 if (edit
->end_mark_curs
< 0)
2260 end_mark_curs
= edit
->curs1
;
2262 end_mark_curs
= edit
->end_mark_curs
;
2264 if (edit
->mark2
>= 0)
2266 *start_mark
= min (edit
->mark1
, edit
->mark2
);
2267 *end_mark
= max (edit
->mark1
, edit
->mark2
);
2271 *start_mark
= min (edit
->mark1
, end_mark_curs
);
2272 *end_mark
= max (edit
->mark1
, end_mark_curs
);
2273 edit
->column2
= edit
->curs_col
+ edit
->over_col
;
2276 if (edit
->column_highlight
2277 && (((edit
->mark1
> end_mark_curs
) && (edit
->column1
< edit
->column2
))
2278 || ((edit
->mark1
< end_mark_curs
) && (edit
->column1
> edit
->column2
))))
2280 start_bol
= edit_bol (edit
, *start_mark
);
2281 start_eol
= edit_eol (edit
, start_bol
- 1) + 1;
2282 end_bol
= edit_bol (edit
, *end_mark
);
2283 end_eol
= edit_eol (edit
, *end_mark
);
2284 col1
= min (edit
->column1
, edit
->column2
);
2285 col2
= max (edit
->column1
, edit
->column2
);
2287 diff1
= edit_move_forward3 (edit
, start_bol
, col2
, 0) -
2288 edit_move_forward3 (edit
, start_bol
, col1
, 0);
2289 diff2
= edit_move_forward3 (edit
, end_bol
, col2
, 0) -
2290 edit_move_forward3 (edit
, end_bol
, col1
, 0);
2292 *start_mark
-= diff1
;
2294 *start_mark
= max (*start_mark
, start_eol
);
2295 *end_mark
= min (*end_mark
, end_eol
);
2301 *start_mark
= *end_mark
= 0;
2302 edit
->column2
= edit
->column1
= 0;
2307 /* --------------------------------------------------------------------------------------------- */
2310 edit_insert_over (WEdit
* edit
)
2314 for (i
= 0; i
< edit
->over_col
; i
++)
2316 edit_insert (edit
, ' ');
2321 /* --------------------------------------------------------------------------------------------- */
2324 edit_insert_column_of_text_from_file (WEdit
* edit
, int file
,
2325 off_t
* start_pos
, off_t
* end_pos
, long *col1
, long *col2
)
2329 off_t blocklen
= -1, width
= 0;
2330 unsigned char *data
;
2332 cursor
= edit
->curs1
;
2333 col
= edit_get_col (edit
);
2334 data
= g_malloc0 (TEMP_BUF_LEN
);
2336 while ((blocklen
= mc_read (file
, (char *) data
, TEMP_BUF_LEN
)) > 0)
2339 for (width
= 0; width
< blocklen
; width
++)
2341 if (data
[width
] == '\n')
2344 for (i
= 0; i
< blocklen
; i
++)
2346 if (data
[i
] == '\n')
2347 { /* fill in and move to next line */
2350 if (edit_get_byte (edit
, edit
->curs1
) != '\n')
2352 l
= width
- (edit_get_col (edit
) - col
);
2355 edit_insert (edit
, ' ');
2359 for (p
= edit
->curs1
;; p
++)
2361 if (p
== edit
->last_byte
)
2363 edit_cursor_move (edit
, edit
->last_byte
- edit
->curs1
);
2364 edit_insert_ahead (edit
, '\n');
2368 if (edit_get_byte (edit
, p
) == '\n')
2374 edit_cursor_move (edit
, edit_move_forward3 (edit
, p
, col
, 0) - edit
->curs1
);
2375 l
= col
- edit_get_col (edit
);
2376 while (l
>= space_width
)
2378 edit_insert (edit
, ' ');
2383 edit_insert (edit
, data
[i
]);
2387 *col2
= col
+ width
;
2388 *start_pos
= cursor
;
2389 *end_pos
= edit
->curs1
;
2390 edit_cursor_move (edit
, cursor
- edit
->curs1
);
2396 /* --------------------------------------------------------------------------------------------- */
2399 edit_block_copy_cmd (WEdit
* edit
)
2401 off_t start_mark
, end_mark
, current
= edit
->curs1
;
2406 unsigned char *copy_buf
;
2408 edit_update_curs_col (edit
);
2409 if (eval_marks (edit
, &start_mark
, &end_mark
))
2412 copy_buf
= edit_get_block (edit
, start_mark
, end_mark
, &size
);
2414 /* all that gets pushed are deletes hence little space is used on the stack */
2416 edit_push_markers (edit
);
2418 if (edit
->column_highlight
)
2420 col_delta
= abs (edit
->column2
- edit
->column1
);
2421 edit_insert_column_of_text (edit
, copy_buf
, size
, col_delta
, &mark1
, &mark2
, &c1
, &c2
);
2426 edit_insert_ahead (edit
, copy_buf
[size
]);
2430 edit_scroll_screen_over_cursor (edit
);
2432 if (edit
->column_highlight
)
2433 edit_set_markers (edit
, edit
->curs1
, mark2
, c1
, c2
);
2434 else if (start_mark
< current
&& end_mark
> current
)
2435 edit_set_markers (edit
, start_mark
, end_mark
+ end_mark
- start_mark
, 0, 0);
2437 edit
->force
|= REDRAW_PAGE
;
2441 /* --------------------------------------------------------------------------------------------- */
2444 edit_block_move_cmd (WEdit
* edit
)
2447 unsigned char *copy_buf
= NULL
;
2448 off_t start_mark
, end_mark
;
2451 if (eval_marks (edit
, &start_mark
, &end_mark
))
2454 line
= edit
->curs_line
;
2455 if (edit
->mark2
< 0)
2456 edit_mark_cmd (edit
, FALSE
);
2457 edit_push_markers (edit
);
2459 if (edit
->column_highlight
)
2467 c1
= min (edit
->column1
, edit
->column2
);
2468 c2
= max (edit
->column1
, edit
->column2
);
2469 b_width
= (c2
- c1
);
2471 edit_update_curs_col (edit
);
2474 x2
= x
+ edit
->over_col
;
2476 /* do nothing when cursor inside first line of selected area */
2477 if ((edit_eol (edit
, edit
->curs1
) == edit_eol (edit
, start_mark
)) && (x2
> c1
&& x2
<= c2
))
2480 if (edit
->curs1
> start_mark
&& edit
->curs1
< edit_eol (edit
, end_mark
))
2484 else if (x
> c1
&& x
<= c2
)
2487 /* save current selection into buffer */
2488 copy_buf
= edit_get_block (edit
, start_mark
, end_mark
, &size
);
2490 /* remove current selection */
2491 edit_block_delete_cmd (edit
);
2493 edit
->over_col
= max (0, edit
->over_col
- b_width
);
2494 /* calculate the cursor pos after delete block */
2495 current
= edit_move_forward3 (edit
, edit_bol (edit
, edit
->curs1
), x
, 0);
2496 edit_cursor_move (edit
, current
- edit
->curs1
);
2497 edit_scroll_screen_over_cursor (edit
);
2499 /* add TWS if need before block insertion */
2500 if (option_cursor_beyond_eol
&& edit
->over_col
> 0)
2501 edit_insert_over (edit
);
2503 edit_insert_column_of_text (edit
, copy_buf
, size
, b_width
, &mark1
, &mark2
, &c1
, &c2
);
2504 edit_set_markers (edit
, mark1
, mark2
, c1
, c2
);
2510 current
= edit
->curs1
;
2511 copy_buf
= g_malloc0 (end_mark
- start_mark
);
2512 edit_cursor_move (edit
, start_mark
- edit
->curs1
);
2513 edit_scroll_screen_over_cursor (edit
);
2515 while (count
< end_mark
)
2517 copy_buf
[end_mark
- count
- 1] = edit_delete (edit
, 1);
2520 edit_scroll_screen_over_cursor (edit
);
2521 edit_cursor_move (edit
,
2522 current
- edit
->curs1
-
2523 (((current
- edit
->curs1
) > 0) ? end_mark
- start_mark
: 0));
2524 edit_scroll_screen_over_cursor (edit
);
2525 while (count
-- > start_mark
)
2526 edit_insert_ahead (edit
, copy_buf
[end_mark
- count
- 1]);
2527 edit_set_markers (edit
, edit
->curs1
, edit
->curs1
+ end_mark
- start_mark
, 0, 0);
2530 edit_scroll_screen_over_cursor (edit
);
2532 edit
->force
|= REDRAW_PAGE
;
2535 /* --------------------------------------------------------------------------------------------- */
2536 /** returns 1 if canceelled by user */
2539 edit_block_delete_cmd (WEdit
* edit
)
2541 off_t start_mark
, end_mark
;
2542 if (eval_marks (edit
, &start_mark
, &end_mark
))
2544 edit_delete_line (edit
);
2547 return edit_block_delete (edit
);
2550 /* --------------------------------------------------------------------------------------------- */
2551 /** call with edit = 0 before shutdown to close memory leaks */
2554 edit_replace_cmd (WEdit
* edit
, int again
)
2556 /* 1 = search string, 2 = replace with */
2557 static char *saved1
= NULL
; /* saved default[123] */
2558 static char *saved2
= NULL
;
2559 char *input1
= NULL
; /* user input from the dialog */
2560 char *input2
= NULL
;
2561 GString
*input2_str
= NULL
;
2564 long times_replaced
= 0;
2565 gboolean once_found
= FALSE
;
2569 g_free (saved1
), saved1
= NULL
;
2570 g_free (saved2
), saved2
= NULL
;
2574 edit
->force
|= REDRAW_COMPLETELY
;
2576 if (again
&& !saved1
&& !saved2
)
2581 input1
= g_strdup (saved1
? saved1
: "");
2582 input2
= g_strdup (saved2
? saved2
: "");
2586 char *tmp_inp1
, *tmp_inp2
;
2588 disp1
= edit_replace_cmd__conv_to_display (saved1
? saved1
: (char *) "");
2589 disp2
= edit_replace_cmd__conv_to_display (saved2
? saved2
: (char *) "");
2591 edit_push_undo_action (edit
, KEY_PRESS
+ edit
->start_display
);
2593 editcmd_dialog_replace_show (edit
, disp1
, disp2
, &input1
, &input2
);
2598 if (input1
== NULL
|| *input1
== '\0')
2600 edit
->force
= REDRAW_COMPLETELY
;
2606 input1
= edit_replace_cmd__conv_to_input (input1
);
2607 input2
= edit_replace_cmd__conv_to_input (input2
);
2611 g_free (saved1
), saved1
= g_strdup (input1
);
2612 g_free (saved2
), saved2
= g_strdup (input2
);
2614 mc_search_free (edit
->search
);
2615 edit
->search
= NULL
;
2618 input2_str
= g_string_new (input2
);
2622 edit
->search
= mc_search_new (input1
, -1);
2623 if (edit
->search
== NULL
)
2625 edit
->search_start
= edit
->curs1
;
2628 edit
->search
->search_type
= edit_search_options
.type
;
2629 edit
->search
->is_all_charsets
= edit_search_options
.all_codepages
;
2630 edit
->search
->is_case_sensitive
= edit_search_options
.case_sens
;
2631 edit
->search
->whole_words
= edit_search_options
.whole_words
;
2632 edit
->search
->search_fn
= edit_search_cmd_callback
;
2633 edit
->search_line_type
= edit_get_search_line_type (edit
->search
);
2634 edit_search_fix_search_start_if_selection (edit
);
2637 if (edit
->found_len
&& edit
->search_start
== edit
->found_start
+ 1
2638 && edit_search_options
.backwards
)
2639 edit
->search_start
--;
2641 if (edit
->found_len
&& edit
->search_start
== edit
->found_start
- 1
2642 && !edit_search_options
.backwards
)
2643 edit
->search_start
++;
2649 if (!editcmd_find (edit
, &len
))
2651 if (!(edit
->search
->error
== MC_SEARCH_E_OK
||
2652 (once_found
&& edit
->search
->error
== MC_SEARCH_E_NOTFOUND
)))
2654 edit_error_dialog (_("Search"), edit
->search
->error_str
);
2660 edit
->search_start
= edit
->search
->normal_offset
;
2661 /*returns negative on not found or error in pattern */
2663 if ((edit
->search_start
>= 0) && (edit
->search_start
< edit
->last_byte
))
2668 edit
->found_start
= edit
->search_start
;
2669 i
= edit
->found_len
= len
;
2671 edit_cursor_move (edit
, edit
->search_start
- edit
->curs1
);
2672 edit_scroll_screen_over_cursor (edit
);
2674 if (edit
->replace_mode
== 0)
2679 l
= (long) (edit
->curs_row
- edit
->widget
.lines
/ 3);
2681 edit_scroll_downward (edit
, l
);
2683 edit_scroll_upward (edit
, -l
);
2685 edit_scroll_screen_over_cursor (edit
);
2686 edit
->force
|= REDRAW_PAGE
;
2687 edit_render_keypress (edit
);
2689 /*so that undo stops at each query */
2690 edit_push_key_press (edit
);
2691 /* and prompt 2/3 down */
2692 disp1
= edit_replace_cmd__conv_to_display (saved1
);
2693 disp2
= edit_replace_cmd__conv_to_display (saved2
);
2694 prompt
= editcmd_dialog_replace_prompt_show (edit
, disp1
, disp2
, -1, -1);
2698 if (prompt
== B_REPLACE_ALL
)
2699 edit
->replace_mode
= 1;
2700 else if (prompt
== B_SKIP_REPLACE
)
2702 if (edit_search_options
.backwards
)
2703 edit
->search_start
--;
2705 edit
->search_start
++;
2706 continue; /* loop */
2708 else if (prompt
== B_CANCEL
)
2710 edit
->replace_mode
= -1;
2715 repl_str
= mc_search_prepare_replace_str (edit
->search
, input2_str
);
2717 if (edit
->search
->error
!= MC_SEARCH_E_OK
)
2719 edit_error_dialog (_("Replace"), edit
->search
->error_str
);
2720 g_string_free (repl_str
, TRUE
);
2724 /* delete then insert new */
2725 for (i
= 0; i
< len
; i
++)
2726 edit_delete (edit
, 1);
2728 for (i
= 0; i
< repl_str
->len
; i
++)
2729 edit_insert (edit
, repl_str
->str
[i
]);
2731 edit
->found_len
= repl_str
->len
;
2732 g_string_free (repl_str
, TRUE
);
2735 /* so that we don't find the same string again */
2736 if (edit_search_options
.backwards
)
2738 edit
->search_start
--;
2742 edit
->search_start
+= edit
->found_len
+ (len
== 0 ? 1 : 0);
2744 if (edit
->search_start
>= edit
->last_byte
)
2748 edit_scroll_screen_over_cursor (edit
);
2752 /* try and find from right here for next search */
2753 edit
->search_start
= edit
->curs1
;
2754 edit_update_curs_col (edit
);
2756 edit
->force
|= REDRAW_PAGE
;
2757 edit_render_keypress (edit
);
2759 if (times_replaced
== 0)
2760 query_dialog (_("Replace"), _("Search string not found"), D_NORMAL
, 1, _("&OK"));
2764 while (edit
->replace_mode
>= 0);
2766 edit_scroll_screen_over_cursor (edit
);
2767 edit
->force
|= REDRAW_COMPLETELY
;
2768 edit_render_keypress (edit
);
2770 if ((edit
->replace_mode
== 1) && (times_replaced
!= 0))
2771 message (D_NORMAL
, _("Replace"), _("%ld replacements made"), times_replaced
);
2776 if (input2_str
!= NULL
)
2777 g_string_free (input2_str
, TRUE
);
2780 /* --------------------------------------------------------------------------------------------- */
2783 edit_search_cmd_callback (const void *user_data
, gsize char_offset
, int *current_char
)
2785 *current_char
= edit_get_byte ((WEdit
*) user_data
, (off_t
) char_offset
);
2786 return MC_SEARCH_CB_OK
;
2789 /* --------------------------------------------------------------------------------------------- */
2792 edit_search_cmd (WEdit
* edit
, gboolean again
)
2800 else if (edit
->last_search_string
!= NULL
)
2801 edit_do_search (edit
);
2804 /* find last search string in history */
2807 history
= history_get (MC_HISTORY_SHARED_SEARCH
);
2808 if (history
!= NULL
&& history
->data
!= NULL
)
2810 edit
->last_search_string
= (char *) history
->data
;
2811 history
->data
= NULL
;
2812 history
= g_list_first (history
);
2813 g_list_foreach (history
, (GFunc
) g_free
, NULL
);
2814 g_list_free (history
);
2816 edit
->search
= mc_search_new (edit
->last_search_string
, -1);
2817 if (edit
->search
== NULL
)
2819 /* if not... then ask for an expression */
2820 g_free (edit
->last_search_string
);
2821 edit
->last_search_string
= NULL
;
2826 edit
->search
->search_type
= edit_search_options
.type
;
2827 edit
->search
->is_all_charsets
= edit_search_options
.all_codepages
;
2828 edit
->search
->is_case_sensitive
= edit_search_options
.case_sens
;
2829 edit
->search
->whole_words
= edit_search_options
.whole_words
;
2830 edit
->search
->search_fn
= edit_search_cmd_callback
;
2831 edit
->search_line_type
= edit_get_search_line_type (edit
->search
);
2832 edit_do_search (edit
);
2837 /* if not... then ask for an expression */
2838 g_free (edit
->last_search_string
);
2839 edit
->last_search_string
= NULL
;
2846 /* --------------------------------------------------------------------------------------------- */
2848 * Check if it's OK to close the file. If there are unsaved changes, ask user.
2850 * @returns TRUE if it's OK to exit, FALSE to continue editing.
2854 edit_ok_to_exit (WEdit
* edit
)
2856 char *fname
= (char *) N_("[NoName]");
2860 if (!edit
->modified
)
2863 if (edit
->filename_vpath
!= NULL
)
2864 fname
= vfs_path_to_str (edit
->filename_vpath
);
2867 fname
= g_strdup (_(fname
));
2870 fname
= g_strdup (fname
);
2873 if (!mc_global
.midnight_shutdown
)
2875 if (!edit_check_newline (edit
))
2883 msg
= g_strdup_printf (_("File %s was modified.\nSave before close?"), fname
);
2884 act
= edit_query_dialog3 (_("Close file"), msg
, _("&Yes"), _("&No"), _("&Cancel"));
2888 msg
= g_strdup_printf (_("Midnight Commander is being shut down.\nSave modified file %s?"),
2890 act
= edit_query_dialog2 (_("Quit"), msg
, _("&Yes"), _("&No"));
2903 edit_push_markers (edit
);
2904 edit_set_markers (edit
, 0, 0, 0, 0);
2905 if (!edit_save_cmd (edit
) || mc_global
.midnight_shutdown
)
2906 return mc_global
.midnight_shutdown
;
2910 case 2: /* Cancel quit */
2918 /* --------------------------------------------------------------------------------------------- */
2919 /** save block, returns TRUE on success */
2922 edit_save_block (WEdit
* edit
, const char *filename
, off_t start
, off_t finish
)
2927 vpath
= vfs_path_from_str (filename
);
2928 file
= mc_open (vpath
, O_CREAT
| O_WRONLY
| O_TRUNC
,
2929 S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IROTH
| O_BINARY
);
2930 vfs_path_free (vpath
);
2934 if (edit
->column_highlight
)
2938 r
= mc_write (file
, VERTICAL_MAGIC
, sizeof (VERTICAL_MAGIC
));
2941 unsigned char *block
, *p
;
2943 p
= block
= edit_get_block (edit
, start
, finish
, &len
);
2946 r
= mc_write (file
, p
, len
);
2961 len
= finish
- start
;
2962 buf
= g_malloc0 (TEMP_BUF_LEN
);
2963 while (start
!= finish
)
2965 end
= min (finish
, start
+ TEMP_BUF_LEN
);
2966 for (; i
< end
; i
++)
2967 buf
[i
- start
] = edit_get_byte (edit
, i
);
2968 len
-= mc_write (file
, (char *) buf
, end
- start
);
2978 /* --------------------------------------------------------------------------------------------- */
2981 edit_paste_from_history (WEdit
* edit
)
2984 edit_error_dialog (_("Error"), _("This function is not implemented"));
2987 /* --------------------------------------------------------------------------------------------- */
2990 edit_copy_to_X_buf_cmd (WEdit
* edit
)
2992 off_t start_mark
, end_mark
;
2994 if (eval_marks (edit
, &start_mark
, &end_mark
))
2996 if (!edit_save_block_to_clip_file (edit
, start_mark
, end_mark
))
2998 edit_error_dialog (_("Copy to clipboard"), get_sys_error (_("Unable to save to file")));
3001 /* try use external clipboard utility */
3002 mc_event_raise (MCEVENT_GROUP_CORE
, "clipboard_file_to_ext_clip", NULL
);
3007 /* --------------------------------------------------------------------------------------------- */
3010 edit_cut_to_X_buf_cmd (WEdit
* edit
)
3012 off_t start_mark
, end_mark
;
3014 if (eval_marks (edit
, &start_mark
, &end_mark
))
3016 if (!edit_save_block_to_clip_file (edit
, start_mark
, end_mark
))
3018 edit_error_dialog (_("Cut to clipboard"), _("Unable to save to file"));
3021 /* try use external clipboard utility */
3022 mc_event_raise (MCEVENT_GROUP_CORE
, "clipboard_file_to_ext_clip", NULL
);
3024 edit_block_delete_cmd (edit
);
3025 edit_mark_cmd (edit
, TRUE
);
3030 /* --------------------------------------------------------------------------------------------- */
3033 edit_paste_from_X_buf_cmd (WEdit
* edit
)
3038 /* try use external clipboard utility */
3039 mc_event_raise (MCEVENT_GROUP_CORE
, "clipboard_file_from_ext_clip", NULL
);
3040 tmp
= mc_config_get_full_vpath (EDIT_CLIP_FILE
);
3041 ret
= (edit_insert_file (edit
, tmp
) >= 0);
3042 vfs_path_free (tmp
);
3047 /* --------------------------------------------------------------------------------------------- */
3049 * Ask user for the line and go to that line.
3050 * Negative numbers mean line from the end (i.e. -1 is the last line).
3054 edit_goto_cmd (WEdit
* edit
)
3057 static long line
= 0; /* line as typed, saved as default */
3062 g_snprintf (s
, sizeof (s
), "%ld", line
);
3063 f
= input_dialog (_("Goto line"), _("Enter line:"), MC_HISTORY_EDIT_GOTO_LINE
, line
? s
: "");
3073 l
= strtol (f
, &error
, 0);
3082 l
= edit
->total_lines
+ l
+ 2;
3083 edit_move_display (edit
, l
- edit
->widget
.lines
/ 2 - 1);
3084 edit_move_to_line (edit
, l
- 1);
3085 edit
->force
|= REDRAW_COMPLETELY
;
3090 /* --------------------------------------------------------------------------------------------- */
3091 /** Return TRUE on success */
3094 edit_save_block_cmd (WEdit
* edit
)
3096 off_t start_mark
, end_mark
;
3098 gboolean ret
= FALSE
;
3100 if (eval_marks (edit
, &start_mark
, &end_mark
))
3103 tmp
= mc_config_get_full_path (EDIT_CLIP_FILE
);
3105 input_expand_dialog (_("Save block"), _("Enter file name:"),
3106 MC_HISTORY_EDIT_SAVE_BLOCK
, tmp
);
3108 edit_push_undo_action (edit
, KEY_PRESS
+ edit
->start_display
);
3110 if (exp
!= NULL
&& *exp
!= '\0')
3112 if (edit_save_block (edit
, exp
, start_mark
, end_mark
))
3115 edit_error_dialog (_("Save block"), get_sys_error (_("Cannot save file")));
3117 edit
->force
|= REDRAW_COMPLETELY
;
3126 /* --------------------------------------------------------------------------------------------- */
3128 /** returns TRUE on success */
3130 edit_insert_file_cmd (WEdit
* edit
)
3134 gboolean ret
= FALSE
;
3136 tmp
= mc_config_get_full_path (EDIT_CLIP_FILE
);
3137 exp
= input_expand_dialog (_("Insert file"), _("Enter file name:"),
3138 MC_HISTORY_EDIT_INSERT_FILE
, tmp
);
3141 edit_push_undo_action (edit
, KEY_PRESS
+ edit
->start_display
);
3143 if (exp
!= NULL
&& *exp
!= '\0')
3145 vfs_path_t
*exp_vpath
;
3147 exp_vpath
= vfs_path_from_str (exp
);
3148 ret
= (edit_insert_file (edit
, exp_vpath
) >= 0);
3149 vfs_path_free (exp_vpath
);
3152 edit_error_dialog (_("Insert file"), get_sys_error (_("Cannot insert file")));
3157 edit
->force
|= REDRAW_COMPLETELY
;
3161 /* --------------------------------------------------------------------------------------------- */
3162 /** sorts a block, returns -1 on system fail, 1 on cancel and 0 on success */
3165 edit_sort_cmd (WEdit
* edit
)
3167 static char *old
= 0;
3168 char *exp
, *tmp
, *tmp_edit_block_name
, *tmp_edit_temp_name
;
3169 off_t start_mark
, end_mark
;
3172 if (eval_marks (edit
, &start_mark
, &end_mark
))
3174 edit_error_dialog (_("Sort block"), _("You must first highlight a block of text"));
3178 tmp
= mc_config_get_full_path (EDIT_BLOCK_FILE
);
3179 edit_save_block (edit
, tmp
, start_mark
, end_mark
);
3182 exp
= input_dialog (_("Run sort"),
3183 _("Enter sort options (see manpage) separated by whitespace:"),
3184 MC_HISTORY_EDIT_SORT
, (old
!= NULL
) ? old
: "");
3190 tmp_edit_block_name
= mc_config_get_full_path (EDIT_BLOCK_FILE
);
3191 tmp_edit_temp_name
= mc_config_get_full_path (EDIT_TEMP_FILE
);
3193 g_strconcat (" sort ", exp
, " ", tmp_edit_block_name
,
3194 " > ", tmp_edit_temp_name
, (char *) NULL
);
3195 g_free (tmp_edit_temp_name
);
3196 g_free (tmp_edit_block_name
);
3202 if (e
== -1 || e
== 127)
3204 edit_error_dialog (_("Sort"), get_sys_error (_("Cannot execute sort command")));
3209 sprintf (q
, "%d ", e
);
3210 tmp
= g_strdup_printf (_("Sort returned non-zero: %s"), q
);
3211 edit_error_dialog (_("Sort"), tmp
);
3217 edit
->force
|= REDRAW_COMPLETELY
;
3219 if (edit_block_delete_cmd (edit
))
3223 vfs_path_t
*tmp_vpath
;
3225 tmp_vpath
= mc_config_get_full_vpath (EDIT_TEMP_FILE
);
3226 edit_insert_file (edit
, tmp_vpath
);
3227 vfs_path_free (tmp_vpath
);
3232 /* --------------------------------------------------------------------------------------------- */
3234 * Ask user for a command, execute it and paste its output back to the
3239 edit_ext_cmd (WEdit
* edit
)
3241 char *exp
, *tmp
, *tmp_edit_temp_file
;
3245 input_dialog (_("Paste output of external command"),
3246 _("Enter shell command(s):"), MC_HISTORY_EDIT_PASTE_EXTCMD
, NULL
);
3251 tmp_edit_temp_file
= mc_config_get_full_path (EDIT_TEMP_FILE
);
3252 tmp
= g_strconcat (exp
, " > ", tmp_edit_temp_file
, (char *) NULL
);
3253 g_free (tmp_edit_temp_file
);
3260 edit_error_dialog (_("External command"), get_sys_error (_("Cannot execute command")));
3264 edit
->force
|= REDRAW_COMPLETELY
;
3267 vfs_path_t
*tmp_vpath
;
3269 tmp_vpath
= mc_config_get_full_vpath (EDIT_TEMP_FILE
);
3270 edit_insert_file (edit
, tmp_vpath
);
3271 vfs_path_free (tmp_vpath
);
3276 /* --------------------------------------------------------------------------------------------- */
3277 /** if block is 1, a block must be highlighted and the shell command
3278 processes it. If block is 0 the shell command is a straight system
3279 command, that just produces some output which is to be inserted */
3282 edit_block_process_cmd (WEdit
* edit
, int macro_number
)
3285 char *macros_fname
= NULL
;
3287 fname
= g_strdup_printf ("%s.%i.sh", MC_EXTMACRO_FILE
, macro_number
);
3288 macros_fname
= g_build_filename (mc_config_get_data_path (), fname
, (char *) NULL
);
3289 user_menu (edit
, macros_fname
, 0);
3291 g_free (macros_fname
);
3292 edit
->force
|= REDRAW_COMPLETELY
;
3295 /* --------------------------------------------------------------------------------------------- */
3298 edit_mail_dialog (WEdit
* edit
)
3301 char *tmail_subject
;
3304 static char *mail_cc_last
= 0;
3305 static char *mail_subject_last
= 0;
3306 static char *mail_to_last
= 0;
3308 QuickWidget quick_widgets
[] = {
3309 /* 0 */ QUICK_BUTTON (6, 10, 9, MAIL_DLG_HEIGHT
, N_("&Cancel"), B_CANCEL
, NULL
),
3310 /* 1 */ QUICK_BUTTON (2, 10, 9, MAIL_DLG_HEIGHT
, N_("&OK"), B_ENTER
, NULL
),
3311 /* 2 */ QUICK_INPUT (3, 50, 8, MAIL_DLG_HEIGHT
, "", 44, 0, "mail-dlg-input", &tmail_cc
),
3312 /* 3 */ QUICK_LABEL (3, 50, 7, MAIL_DLG_HEIGHT
, N_("Copies to")),
3313 /* 4 */ QUICK_INPUT (3, 50, 6, MAIL_DLG_HEIGHT
, "", 44, 0, "mail-dlg-input-2",
3315 /* 5 */ QUICK_LABEL (3, 50, 5, MAIL_DLG_HEIGHT
, N_("Subject")),
3316 /* 6 */ QUICK_INPUT (3, 50, 4, MAIL_DLG_HEIGHT
, "", 44, 0, "mail-dlg-input-3", &tmail_to
),
3317 /* 7 */ QUICK_LABEL (3, 50, 3, MAIL_DLG_HEIGHT
, N_("To")),
3318 /* 8 */ QUICK_LABEL (3, 50, 2, MAIL_DLG_HEIGHT
, N_("mail -s <subject> -c <cc> <to>")),
3322 QuickDialog Quick_input
= {
3323 50, MAIL_DLG_HEIGHT
, -1, -1, N_("Mail"),
3324 "[Input Line Keys]", quick_widgets
, NULL
, NULL
, FALSE
3327 quick_widgets
[2].u
.input
.text
= mail_cc_last
? mail_cc_last
: "";
3328 quick_widgets
[4].u
.input
.text
= mail_subject_last
? mail_subject_last
: "";
3329 quick_widgets
[6].u
.input
.text
= mail_to_last
? mail_to_last
: "";
3331 if (quick_dialog (&Quick_input
) != B_CANCEL
)
3333 g_free (mail_cc_last
);
3334 g_free (mail_subject_last
);
3335 g_free (mail_to_last
);
3336 mail_cc_last
= tmail_cc
;
3337 mail_subject_last
= tmail_subject
;
3338 mail_to_last
= tmail_to
;
3339 pipe_mail (edit
, mail_to_last
, mail_subject_last
, mail_cc_last
);
3344 /*******************/
3345 /* Word Completion */
3346 /*******************/
3348 /* --------------------------------------------------------------------------------------------- */
3350 * Complete current word using regular expression search
3351 * backwards beginning at the current cursor position.
3355 edit_complete_word_cmd (WEdit
* edit
)
3357 gsize i
, max_len
, word_len
= 0, num_compl
= 0;
3358 off_t word_start
= 0;
3359 unsigned char *bufpos
;
3361 struct selection
compl[MAX_WORD_COMPLETIONS
]; /* completions */
3363 /* search start of word to be completed */
3364 if (!edit_find_word_start (edit
, &word_start
, &word_len
))
3367 /* prepare match expression */
3368 bufpos
= &edit
->buffers1
[word_start
>> S_EDIT_BUF_SIZE
][word_start
& M_EDIT_BUF_SIZE
];
3370 /* match_expr = g_strdup_printf ("\\b%.*s[a-zA-Z_0-9]+", word_len, bufpos); */
3373 ("(^|\\s+|\\b)%.*s[^\\s\\.=\\+\\[\\]\\(\\)\\,\\;\\:\\\"\\'\\-\\?\\/\\|\\\\\\{\\}\\*\\&\\^\\%%\\$#@\\!]+",
3374 (int) word_len
, bufpos
);
3376 /* collect the possible completions */
3377 /* start search from begin to end of file */
3379 edit_collect_completions (edit
, word_start
, word_len
, match_expr
,
3380 (struct selection
*) &compl, &num_compl
);
3384 /* insert completed word if there is only one match */
3387 for (i
= word_len
; i
< compl[0].len
; i
++)
3388 edit_insert (edit
, *(compl[0].text
+ i
));
3390 /* more than one possible completion => ask the user */
3393 /* !!! usually only a beep is expected and when <ALT-TAB> is !!! */
3394 /* !!! pressed again the selection dialog pops up, but that !!! */
3395 /* !!! seems to require a further internal state !!! */
3398 /* let the user select the preferred completion */
3399 editcmd_dialog_completion_show (edit
, max_len
, word_len
,
3400 (struct selection
*) &compl, num_compl
);
3404 g_free (match_expr
);
3405 /* release memory before return */
3406 for (i
= 0; i
< num_compl
; i
++)
3407 g_free (compl[i
].text
);
3410 /* --------------------------------------------------------------------------------------------- */
3414 edit_select_codepage_cmd (WEdit
* edit
)
3416 if (do_select_codepage ())
3417 edit_set_codeset (edit
);
3419 edit
->force
= REDRAW_PAGE
;
3420 send_message ((Widget
*) edit
, WIDGET_DRAW
, 0);
3424 /* --------------------------------------------------------------------------------------------- */
3427 edit_insert_literal_cmd (WEdit
* edit
)
3429 int char_for_insertion
;
3431 char_for_insertion
= editcmd_dialog_raw_key_query (_("Insert literal"),
3432 _("Press any key:"), FALSE
);
3433 edit_execute_key_command (edit
, -1, ascii_alpha_to_cntrl (char_for_insertion
));
3436 /* --------------------------------------------------------------------------------------------- */
3439 edit_begin_end_macro_cmd (WEdit
* edit
)
3441 /* edit is a pointer to the widget */
3444 unsigned long command
= macro_index
< 0 ? CK_MacroStartRecord
: CK_MacroStopRecord
;
3445 edit_execute_key_command (edit
, command
, -1);
3449 /* --------------------------------------------------------------------------------------------- */
3452 edit_begin_end_repeat_cmd (WEdit
* edit
)
3454 /* edit is a pointer to the widget */
3457 unsigned long command
= macro_index
< 0 ? CK_RepeatStartRecord
: CK_RepeatStopRecord
;
3458 edit_execute_key_command (edit
, command
, -1);
3462 /* --------------------------------------------------------------------------------------------- */
3465 edit_load_forward_cmd (WEdit
* edit
)
3468 && edit_query_dialog2 (_("Warning"),
3469 _("Current text was modified without a file save.\n"
3470 "Continue discards these changes"), _("C&ontinue"),
3473 edit
->force
|= REDRAW_COMPLETELY
;
3477 if (edit_stack_iterator
+ 1 >= MAX_HISTORY_MOVETO
)
3480 if (edit_history_moveto
[edit_stack_iterator
+ 1].line
< 1)
3483 edit_stack_iterator
++;
3484 if (edit_history_moveto
[edit_stack_iterator
].filename_vpath
!= NULL
)
3485 return edit_reload_line (edit
, edit_history_moveto
[edit_stack_iterator
].filename_vpath
,
3486 edit_history_moveto
[edit_stack_iterator
].line
);
3491 /* --------------------------------------------------------------------------------------------- */
3494 edit_load_back_cmd (WEdit
* edit
)
3497 && edit_query_dialog2 (_("Warning"),
3498 _("Current text was modified without a file save.\n"
3499 "Continue discards these changes"), _("C&ontinue"),
3502 edit
->force
|= REDRAW_COMPLETELY
;
3506 /* we are in the bottom of the stack, NO WAY! */
3507 if (edit_stack_iterator
== 0)
3510 edit_stack_iterator
--;
3511 if (edit_history_moveto
[edit_stack_iterator
].filename_vpath
!= NULL
)
3512 return edit_reload_line (edit
, edit_history_moveto
[edit_stack_iterator
].filename_vpath
,
3513 edit_history_moveto
[edit_stack_iterator
].line
);
3518 /* --------------------------------------------------------------------------------------------- */
3521 edit_get_match_keyword_cmd (WEdit
* edit
)
3523 gsize word_len
= 0, max_len
= 0;
3526 off_t word_start
= 0;
3527 unsigned char *bufpos
;
3531 char *tagfile
= NULL
;
3533 etags_hash_t def_hash
[MAX_DEFINITIONS
];
3535 for (i
= 0; i
< MAX_DEFINITIONS
; i
++)
3537 def_hash
[i
].filename
= NULL
;
3540 /* search start of word to be completed */
3541 if (!edit_find_word_start (edit
, &word_start
, &word_len
))
3544 /* prepare match expression */
3545 bufpos
= &edit
->buffers1
[word_start
>> S_EDIT_BUF_SIZE
][word_start
& M_EDIT_BUF_SIZE
];
3546 match_expr
= g_strdup_printf ("%.*s", (int) word_len
, bufpos
);
3548 ptr
= g_get_current_dir ();
3549 path
= g_strconcat (ptr
, G_DIR_SEPARATOR_S
, (char *) NULL
);
3552 /* Recursive search file 'TAGS' in parent dirs */
3555 ptr
= g_path_get_dirname (path
);
3559 tagfile
= mc_build_filename (path
, TAGS_NAME
, (char *) NULL
);
3560 if (exist_file (tagfile
))
3563 while (strcmp (path
, G_DIR_SEPARATOR_S
) != 0);
3568 etags_set_definition_hash (tagfile
, path
, match_expr
, (etags_hash_t
*) & def_hash
);
3573 max_len
= MAX_WIDTH_DEF_DIALOG
;
3577 editcmd_dialog_select_definition_show (edit
, match_expr
, max_len
, word_len
,
3578 (etags_hash_t
*) & def_hash
, num_def
);
3580 g_free (match_expr
);
3583 /* --------------------------------------------------------------------------------------------- */
3587 edit_suggest_current_word (WEdit
* edit
)
3591 off_t word_start
= 0;
3592 int retval
= B_SKIP_WORD
;
3595 /* search start of word to spell check */
3596 match_word
= edit_get_word_from_pos (edit
, edit
->curs1
, &word_start
, &word_len
, &cut_len
);
3599 if (mc_global
.source_codepage
>= 0 && (mc_global
.source_codepage
!= mc_global
.display_codepage
))
3603 tmp_word
= str_convert_to_display (match_word
);
3604 g_free (match_word
);
3605 match_word
= g_string_free (tmp_word
, FALSE
);
3608 if (!aspell_check (match_word
, (int) word_len
))
3613 suggest
= g_array_new (TRUE
, FALSE
, sizeof (char *));
3615 res
= aspell_suggest (suggest
, match_word
, (int) word_len
);
3618 char *new_word
= NULL
;
3620 edit
->found_start
= word_start
;
3621 edit
->found_len
= word_len
;
3622 edit
->force
|= REDRAW_PAGE
;
3623 edit_scroll_screen_over_cursor (edit
);
3624 edit_render_keypress (edit
);
3626 retval
= spell_dialog_spell_suggest_show (edit
, match_word
, &new_word
, suggest
);
3627 edit_cursor_move (edit
, word_len
- cut_len
);
3629 if (retval
== B_ENTER
&& new_word
!= NULL
)
3635 if (mc_global
.source_codepage
>= 0 &&
3636 (mc_global
.source_codepage
!= mc_global
.display_codepage
))
3640 tmp_word
= str_convert_to_input (new_word
);
3642 new_word
= g_string_free (tmp_word
, FALSE
);
3646 for (i
= 0; i
< word_len
; i
++)
3647 edit_backspace (edit
, 1);
3648 for (; *new_word
; new_word
++)
3649 edit_insert (edit
, *new_word
);
3652 else if (retval
== B_ADD_WORD
&& match_word
!= NULL
)
3653 aspell_add_to_dict (match_word
, (int) word_len
);
3656 g_array_free (suggest
, TRUE
);
3657 edit
->found_start
= 0;
3658 edit
->found_len
= 0;
3660 g_free (match_word
);
3664 /* --------------------------------------------------------------------------------------------- */
3667 edit_spellcheck_file (WEdit
* edit
)
3669 if (edit
->curs_line
> 0)
3671 edit_cursor_move (edit
, -edit
->curs1
);
3672 edit_move_to_prev_col (edit
, 0);
3673 edit_update_curs_row (edit
);
3680 c2
= edit_get_byte (edit
, edit
->curs1
);
3684 if (edit
->curs1
>= edit
->last_byte
)
3688 edit_cursor_move (edit
, 1);
3689 c2
= edit_get_byte (edit
, edit
->curs1
);
3691 while (is_break_char (c1
) || is_break_char (c2
));
3693 while (edit_suggest_current_word (edit
) != B_CANCEL
);
3696 /* --------------------------------------------------------------------------------------------- */
3699 edit_set_spell_lang (void)
3703 lang_list
= g_array_new (TRUE
, FALSE
, sizeof (char *));
3704 if (aspell_get_lang_list (lang_list
) != 0)
3708 lang
= spell_dialog_lang_list_show (lang_list
);
3710 (void) aspell_set_lang (lang
);
3712 aspell_array_clean (lang_list
);
3714 #endif /* HAVE_ASPELL */
3716 /* --------------------------------------------------------------------------------------------- */