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 */
72 #include "src/selcodepage.h"
74 #include "src/keybind-defaults.h"
75 #include "src/util.h" /* check_for_default() */
76 #include "src/filemanager/layout.h" /* mc_refresh() */
78 #include "edit-impl.h"
79 #include "editwidget.h"
80 #include "editcmd_dialogs.h"
83 #include "spell_dialogs.h"
87 /*** global variables ****************************************************************************/
89 /* search and replace: */
90 int search_create_bookmark
= FALSE
;
92 /* queries on a save */
93 int edit_confirm_save
= 1;
95 /*** file scope macro definitions ****************************************************************/
99 #define TEMP_BUF_LEN 1024
101 #define INPUT_INDEX 9
103 /* thanks to Liviu Daia <daia@stoilow.imar.ro> for getting this
104 (and the above) routines to work properly - paul */
106 #define is_digit(x) ((x) >= '0' && (x) <= '9')
108 #define MAIL_DLG_HEIGHT 12
110 #define MAX_WORD_COMPLETIONS 100 /* in listbox */
112 /*** file scope type declarations ****************************************************************/
114 /*** file scope variables ************************************************************************/
116 /*** file scope functions ************************************************************************/
117 /* --------------------------------------------------------------------------------------------- */
119 /* If 0 (quick save) then a) create/truncate <filename> file,
120 b) save to <filename>;
121 if 1 (safe save) then a) save to <tempnam>,
122 b) rename <tempnam> to <filename>;
123 if 2 (do backups) then a) save to <tempnam>,
124 b) rename <filename> to <filename.backup_ext>,
125 c) rename <tempnam> to <filename>. */
127 /* returns 0 on error, -1 on abort */
130 edit_save_file (WEdit
* edit
, const vfs_path_t
* filename_vpath
)
135 int this_save_mode
, fd
= -1;
136 vfs_path_t
*real_filename_vpath
;
137 vfs_path_t
*savename_vpath
= NULL
;
138 const char *start_filename
;
139 const vfs_path_element_t
*vpath_element
;
141 vpath_element
= vfs_path_get_by_index (filename_vpath
, 0);
142 if (vpath_element
== NULL
)
145 start_filename
= vpath_element
->path
;
146 if (*start_filename
== '\0')
149 if (*start_filename
!= PATH_SEP
&& edit
->dir_vpath
!= NULL
)
151 real_filename_vpath
= vfs_path_append_vpath_new (edit
->dir_vpath
, filename_vpath
, NULL
);
155 real_filename_vpath
= vfs_path_clone (filename_vpath
);
158 this_save_mode
= option_save_mode
;
159 if (this_save_mode
!= EDIT_QUICK_SAVE
)
161 if (!vfs_file_is_local (real_filename_vpath
)
162 || (fd
= mc_open (real_filename_vpath
, O_RDONLY
| O_BINARY
)) == -1)
165 * The file does not exists yet, so no safe save or
166 * backup are necessary.
168 this_save_mode
= EDIT_QUICK_SAVE
;
174 if (this_save_mode
== EDIT_QUICK_SAVE
&& !edit
->skip_detach_prompt
)
179 rv
= mc_stat (real_filename_vpath
, &sb
);
180 if (rv
== 0 && sb
.st_nlink
> 1)
182 rv
= edit_query_dialog3 (_("Warning"),
183 _("File has hard-links. Detach before saving?"),
184 _("&Yes"), _("&No"), _("&Cancel"));
188 this_save_mode
= EDIT_SAFE_SAVE
;
191 edit
->skip_detach_prompt
= 1;
194 vfs_path_free (real_filename_vpath
);
199 /* Prevent overwriting changes from other editor sessions. */
200 if (rv
== 0 && edit
->stat1
.st_mtime
!= 0 && edit
->stat1
.st_mtime
!= sb
.st_mtime
)
202 /* The default action is "Cancel". */
205 rv
= edit_query_dialog2 (_("Warning"),
206 _("The file has been modified in the meantime. Save anyway?"),
207 _("&Yes"), _("&Cancel"));
210 vfs_path_free (real_filename_vpath
);
216 if (this_save_mode
!= EDIT_QUICK_SAVE
)
218 char *savedir
, *saveprefix
;
220 savedir
= vfs_path_tokens_get (real_filename_vpath
, 0, -1);
222 savedir
= g_strdup (".");
224 /* Token-related function never return leading slash, so we need add it manually */
225 saveprefix
= mc_build_filename ("/", savedir
, "cooledit", NULL
);
227 fd
= mc_mkstemps (&savename_vpath
, saveprefix
, NULL
);
229 if (savename_vpath
== NULL
)
231 vfs_path_free (real_filename_vpath
);
235 * Close for now because mc_mkstemps use pure open system call
236 * to create temporary file and it needs to be reopened by
237 * VFS-aware mc_open().
242 savename_vpath
= vfs_path_clone (real_filename_vpath
);
244 (void) mc_chown (savename_vpath
, edit
->stat1
.st_uid
, edit
->stat1
.st_gid
);
245 (void) mc_chmod (savename_vpath
, edit
->stat1
.st_mode
);
247 fd
= mc_open (savename_vpath
, O_CREAT
| O_WRONLY
| O_TRUNC
| O_BINARY
, edit
->stat1
.st_mode
);
252 p
= edit_get_write_filter (savename_vpath
, real_filename_vpath
);
258 file
= (FILE *) popen (p
, "w");
262 filelen
= edit_write_stream (edit
, file
);
266 if (pclose (file
) != 0)
268 tmp
= g_strdup_printf (_("Error writing to pipe: %s"), p
);
269 edit_error_dialog (_("Error"), tmp
);
278 tmp
= g_strdup_printf (_("Cannot open pipe for writing: %s"), p
);
279 edit_error_dialog (_("Error"), get_sys_error (tmp
));
286 else if (edit
->lb
== LB_ASIS
)
287 { /* do not change line breaks */
290 filelen
= edit
->last_byte
;
291 while (buf
<= (edit
->curs1
>> S_EDIT_BUF_SIZE
) - 1)
293 if (mc_write (fd
, (char *) edit
->buffers1
[buf
], EDIT_BUF_SIZE
) != EDIT_BUF_SIZE
)
301 (fd
, (char *) edit
->buffers1
[buf
],
302 edit
->curs1
& M_EDIT_BUF_SIZE
) != (edit
->curs1
& M_EDIT_BUF_SIZE
))
306 else if (edit
->curs2
)
309 buf
= (edit
->curs2
>> S_EDIT_BUF_SIZE
);
312 (char *) edit
->buffers2
[buf
] + EDIT_BUF_SIZE
-
313 (edit
->curs2
& M_EDIT_BUF_SIZE
) - 1,
314 1 + (edit
->curs2
& M_EDIT_BUF_SIZE
)) != 1 + (edit
->curs2
& M_EDIT_BUF_SIZE
))
322 if (mc_write (fd
, (char *) edit
->buffers2
[buf
], EDIT_BUF_SIZE
) != EDIT_BUF_SIZE
)
334 /* Update the file information, especially the mtime. */
335 if (mc_stat (savename_vpath
, &edit
->stat1
) == -1)
339 { /* change line breaks */
341 const vfs_path_element_t
*path_element
;
345 path_element
= vfs_path_get_by_index (savename_vpath
, -1);
346 file
= (FILE *) fopen (path_element
->path
, "w");
349 filelen
= edit_write_stream (edit
, file
);
356 msg
= g_strdup_printf (_("Cannot open file for writing: %s"), path_element
->path
);
357 edit_error_dialog (_("Error"), msg
);
363 if (filelen
!= edit
->last_byte
)
366 if (this_save_mode
== EDIT_DO_BACKUP
)
368 char *tmp_store_filename
;
369 vfs_path_element_t
*last_vpath_element
;
370 vfs_path_t
*tmp_vpath
;
374 assert (option_backup_ext
!= NULL
);
376 /* add backup extention to the path */
377 tmp_vpath
= vfs_path_clone (real_filename_vpath
);
378 last_vpath_element
= (vfs_path_element_t
*) vfs_path_get_by_index (tmp_vpath
, -1);
379 tmp_store_filename
= last_vpath_element
->path
;
380 last_vpath_element
->path
= g_strdup_printf ("%s%s", tmp_store_filename
, option_backup_ext
);
381 g_free (tmp_store_filename
);
383 ok
= (mc_rename (real_filename_vpath
, tmp_vpath
) != -1);
384 vfs_path_free (tmp_vpath
);
389 if (this_save_mode
!= EDIT_QUICK_SAVE
)
390 if (mc_rename (savename_vpath
, real_filename_vpath
) == -1)
393 vfs_path_free (real_filename_vpath
);
394 vfs_path_free (savename_vpath
);
397 /* FIXME: Is this safe ?
398 * if (this_save_mode != EDIT_QUICK_SAVE)
399 * mc_unlink (savename);
401 vfs_path_free (real_filename_vpath
);
402 vfs_path_free (savename_vpath
);
406 /* --------------------------------------------------------------------------------------------- */
409 edit_check_newline (WEdit
* edit
)
411 return !(option_check_nl_at_eof
&& edit
->last_byte
> 0
412 && edit_get_byte (edit
, edit
->last_byte
- 1) != '\n'
413 && edit_query_dialog2 (_("Warning"),
414 _("The file you are saving is not finished with a newline"),
415 _("C&ontinue"), _("&Cancel")));
418 /* --------------------------------------------------------------------------------------------- */
421 edit_get_save_file_as (WEdit
* edit
)
424 #define DLG_HEIGHT 14
426 static LineBreaks cur_lb
= LB_ASIS
;
428 char *filename
= vfs_path_to_str (edit
->filename_vpath
);
431 const char *lb_names
[LB_NAMES
] = {
432 N_("&Do not change"),
433 N_("&Unix format (LF)"),
434 N_("&Windows/DOS format (CR LF)"),
435 N_("&Macintosh format (CR)")
438 QuickWidget quick_widgets
[] = {
439 QUICK_BUTTON (6, 10, DLG_HEIGHT
- 3, DLG_HEIGHT
, N_("&Cancel"), B_CANCEL
, NULL
),
440 QUICK_BUTTON (2, 10, DLG_HEIGHT
- 3, DLG_HEIGHT
, N_("&OK"), B_ENTER
, NULL
),
441 QUICK_RADIO (5, DLG_WIDTH
, DLG_HEIGHT
- 8, DLG_HEIGHT
, LB_NAMES
, lb_names
, (int *) &cur_lb
),
442 QUICK_LABEL (3, DLG_WIDTH
, DLG_HEIGHT
- 9, DLG_HEIGHT
, N_("Change line breaks to:")),
443 QUICK_INPUT (3, DLG_WIDTH
, DLG_HEIGHT
- 11, DLG_HEIGHT
, filename
, DLG_WIDTH
- 6, 0,
444 "save-as", &filename_res
),
445 QUICK_LABEL (3, DLG_WIDTH
, DLG_HEIGHT
- 12, DLG_HEIGHT
, N_("Enter file name:")),
449 QuickDialog Quick_options
= {
450 DLG_WIDTH
, DLG_HEIGHT
, -1, -1,
451 N_("Save As"), "[Save File As]",
452 quick_widgets
, NULL
, NULL
, FALSE
455 if (quick_dialog (&Quick_options
) != B_CANCEL
)
458 vfs_path_t
*ret_vpath
;
461 fname
= tilde_expand (filename_res
);
462 g_free (filename_res
);
463 ret_vpath
= vfs_path_from_str (fname
);
475 /* --------------------------------------------------------------------------------------------- */
477 /** returns TRUE on success */
480 edit_save_cmd (WEdit
* edit
)
482 int res
, save_lock
= 0;
484 if (!edit
->locked
&& !edit
->delete_file
)
485 save_lock
= lock_file (edit
->filename_vpath
);
486 res
= edit_save_file (edit
, edit
->filename_vpath
);
488 /* Maintain modify (not save) lock on failure */
489 if ((res
> 0 && edit
->locked
) || save_lock
)
490 edit
->locked
= unlock_file (edit
->filename_vpath
);
492 /* On failure try 'save as', it does locking on its own */
494 return edit_save_as_cmd (edit
);
495 edit
->force
|= REDRAW_COMPLETELY
;
498 edit
->delete_file
= 0;
505 /* --------------------------------------------------------------------------------------------- */
509 * @param h screen the owner of editor window
510 * @param vpath vfs file path
511 * @return TRUE if file content was successfully loaded, FALSE otherwise
514 static inline gboolean
515 edit_load_file_from_filename (Dlg_head
* h
, const vfs_path_t
* vpath
)
517 return edit_add_window (h
, h
->y
+ 1, h
->x
, h
->lines
- 2, h
->cols
, vpath
, 0);
520 /* --------------------------------------------------------------------------------------------- */
523 edit_delete_column_of_text (WEdit
* edit
)
530 eval_marks (edit
, &m1
, &m2
);
531 n
= edit_move_forward (edit
, m1
, 0, m2
) + 1;
532 c
= (long) edit_move_forward3 (edit
, edit_bol (edit
, m1
), 0, m1
);
533 d
= (long) edit_move_forward3 (edit
, edit_bol (edit
, m2
), 0, m2
);
534 b
= max (min (c
, d
), min (edit
->column1
, edit
->column2
));
535 c
= max (c
, max (edit
->column1
, edit
->column2
));
539 r
= edit_bol (edit
, edit
->curs1
);
540 p
= edit_move_forward3 (edit
, r
, b
, 0);
541 q
= edit_move_forward3 (edit
, r
, c
, 0);
546 edit_cursor_move (edit
, p
- edit
->curs1
);
549 /* delete line between margins */
550 if (edit_get_byte (edit
, edit
->curs1
) != '\n')
551 edit_delete (edit
, TRUE
);
555 /* move to next line except on the last delete */
556 edit_cursor_move (edit
, edit_move_forward (edit
, edit
->curs1
, 1, 0) - edit
->curs1
);
560 /* --------------------------------------------------------------------------------------------- */
561 /** if success return 0 */
564 edit_block_delete (WEdit
* edit
)
566 off_t start_mark
, end_mark
;
568 long curs_line
, c1
, c2
;
570 if (eval_marks (edit
, &start_mark
, &end_mark
))
572 if (edit
->column_highlight
&& edit
->mark2
< 0)
573 edit_mark_cmd (edit
, FALSE
);
574 if ((end_mark
- start_mark
) > option_max_undo
/ 2)
576 /* Warning message with a query to continue or cancel the operation */
577 if (edit_query_dialog2
580 ("Block is large, you may not be able to undo this action"),
581 _("C&ontinue"), _("&Cancel")))
586 c1
= min (edit
->column1
, edit
->column2
);
587 c2
= max (edit
->column1
, edit
->column2
);
591 edit_push_markers (edit
);
593 curs_line
= edit
->curs_line
;
595 curs_pos
= edit
->curs_col
+ edit
->over_col
;
597 /* move cursor to start of selection */
598 edit_cursor_move (edit
, start_mark
- edit
->curs1
);
599 edit_scroll_screen_over_cursor (edit
);
601 if (start_mark
< end_mark
)
603 if (edit
->column_highlight
)
608 edit_mark_cmd (edit
, FALSE
);
609 edit_delete_column_of_text (edit
);
610 /* move cursor to the saved position */
611 edit_move_to_line (edit
, curs_line
);
612 /* calculate line width and cursor position before cut */
613 line_width
= edit_move_forward3 (edit
, edit_bol (edit
, edit
->curs1
), 0,
614 edit_eol (edit
, edit
->curs1
));
615 if (option_cursor_beyond_eol
&& curs_pos
> line_width
)
616 edit
->over_col
= curs_pos
- line_width
;
622 for (count
= start_mark
; count
< end_mark
; count
++)
623 edit_delete (edit
, TRUE
);
626 edit_set_markers (edit
, 0, 0, 0, 0);
627 edit
->force
|= REDRAW_PAGE
;
631 /* --------------------------------------------------------------------------------------------- */
633 * Get EOL symbol for searching.
635 * @param edit editor object
640 edit_search_get_current_end_line_char (const WEdit
* edit
)
651 /* --------------------------------------------------------------------------------------------- */
653 * Checking if search condition have BOL(^) or EOL ($) regexp special characters.
655 * @param search search object
656 * @return result of checks.
659 static edit_search_line_t
660 edit_get_search_line_type (mc_search_t
* search
)
662 edit_search_line_t search_line_type
= 0;
664 if (search
->search_type
!= MC_SEARCH_T_REGEX
)
665 return search_line_type
;
667 if (*search
->original
== '^')
668 search_line_type
|= AT_START_LINE
;
670 if (search
->original
[search
->original_len
- 1] == '$')
671 search_line_type
|= AT_END_LINE
;
672 return search_line_type
;
675 /* --------------------------------------------------------------------------------------------- */
677 * Calculating the start position of next line.
679 * @param edit editor object
680 * @param current_pos current position
681 * @param max_pos max position
682 * @param end_string_symbol end of line symbol
683 * @return start position of next line
687 edit_calculate_start_of_next_line (WEdit
* edit
, off_t current_pos
, off_t max_pos
,
688 char end_string_symbol
)
692 for (i
= current_pos
; i
< max_pos
; i
++)
695 if (edit_get_byte (edit
, i
) == end_string_symbol
)
702 /* --------------------------------------------------------------------------------------------- */
704 * Calculating the end position of previous line.
706 * @param edit editor object
707 * @param current_pos current position
708 * @param end_string_symbol end of line symbol
709 * @return end position of previous line
713 edit_calculate_end_of_previous_line (WEdit
* edit
, off_t current_pos
, char end_string_symbol
)
717 for (i
= current_pos
- 1; i
>= 0; i
--)
718 if (edit_get_byte (edit
, i
) == end_string_symbol
)
724 /* --------------------------------------------------------------------------------------------- */
726 * Calculating the start position of previous line.
728 * @param edit editor object
729 * @param current_pos current position
730 * @param end_string_symbol end of line symbol
731 * @return start position of previous line
735 edit_calculate_start_of_previous_line (WEdit
* edit
, off_t current_pos
, char end_string_symbol
)
737 current_pos
= edit_calculate_end_of_previous_line (edit
, current_pos
, end_string_symbol
);
738 current_pos
= edit_calculate_end_of_previous_line (edit
, current_pos
, end_string_symbol
);
740 return (current_pos
+ 1);
743 /* --------------------------------------------------------------------------------------------- */
745 * Calculating the start position of current line.
747 * @param edit editor object
748 * @param current_pos current position
749 * @param end_string_symbol end of line symbol
750 * @return start position of current line
754 edit_calculate_start_of_current_line (WEdit
* edit
, off_t current_pos
, char end_string_symbol
)
756 current_pos
= edit_calculate_end_of_previous_line (edit
, current_pos
, end_string_symbol
);
758 return (current_pos
+ 1);
761 /* --------------------------------------------------------------------------------------------- */
763 * Fixing (if needed) search start position if 'only in selection' option present.
765 * @param edit editor object
769 edit_search_fix_search_start_if_selection (WEdit
* edit
)
771 off_t start_mark
= 0;
774 if (!edit_search_options
.only_in_selection
)
777 if (eval_marks (edit
, &start_mark
, &end_mark
) != 0)
780 if (edit_search_options
.backwards
)
782 if (edit
->search_start
> end_mark
|| edit
->search_start
<= start_mark
)
783 edit
->search_start
= end_mark
;
787 if (edit
->search_start
< start_mark
|| edit
->search_start
>= end_mark
)
788 edit
->search_start
= start_mark
;
792 /* --------------------------------------------------------------------------------------------- */
795 editcmd_find (WEdit
* edit
, gsize
* len
)
797 off_t search_start
= edit
->search_start
;
799 off_t start_mark
= 0;
800 off_t end_mark
= edit
->last_byte
;
802 char end_string_symbol
;
804 end_string_symbol
= edit_search_get_current_end_line_char (edit
);
806 /* prepare for search */
807 if (edit_search_options
.only_in_selection
)
809 mark_res
= eval_marks (edit
, &start_mark
, &end_mark
);
812 edit
->search
->error
= MC_SEARCH_E_NOTFOUND
;
813 edit
->search
->error_str
= g_strdup (_("Search string not found"));
817 /* fix the start and the end of search block positions */
818 if ((edit
->search_line_type
& AT_START_LINE
) != 0
819 && (start_mark
!= 0 || edit_get_byte (edit
, start_mark
- 1) != end_string_symbol
))
822 edit_calculate_start_of_next_line (edit
, start_mark
, edit
->last_byte
,
825 if ((edit
->search_line_type
& AT_END_LINE
) != 0
826 && (end_mark
- 1 != edit
->last_byte
827 || edit_get_byte (edit
, end_mark
) != end_string_symbol
))
829 end_mark
= edit_calculate_end_of_previous_line (edit
, end_mark
, end_string_symbol
);
831 if (start_mark
>= end_mark
)
833 edit
->search
->error
= MC_SEARCH_E_NOTFOUND
;
834 edit
->search
->error_str
= g_strdup (_("Search string not found"));
840 if (edit_search_options
.backwards
)
841 end_mark
= max (1, edit
->curs1
) - 1;
845 if (edit_search_options
.backwards
)
847 /* backward search */
848 search_end
= end_mark
;
850 if ((edit
->search_line_type
& AT_START_LINE
) != 0)
852 edit_calculate_start_of_current_line (edit
, search_start
, end_string_symbol
);
854 while ((int) search_start
>= start_mark
)
856 if (search_end
> (off_t
) (search_start
+ edit
->search
->original_len
)
857 && mc_search_is_fixed_search_str (edit
->search
))
859 search_end
= search_start
+ edit
->search
->original_len
;
861 if (mc_search_run (edit
->search
, (void *) edit
, search_start
, search_end
, len
)
862 && edit
->search
->normal_offset
== search_start
)
868 if ((edit
->search_line_type
& AT_START_LINE
) != 0)
870 edit_calculate_start_of_previous_line (edit
, search_start
, end_string_symbol
);
874 edit
->search
->error_str
= g_strdup (_("Search string not found"));
879 if ((edit
->search_line_type
& AT_START_LINE
) != 0 && search_start
!= start_mark
)
881 edit_calculate_start_of_next_line (edit
, search_start
, end_mark
, end_string_symbol
);
882 return mc_search_run (edit
->search
, (void *) edit
, search_start
, end_mark
, len
);
887 /* --------------------------------------------------------------------------------------------- */
890 edit_replace_cmd__conv_to_display (char *str
)
895 tmp
= str_convert_to_display (str
);
899 return g_string_free (tmp
, FALSE
);
900 g_string_free (tmp
, TRUE
);
903 return g_strdup (str
);
906 /* --------------------------------------------------------------------------------------------- */
909 edit_replace_cmd__conv_to_input (char *str
)
914 tmp
= str_convert_to_input (str
);
918 return g_string_free (tmp
, FALSE
);
919 g_string_free (tmp
, TRUE
);
922 return g_strdup (str
);
925 /* --------------------------------------------------------------------------------------------- */
928 edit_do_search (WEdit
* edit
)
932 if (edit
->search
== NULL
)
933 edit
->search_start
= edit
->curs1
;
935 edit_push_undo_action (edit
, KEY_PRESS
+ edit
->start_display
);
937 if (search_create_bookmark
)
939 int found
= 0, books
= 0;
940 long l
= 0, l_last
= -1;
943 search_create_bookmark
= FALSE
;
944 book_mark_flush (edit
, -1);
946 while (mc_search_run (edit
->search
, (void *) edit
, q
, edit
->last_byte
, &len
))
949 edit
->search_start
= edit
->search
->normal_offset
;
951 l
+= edit_count_lines (edit
, q
, edit
->search
->normal_offset
);
954 book_mark_insert (edit
, l
, BOOK_MARK_FOUND_COLOR
);
958 q
= edit
->search
->normal_offset
+ 1;
962 edit_error_dialog (_("Search"), _("Search string not found"));
964 edit_cursor_move (edit
, edit
->search_start
- edit
->curs1
);
968 if (edit
->found_len
!= 0 && edit
->search_start
== edit
->found_start
+ 1
969 && edit_search_options
.backwards
)
970 edit
->search_start
--;
972 if (edit
->found_len
!= 0 && edit
->search_start
== edit
->found_start
- 1
973 && !edit_search_options
.backwards
)
974 edit
->search_start
++;
976 if (editcmd_find (edit
, &len
))
978 edit
->found_start
= edit
->search_start
= edit
->search
->normal_offset
;
979 edit
->found_len
= len
;
981 edit_cursor_move (edit
, edit
->search_start
- edit
->curs1
);
982 edit_scroll_screen_over_cursor (edit
);
983 if (edit_search_options
.backwards
)
984 edit
->search_start
--;
986 edit
->search_start
++;
990 edit
->search_start
= edit
->curs1
;
991 if (edit
->search
->error_str
!= NULL
)
992 edit_error_dialog (_("Search"), edit
->search
->error_str
);
996 edit
->force
|= REDRAW_COMPLETELY
;
997 edit_scroll_screen_over_cursor (edit
);
1000 /* --------------------------------------------------------------------------------------------- */
1003 edit_search (WEdit
* edit
)
1005 if (editcmd_dialog_search_show (edit
))
1007 edit
->search_line_type
= edit_get_search_line_type (edit
->search
);
1008 edit_search_fix_search_start_if_selection (edit
);
1009 edit_do_search (edit
);
1013 /* --------------------------------------------------------------------------------------------- */
1014 /** Return a null terminated length of text. Result must be g_free'd */
1016 static unsigned char *
1017 edit_get_block (WEdit
* edit
, off_t start
, off_t finish
, off_t
* l
)
1019 unsigned char *s
, *r
;
1021 r
= s
= g_malloc0 (finish
- start
+ 1);
1022 if (edit
->column_highlight
)
1025 /* copy from buffer, excluding chars that are out of the column 'margins' */
1026 while (start
< finish
)
1031 x
= edit_move_forward3 (edit
, edit_bol (edit
, start
), 0, start
);
1032 c
= edit_get_byte (edit
, start
);
1033 if ((x
>= edit
->column1
&& x
< edit
->column2
)
1034 || (x
>= edit
->column2
&& x
< edit
->column1
) || c
== '\n')
1044 *l
= finish
- start
;
1045 while (start
< finish
)
1046 *s
++ = edit_get_byte (edit
, start
++);
1052 /* --------------------------------------------------------------------------------------------- */
1053 /** copies a block to clipboard file */
1056 edit_save_block_to_clip_file (WEdit
* edit
, off_t start
, off_t finish
)
1061 tmp
= mc_config_get_full_path (EDIT_CLIP_FILE
);
1062 ret
= edit_save_block (edit
, tmp
, start
, finish
);
1067 /* --------------------------------------------------------------------------------------------- */
1070 pipe_mail (WEdit
* edit
, char *to
, char *subject
, char *cc
)
1075 to
= name_quote (to
, 0);
1076 subject
= name_quote (subject
, 0);
1077 cc
= name_quote (cc
, 0);
1078 s
= g_strconcat ("mail -s ", subject
, *cc
? " -c " : "", cc
, " ", to
, (char *) NULL
);
1092 for (i
= 0; i
< edit
->last_byte
; i
++)
1093 fputc (edit_get_byte (edit
, i
), p
);
1098 /* --------------------------------------------------------------------------------------------- */
1099 /** find first character of current word */
1102 edit_find_word_start (WEdit
* edit
, off_t
* word_start
, gsize
* word_len
)
1107 /* return if at begin of file */
1108 if (edit
->curs1
<= 0)
1111 c
= edit_get_byte (edit
, edit
->curs1
- 1);
1112 /* return if not at end or in word */
1113 if (is_break_char (c
))
1116 /* search start of word to be completed */
1119 /* return if at begin of file */
1120 if ((gsize
) edit
->curs1
< i
)
1124 c
= edit_get_byte (edit
, edit
->curs1
- i
);
1126 if (is_break_char (c
))
1128 /* return if word starts with digit */
1132 *word_start
= edit
->curs1
- (i
- 1); /* start found */
1141 /* --------------------------------------------------------------------------------------------- */
1143 * Get current word under cursor
1145 * @param edit editor object
1146 * @param srch mc_search object
1147 * @param word_start start word position
1149 * @return newly allocated string or NULL if no any words under cursor
1153 edit_collect_completions_get_current_word (WEdit
* edit
, mc_search_t
* srch
, off_t word_start
)
1159 if (!mc_search_run (srch
, (void *) edit
, word_start
, edit
->last_byte
, &len
))
1162 temp
= g_string_sized_new (len
);
1164 for (i
= 0; i
< (off_t
) len
; i
++)
1168 chr
= edit_get_byte (edit
, word_start
+ i
);
1170 g_string_append_c (temp
, chr
);
1173 return g_string_free (temp
, temp
->len
== 0);
1176 /* --------------------------------------------------------------------------------------------- */
1177 /** collect the possible completions */
1180 edit_collect_completions (WEdit
* edit
, off_t word_start
, gsize word_len
,
1181 char *match_expr
, struct selection
*compl, gsize
* num
)
1189 off_t last_byte
, start
= -1;
1192 srch
= mc_search_new (match_expr
, -1);
1196 if (mc_config_get_bool
1197 (mc_main_config
, CONFIG_APP_SECTION
, "editor_wordcompletion_collect_entire_file", 0))
1199 last_byte
= edit
->last_byte
;
1203 last_byte
= word_start
;
1206 srch
->search_type
= MC_SEARCH_T_REGEX
;
1207 srch
->is_case_sensitive
= TRUE
;
1208 srch
->search_fn
= edit_search_cmd_callback
;
1210 current_word
= edit_collect_completions_get_current_word (edit
, srch
, word_start
);
1212 temp
= g_string_new ("");
1214 /* collect max MAX_WORD_COMPLETIONS completions */
1215 while (mc_search_run (srch
, (void *) edit
, start
+ 1, last_byte
, &len
))
1217 g_string_set_size (temp
, 0);
1218 start
= srch
->normal_offset
;
1220 /* add matched completion if not yet added */
1221 for (i
= 0; i
< len
; i
++)
1223 skip
= edit_get_byte (edit
, start
+ i
);
1227 /* skip current word */
1228 if (start
+ (off_t
) i
== word_start
)
1231 g_string_append_c (temp
, skip
);
1237 if (current_word
!= NULL
&& strcmp (current_word
, temp
->str
) == 0)
1242 for (i
= 0; i
< *num
; i
++)
1245 ((char *) &compl[i
].text
[word_len
],
1246 (char *) &temp
->str
[word_len
], max (len
, compl[i
].len
) - word_len
) == 0)
1248 struct selection
this = compl[i
];
1249 for (++i
; i
< *num
; i
++)
1251 compl[i
- 1] = compl[i
];
1253 compl[*num
- 1] = this;
1255 break; /* skip it, already added */
1261 if (*num
== MAX_WORD_COMPLETIONS
)
1263 g_free (compl[0].text
);
1264 for (i
= 1; i
< *num
; i
++)
1266 compl[i
- 1] = compl[i
];
1273 recoded
= str_convert_to_display (temp
->str
);
1275 if (recoded
&& recoded
->len
)
1276 g_string_assign (temp
, recoded
->str
);
1278 g_string_free (recoded
, TRUE
);
1281 compl[*num
].text
= g_strndup (temp
->str
, temp
->len
);
1282 compl[*num
].len
= temp
->len
;
1286 /* note the maximal length needed for the completion dialog */
1291 mc_search_free (srch
);
1292 g_string_free (temp
, TRUE
);
1293 g_free (current_word
);
1298 /* --------------------------------------------------------------------------------------------- */
1301 edit_insert_column_of_text (WEdit
* edit
, unsigned char *data
, off_t size
, long width
,
1302 off_t
* start_pos
, off_t
* end_pos
, long *col1
, long *col2
)
1307 cursor
= edit
->curs1
;
1308 col
= edit_get_col (edit
);
1310 for (i
= 0; i
< size
; i
++)
1312 if (data
[i
] != '\n')
1313 edit_insert (edit
, data
[i
]);
1315 { /* fill in and move to next line */
1319 if (edit_get_byte (edit
, edit
->curs1
) != '\n')
1321 for (l
= width
- (edit_get_col (edit
) - col
); l
> 0; l
-= space_width
)
1322 edit_insert (edit
, ' ');
1324 for (p
= edit
->curs1
;; p
++)
1326 if (p
== edit
->last_byte
)
1328 edit_cursor_move (edit
, edit
->last_byte
- edit
->curs1
);
1329 edit_insert_ahead (edit
, '\n');
1333 if (edit_get_byte (edit
, p
) == '\n')
1339 edit_cursor_move (edit
, edit_move_forward3 (edit
, p
, col
, 0) - edit
->curs1
);
1341 for (l
= col
- edit_get_col (edit
); l
>= space_width
; l
-= space_width
)
1342 edit_insert (edit
, ' ');
1347 *col2
= col
+ width
;
1348 *start_pos
= cursor
;
1349 *end_pos
= edit
->curs1
;
1350 edit_cursor_move (edit
, cursor
- edit
->curs1
);
1353 /* --------------------------------------------------------------------------------------------- */
1356 edit_macro_comparator (gconstpointer
* macro1
, gconstpointer
* macro2
)
1358 const macros_t
*m1
= (const macros_t
*) macro1
;
1359 const macros_t
*m2
= (const macros_t
*) macro2
;
1361 return m1
->hotkey
- m2
->hotkey
;
1364 /* --------------------------------------------------------------------------------------------- */
1367 edit_macro_sort_by_hotkey (void)
1369 if (macros_list
!= NULL
&& macros_list
->len
!= 0)
1370 g_array_sort (macros_list
, (GCompareFunc
) edit_macro_comparator
);
1373 /* --------------------------------------------------------------------------------------------- */
1376 edit_get_macro (WEdit
* edit
, int hotkey
, const macros_t
** macros
, guint
* indx
)
1378 const macros_t
*array_start
= &g_array_index (macros_list
, struct macros_t
, 0);
1380 macros_t search_macro
;
1384 search_macro
.hotkey
= hotkey
;
1385 result
= bsearch (&search_macro
, macros_list
->data
, macros_list
->len
,
1386 sizeof (macros_t
), (GCompareFunc
) edit_macro_comparator
);
1388 if (result
!= NULL
&& result
->macro
!= NULL
)
1390 *indx
= (result
- array_start
);
1398 /* --------------------------------------------------------------------------------------------- */
1399 /** returns FALSE on error */
1402 edit_delete_macro (WEdit
* edit
, int hotkey
)
1404 mc_config_t
*macros_config
= NULL
;
1405 const char *section_name
= "editor";
1406 gchar
*macros_fname
;
1409 const macros_t
*macros
= NULL
;
1411 /* clear array of actions for current hotkey */
1412 while (edit_get_macro (edit
, hotkey
, ¯os
, &indx
))
1414 if (macros
->macro
!= NULL
)
1415 g_array_free (macros
->macro
, TRUE
);
1417 g_array_remove_index (macros_list
, indx
);
1418 edit_macro_sort_by_hotkey ();
1421 macros_fname
= mc_config_get_full_path (MC_MACRO_FILE
);
1422 macros_config
= mc_config_init (macros_fname
, FALSE
);
1423 g_free (macros_fname
);
1425 if (macros_config
== NULL
)
1428 skeyname
= lookup_key_by_code (hotkey
);
1429 while (mc_config_del_key (macros_config
, section_name
, skeyname
))
1432 mc_config_save_file (macros_config
, NULL
);
1433 mc_config_deinit (macros_config
);
1437 /* --------------------------------------------------------------------------------------------- */
1439 * Callback for the iteration of objects in the 'editors' array.
1440 * Toggle syntax highlighting in editor object.
1442 * @param data probably WEdit object
1443 * @param user_data unused
1447 edit_syntax_onoff_cb (void *data
, void *user_data
)
1451 if (edit_widget_is_editor ((const Widget
*) data
))
1453 WEdit
*edit
= (WEdit
*) data
;
1455 if (option_syntax_highlighting
)
1456 edit_load_syntax (edit
, NULL
, edit
->syntax_type
);
1457 edit
->force
|= REDRAW_PAGE
;
1461 /* --------------------------------------------------------------------------------------------- */
1463 * Callback for the iteration of objects in the 'editors' array.
1464 * Redraw editor object.
1466 * @param data probably WEdit object
1467 * @param user_data unused
1471 edit_redraw_page_cb (void *data
, void *user_data
)
1475 if (edit_widget_is_editor ((const Widget
*) data
))
1476 ((WEdit
*) data
)->force
|= REDRAW_PAGE
;
1479 /* --------------------------------------------------------------------------------------------- */
1480 /*** public functions ****************************************************************************/
1481 /* --------------------------------------------------------------------------------------------- */
1484 edit_refresh_cmd (void)
1491 /* --------------------------------------------------------------------------------------------- */
1493 * Toggle syntax highlighting in all editor windows.
1495 * @param h root widget for all windows
1499 edit_syntax_onoff_cmd (Dlg_head
* h
)
1501 option_syntax_highlighting
= !option_syntax_highlighting
;
1502 g_list_foreach (h
->widgets
, edit_syntax_onoff_cb
, NULL
);
1506 /* --------------------------------------------------------------------------------------------- */
1508 * Toggle tabs showing in all editor windows.
1510 * @param h root widget for all windows
1514 edit_show_tabs_tws_cmd (Dlg_head
* h
)
1516 enable_show_tabs_tws
= !enable_show_tabs_tws
;
1517 g_list_foreach (h
->widgets
, edit_redraw_page_cb
, NULL
);
1521 /* --------------------------------------------------------------------------------------------- */
1523 * Toggle right margin showing in all editor windows.
1525 * @param h root widget for all windows
1529 edit_show_margin_cmd (Dlg_head
* h
)
1531 show_right_margin
= !show_right_margin
;
1532 g_list_foreach (h
->widgets
, edit_redraw_page_cb
, NULL
);
1536 /* --------------------------------------------------------------------------------------------- */
1538 * Toggle line numbers showing in all editor windows.
1540 * @param h root widget for all windows
1544 edit_show_numbers_cmd (Dlg_head
* h
)
1546 option_line_state
= !option_line_state
;
1547 option_line_state_width
= option_line_state
? LINE_STATE_WIDTH
: 0;
1548 g_list_foreach (h
->widgets
, edit_redraw_page_cb
, NULL
);
1552 /* --------------------------------------------------------------------------------------------- */
1555 edit_save_mode_cmd (void)
1558 const int DLG_X
= 38;
1559 const int DLG_Y
= 13;
1563 const char *str
[] = {
1566 N_("&Do backups with following extension:")
1569 QuickWidget widgets
[] = {
1571 QUICK_BUTTON (18, DLG_X
, DLG_Y
- 3, DLG_Y
, N_("&Cancel"), B_CANCEL
, NULL
),
1573 QUICK_BUTTON (6, DLG_X
, DLG_Y
- 3, DLG_Y
, N_("&OK"), B_ENTER
, NULL
),
1575 QUICK_CHECKBOX (4, DLG_X
, 8, DLG_Y
, N_("Check &POSIX new line"), &option_check_nl_at_eof
),
1577 QUICK_INPUT (8, DLG_X
, 6, DLG_Y
, option_backup_ext
, 9, 0, "edit-backup-ext", &str_result
),
1579 QUICK_RADIO (4, DLG_X
, 3, DLG_Y
, 3, str
, &option_save_mode
),
1583 QuickDialog dialog
= {
1584 DLG_X
, DLG_Y
, -1, -1, N_("Edit Save Mode"),
1585 "[Edit Save Mode]", widgets
, NULL
, NULL
, FALSE
1590 size_t w0
, w1
, b_len
, w3
;
1592 #ifdef HAVE_ASSERT_H
1593 assert (option_backup_ext
!= NULL
);
1596 /* OK/Cancel buttons */
1597 w0
= str_term_width1 (_(widgets
[0].u
.button
.text
)) + 3;
1598 w1
= str_term_width1 (_(widgets
[1].u
.button
.text
)) + 5; /* default button */
1599 b_len
= w0
+ w1
+ 3;
1601 maxlen
= max (b_len
, (size_t) str_term_width1 (_(dialog
.title
)) + 2);
1604 for (i
= 0; i
< 3; i
++)
1609 w3
= max (w3
, (size_t) str_term_width1 (str
[i
]));
1612 maxlen
= max (maxlen
, w3
+ 4);
1614 dialog
.xlen
= min ((size_t) COLS
, maxlen
+ 8);
1616 widgets
[3].u
.input
.len
= w3
;
1617 widgets
[1].relative_x
= (dialog
.xlen
- b_len
) / 2;
1618 widgets
[0].relative_x
= widgets
[1].relative_x
+ w0
+ 2;
1620 for (i
= 0; i
< sizeof (widgets
) / sizeof (widgets
[0]); i
++)
1621 widgets
[i
].x_divisions
= dialog
.xlen
;
1623 if (quick_dialog (&dialog
) != B_CANCEL
)
1625 g_free (option_backup_ext
);
1626 option_backup_ext
= str_result
;
1630 /* --------------------------------------------------------------------------------------------- */
1633 edit_set_filename (WEdit
* edit
, const vfs_path_t
* name_vpath
)
1635 vfs_path_free (edit
->filename_vpath
);
1636 edit
->filename_vpath
= vfs_path_clone (name_vpath
);
1638 if (edit
->dir_vpath
== NULL
)
1639 edit
->dir_vpath
= vfs_path_clone (vfs_get_raw_current_dir ());
1642 /* --------------------------------------------------------------------------------------------- */
1643 /* Here we want to warn the users of overwriting an existing file,
1644 but only if they have made a change to the filename */
1645 /* returns TRUE on success */
1647 edit_save_as_cmd (WEdit
* edit
)
1649 /* This heads the 'Save As' dialog box */
1650 vfs_path_t
*exp_vpath
;
1652 int different_filename
= 0;
1654 if (!edit_check_newline (edit
))
1657 exp_vpath
= edit_get_save_file_as (edit
);
1658 edit_push_undo_action (edit
, KEY_PRESS
+ edit
->start_display
);
1660 if (exp_vpath
!= NULL
)
1662 if (vfs_path_len (exp_vpath
) == 0)
1668 if (vfs_path_cmp (edit
->filename_vpath
, exp_vpath
) != 0)
1673 if (mc_stat (exp_vpath
, &sb
) == 0 && !S_ISREG (sb
.st_mode
))
1675 edit_error_dialog (_("Save as"),
1677 ("Cannot save: destination is not a regular file")));
1681 different_filename
= 1;
1682 file
= mc_open (exp_vpath
, O_RDONLY
| O_BINARY
);
1686 /* the file exists */
1688 /* Overwrite the current file or cancel the operation */
1689 if (edit_query_dialog2
1691 _("A file already exists with this name"), _("&Overwrite"), _("&Cancel")))
1696 edit
->stat1
.st_mode
|= S_IWUSR
;
1698 save_lock
= lock_file (exp_vpath
);
1702 /* filenames equal, check if already locked */
1703 if (!edit
->locked
&& !edit
->delete_file
)
1704 save_lock
= lock_file (exp_vpath
);
1707 if (different_filename
)
1710 * Allow user to write into saved (under another name) file
1711 * even if original file had r/o user permissions.
1713 edit
->stat1
.st_mode
|= S_IWRITE
;
1716 rv
= edit_save_file (edit
, exp_vpath
);
1720 /* Succesful, so unlock both files */
1721 if (different_filename
)
1724 unlock_file (exp_vpath
);
1726 edit
->locked
= unlock_file (edit
->filename_vpath
);
1730 if (edit
->locked
|| save_lock
)
1731 edit
->locked
= unlock_file (edit
->filename_vpath
);
1734 edit_set_filename (edit
, exp_vpath
);
1735 if (edit
->lb
!= LB_ASIS
)
1736 edit_reload (edit
, exp_vpath
);
1738 edit
->delete_file
= 0;
1739 if (different_filename
)
1740 edit_load_syntax (edit
, NULL
, edit
->syntax_type
);
1741 vfs_path_free (exp_vpath
);
1742 edit
->force
|= REDRAW_COMPLETELY
;
1745 edit_error_dialog (_("Save as"), get_sys_error (_("Cannot save file")));
1748 /* Failed, so maintain modify (not save) lock */
1750 unlock_file (exp_vpath
);
1757 vfs_path_free (exp_vpath
);
1758 edit
->force
|= REDRAW_COMPLETELY
;
1762 /* {{{ Macro stuff starts here */
1763 /* --------------------------------------------------------------------------------------------- */
1766 edit_delete_macro_cmd (WEdit
* edit
)
1770 hotkey
= editcmd_dialog_raw_key_query (_("Delete macro"), _("Press macro hotkey:"), TRUE
);
1772 if (hotkey
!= 0 && !edit_delete_macro (edit
, hotkey
))
1773 message (D_ERROR
, _("Delete macro"), _("Macro not deleted"));
1776 /* --------------------------------------------------------------------------------------------- */
1778 /** returns FALSE on error */
1780 edit_execute_macro (WEdit
* edit
, int hotkey
)
1782 gboolean res
= FALSE
;
1786 const macros_t
*macros
;
1789 if (edit_get_macro (edit
, hotkey
, ¯os
, &indx
) &&
1790 macros
->macro
!= NULL
&& macros
->macro
->len
!= 0)
1794 edit
->force
|= REDRAW_PAGE
;
1796 for (i
= 0; i
< macros
->macro
->len
; i
++)
1798 const macro_action_t
*m_act
;
1800 m_act
= &g_array_index (macros
->macro
, struct macro_action_t
, i
);
1801 edit_execute_cmd (edit
, m_act
->action
, m_act
->ch
);
1810 /* --------------------------------------------------------------------------------------------- */
1812 /** returns FALSE on error */
1814 edit_store_macro_cmd (WEdit
* edit
)
1818 GString
*marcros_string
;
1819 mc_config_t
*macros_config
= NULL
;
1820 const char *section_name
= "editor";
1821 gchar
*macros_fname
;
1822 GArray
*macros
; /* current macro */
1824 gboolean have_macro
= FALSE
;
1825 char *skeyname
= NULL
;
1828 editcmd_dialog_raw_key_query (_("Save macro"), _("Press the macro's new hotkey:"), TRUE
);
1829 if (hotkey
== ESC_CHAR
)
1832 tmp_act
= keybind_lookup_keymap_command (editor_map
, hotkey
);
1834 /* return FALSE if try assign macro into restricted hotkeys */
1835 if (tmp_act
== CK_MacroStartRecord
1836 || tmp_act
== CK_MacroStopRecord
|| tmp_act
== CK_MacroStartStopRecord
)
1839 edit_delete_macro (edit
, hotkey
);
1841 macros_fname
= mc_config_get_full_path (MC_MACRO_FILE
);
1842 macros_config
= mc_config_init (macros_fname
, FALSE
);
1843 g_free (macros_fname
);
1845 if (macros_config
== NULL
)
1848 edit_push_undo_action (edit
, KEY_PRESS
+ edit
->start_display
);
1850 marcros_string
= g_string_sized_new (250);
1851 macros
= g_array_new (TRUE
, FALSE
, sizeof (macro_action_t
));
1853 skeyname
= lookup_key_by_code (hotkey
);
1855 for (i
= 0; i
< macro_index
; i
++)
1857 macro_action_t m_act
;
1858 const char *action_name
;
1860 action_name
= keybind_lookup_actionname (record_macro_buf
[i
].action
);
1862 if (action_name
== NULL
)
1865 m_act
.action
= record_macro_buf
[i
].action
;
1866 m_act
.ch
= record_macro_buf
[i
].ch
;
1867 g_array_append_val (macros
, m_act
);
1869 g_string_append_printf (marcros_string
, "%s:%i;", action_name
,
1870 (int) record_macro_buf
[i
].ch
);
1875 macro
.hotkey
= hotkey
;
1876 macro
.macro
= macros
;
1877 g_array_append_val (macros_list
, macro
);
1878 mc_config_set_string (macros_config
, section_name
, skeyname
, marcros_string
->str
);
1881 mc_config_del_key (macros_config
, section_name
, skeyname
);
1884 edit_macro_sort_by_hotkey ();
1886 g_string_free (marcros_string
, TRUE
);
1887 mc_config_save_file (macros_config
, NULL
);
1888 mc_config_deinit (macros_config
);
1892 /* --------------------------------------------------------------------------------------------- */
1895 edit_repeat_macro_cmd (WEdit
* edit
)
1902 f
= input_dialog (_("Repeat last commands"), _("Repeat times:"), MC_HISTORY_EDIT_REPEAT
, NULL
);
1903 if (f
== NULL
|| *f
== '\0')
1909 count_repeat
= strtol (f
, &error
, 0);
1919 edit_push_undo_action (edit
, KEY_PRESS
+ edit
->start_display
);
1920 edit
->force
|= REDRAW_PAGE
;
1922 for (j
= 0; j
< count_repeat
; j
++)
1923 for (i
= 0; i
< macro_index
; i
++)
1924 edit_execute_cmd (edit
, record_macro_buf
[i
].action
, record_macro_buf
[i
].ch
);
1925 edit_update_screen (edit
);
1929 /* --------------------------------------------------------------------------------------------- */
1930 /** return FALSE on error */
1933 edit_load_macro_cmd (WEdit
* edit
)
1935 mc_config_t
*macros_config
= NULL
;
1936 gchar
**profile_keys
, **keys
;
1937 gchar
**values
, **curr_values
;
1938 gsize len
, values_len
;
1939 const char *section_name
= "editor";
1940 gchar
*macros_fname
;
1945 macros_fname
= mc_config_get_full_path (MC_MACRO_FILE
);
1946 macros_config
= mc_config_init (macros_fname
, TRUE
);
1947 g_free (macros_fname
);
1949 if (macros_config
== NULL
)
1952 profile_keys
= keys
= mc_config_get_keys (macros_config
, section_name
, &len
);
1953 while (*profile_keys
!= NULL
)
1955 gboolean have_macro
;
1959 macros
= g_array_new (TRUE
, FALSE
, sizeof (macro_action_t
));
1961 curr_values
= values
= mc_config_get_string_list (macros_config
, section_name
,
1962 *profile_keys
, &values_len
);
1963 hotkey
= lookup_key (*profile_keys
, NULL
);
1966 while (*curr_values
!= NULL
&& *curr_values
[0] != '\0')
1968 char **macro_pair
= NULL
;
1970 macro_pair
= g_strsplit (*curr_values
, ":", 2);
1972 if (macro_pair
!= NULL
)
1974 macro_action_t m_act
;
1975 if (macro_pair
[0] == NULL
|| macro_pair
[0][0] == '\0')
1979 m_act
.action
= keybind_lookup_action (macro_pair
[0]);
1980 g_free (macro_pair
[0]);
1981 macro_pair
[0] = NULL
;
1983 if (macro_pair
[1] == NULL
|| macro_pair
[1][0] == '\0')
1987 m_act
.ch
= strtol (macro_pair
[1], NULL
, 0);
1988 g_free (macro_pair
[1]);
1989 macro_pair
[1] = NULL
;
1991 if (m_act
.action
!= 0)
1993 /* a shell command */
1994 if ((m_act
.action
/ CK_PipeBlock (0)) == 1)
1996 m_act
.action
= CK_PipeBlock (0) + (m_act
.ch
> 0 ? m_act
.ch
: 0);
1999 g_array_append_val (macros
, m_act
);
2002 g_strfreev (macro_pair
);
2009 macro
.hotkey
= hotkey
;
2010 macro
.macro
= macros
;
2011 g_array_append_val (macros_list
, macro
);
2014 g_strfreev (values
);
2017 mc_config_deinit (macros_config
);
2018 edit_macro_sort_by_hotkey ();
2022 /* }}} Macro stuff end here */
2024 /* --------------------------------------------------------------------------------------------- */
2025 /** returns TRUE on success */
2028 edit_save_confirm_cmd (WEdit
* edit
)
2032 if (edit
->filename_vpath
== NULL
)
2033 return edit_save_as_cmd (edit
);
2035 if (!edit_check_newline (edit
))
2038 if (edit_confirm_save
)
2043 filename
= vfs_path_to_str (edit
->filename_vpath
);
2044 f
= g_strdup_printf (_("Confirm save file: \"%s\""), filename
);
2046 ok
= (edit_query_dialog2 (_("Save file"), f
, _("&Save"), _("&Cancel")) == 0);
2051 return edit_save_cmd (edit
);
2054 /* --------------------------------------------------------------------------------------------- */
2056 * Ask file to edit and load it.
2058 * @return TRUE on success or cancel of ask.
2062 edit_load_cmd (Dlg_head
* h
)
2065 gboolean ret
= TRUE
; /* possible cancel */
2067 exp
= input_expand_dialog (_("Load"), _("Enter file name:"),
2068 MC_HISTORY_EDIT_LOAD
, INPUT_LAST_TEXT
);
2070 if (exp
!= NULL
&& *exp
!= '\0')
2072 vfs_path_t
*exp_vpath
;
2074 exp_vpath
= vfs_path_from_str (exp
);
2075 ret
= edit_load_file_from_filename (h
, exp_vpath
);
2076 vfs_path_free (exp_vpath
);
2084 /* --------------------------------------------------------------------------------------------- */
2086 * Load syntax file to edit.
2088 * @return TRUE on success
2092 edit_load_syntax_file (Dlg_head
* h
)
2094 vfs_path_t
*extdir_vpath
;
2096 gboolean ret
= FALSE
;
2098 if (geteuid () == 0)
2099 dir
= query_dialog (_("Syntax file edit"),
2100 _("Which syntax file you want to edit?"), D_NORMAL
, 2,
2101 _("&User"), _("&System wide"));
2104 vfs_path_build_filename (mc_global
.sysconfig_dir
, "syntax", "Syntax", (char *) NULL
);
2105 if (!exist_file (vfs_path_get_last_path_str (extdir_vpath
)))
2107 vfs_path_free (extdir_vpath
);
2109 vfs_path_build_filename (mc_global
.share_data_dir
, "syntax", "Syntax", (char *) NULL
);
2114 vfs_path_t
*user_syntax_file_vpath
;
2116 user_syntax_file_vpath
= mc_config_get_full_vpath (EDIT_SYNTAX_FILE
);
2117 check_for_default (extdir_vpath
, user_syntax_file_vpath
);
2118 ret
= edit_load_file_from_filename (h
, user_syntax_file_vpath
);
2119 vfs_path_free (user_syntax_file_vpath
);
2122 ret
= edit_load_file_from_filename (h
, extdir_vpath
);
2124 vfs_path_free (extdir_vpath
);
2129 /* --------------------------------------------------------------------------------------------- */
2131 * Load menu file to edit.
2133 * @return TRUE on success
2137 edit_load_menu_file (Dlg_head
* h
)
2139 vfs_path_t
*buffer_vpath
;
2140 vfs_path_t
*menufile_vpath
;
2144 dir
= query_dialog (_("Menu edit"),
2145 _("Which menu file do you want to edit?"), D_NORMAL
,
2146 geteuid () != 0 ? 2 : 3, _("&Local"), _("&User"), _("&System wide"));
2148 menufile_vpath
= vfs_path_build_filename (mc_global
.sysconfig_dir
, EDIT_GLOBAL_MENU
, NULL
);
2149 if (!exist_file (vfs_path_get_last_path_str (menufile_vpath
)))
2151 vfs_path_free (menufile_vpath
);
2152 menufile_vpath
= vfs_path_build_filename (mc_global
.share_data_dir
, EDIT_GLOBAL_MENU
, NULL
);
2158 buffer_vpath
= vfs_path_from_str (EDIT_LOCAL_MENU
);
2159 check_for_default (menufile_vpath
, buffer_vpath
);
2160 chmod (vfs_path_get_last_path_str (buffer_vpath
), 0600);
2164 buffer_vpath
= mc_config_get_full_vpath (EDIT_HOME_MENU
);
2165 check_for_default (menufile_vpath
, buffer_vpath
);
2169 buffer_vpath
= vfs_path_build_filename (mc_global
.sysconfig_dir
, EDIT_GLOBAL_MENU
, NULL
);
2170 if (!exist_file (vfs_path_get_last_path_str (buffer_vpath
)))
2172 vfs_path_free (buffer_vpath
);
2174 vfs_path_build_filename (mc_global
.share_data_dir
, EDIT_GLOBAL_MENU
, NULL
);
2179 vfs_path_free (menufile_vpath
);
2183 ret
= edit_load_file_from_filename (h
, buffer_vpath
);
2185 vfs_path_free (buffer_vpath
);
2186 vfs_path_free (menufile_vpath
);
2191 /* --------------------------------------------------------------------------------------------- */
2193 * Close window with opened file.
2195 * @return TRUE if file was closed.
2199 edit_close_cmd (WEdit
* edit
)
2203 ret
= (edit
!= NULL
) && edit_ok_to_exit (edit
);
2207 Dlg_head
*h
= ((Widget
*) edit
)->owner
;
2209 if (edit
->locked
!= 0)
2210 unlock_file (edit
->filename_vpath
);
2214 if (edit_widget_is_editor ((Widget
*) h
->current
->data
))
2215 edit
= (WEdit
*) h
->current
->data
;
2218 edit
= find_editor (h
);
2220 dlg_set_top_widget (edit
);
2225 edit
->force
|= REDRAW_COMPLETELY
;
2230 /* --------------------------------------------------------------------------------------------- */
2232 if mark2 is -1 then marking is from mark1 to the cursor.
2233 Otherwise its between the markers. This handles this.
2234 Returns 1 if no text is marked.
2238 eval_marks (WEdit
* edit
, off_t
* start_mark
, off_t
* end_mark
)
2240 if (edit
->mark1
!= edit
->mark2
)
2242 off_t start_bol
, start_eol
;
2243 off_t end_bol
, end_eol
;
2248 if (edit
->end_mark_curs
< 0)
2249 end_mark_curs
= edit
->curs1
;
2251 end_mark_curs
= edit
->end_mark_curs
;
2253 if (edit
->mark2
>= 0)
2255 *start_mark
= min (edit
->mark1
, edit
->mark2
);
2256 *end_mark
= max (edit
->mark1
, edit
->mark2
);
2260 *start_mark
= min (edit
->mark1
, end_mark_curs
);
2261 *end_mark
= max (edit
->mark1
, end_mark_curs
);
2262 edit
->column2
= edit
->curs_col
+ edit
->over_col
;
2265 if (edit
->column_highlight
2266 && (((edit
->mark1
> end_mark_curs
) && (edit
->column1
< edit
->column2
))
2267 || ((edit
->mark1
< end_mark_curs
) && (edit
->column1
> edit
->column2
))))
2269 start_bol
= edit_bol (edit
, *start_mark
);
2270 start_eol
= edit_eol (edit
, start_bol
- 1) + 1;
2271 end_bol
= edit_bol (edit
, *end_mark
);
2272 end_eol
= edit_eol (edit
, *end_mark
);
2273 col1
= min (edit
->column1
, edit
->column2
);
2274 col2
= max (edit
->column1
, edit
->column2
);
2276 diff1
= edit_move_forward3 (edit
, start_bol
, col2
, 0) -
2277 edit_move_forward3 (edit
, start_bol
, col1
, 0);
2278 diff2
= edit_move_forward3 (edit
, end_bol
, col2
, 0) -
2279 edit_move_forward3 (edit
, end_bol
, col1
, 0);
2281 *start_mark
-= diff1
;
2283 *start_mark
= max (*start_mark
, start_eol
);
2284 *end_mark
= min (*end_mark
, end_eol
);
2290 *start_mark
= *end_mark
= 0;
2291 edit
->column2
= edit
->column1
= 0;
2296 /* --------------------------------------------------------------------------------------------- */
2299 edit_insert_over (WEdit
* edit
)
2303 for (i
= 0; i
< edit
->over_col
; i
++)
2305 edit_insert (edit
, ' ');
2310 /* --------------------------------------------------------------------------------------------- */
2313 edit_insert_column_of_text_from_file (WEdit
* edit
, int file
,
2314 off_t
* start_pos
, off_t
* end_pos
, long *col1
, long *col2
)
2318 off_t blocklen
= -1, width
= 0;
2319 unsigned char *data
;
2321 cursor
= edit
->curs1
;
2322 col
= edit_get_col (edit
);
2323 data
= g_malloc0 (TEMP_BUF_LEN
);
2325 while ((blocklen
= mc_read (file
, (char *) data
, TEMP_BUF_LEN
)) > 0)
2328 for (width
= 0; width
< blocklen
; width
++)
2330 if (data
[width
] == '\n')
2333 for (i
= 0; i
< blocklen
; i
++)
2335 if (data
[i
] == '\n')
2336 { /* fill in and move to next line */
2339 if (edit_get_byte (edit
, edit
->curs1
) != '\n')
2341 l
= width
- (edit_get_col (edit
) - col
);
2344 edit_insert (edit
, ' ');
2348 for (p
= edit
->curs1
;; p
++)
2350 if (p
== edit
->last_byte
)
2352 edit_cursor_move (edit
, edit
->last_byte
- edit
->curs1
);
2353 edit_insert_ahead (edit
, '\n');
2357 if (edit_get_byte (edit
, p
) == '\n')
2363 edit_cursor_move (edit
, edit_move_forward3 (edit
, p
, col
, 0) - edit
->curs1
);
2364 l
= col
- edit_get_col (edit
);
2365 while (l
>= space_width
)
2367 edit_insert (edit
, ' ');
2372 edit_insert (edit
, data
[i
]);
2376 *col2
= col
+ width
;
2377 *start_pos
= cursor
;
2378 *end_pos
= edit
->curs1
;
2379 edit_cursor_move (edit
, cursor
- edit
->curs1
);
2385 /* --------------------------------------------------------------------------------------------- */
2388 edit_block_copy_cmd (WEdit
* edit
)
2390 off_t start_mark
, end_mark
, current
= edit
->curs1
;
2395 unsigned char *copy_buf
;
2397 edit_update_curs_col (edit
);
2398 if (eval_marks (edit
, &start_mark
, &end_mark
))
2401 copy_buf
= edit_get_block (edit
, start_mark
, end_mark
, &size
);
2403 /* all that gets pushed are deletes hence little space is used on the stack */
2405 edit_push_markers (edit
);
2407 if (edit
->column_highlight
)
2409 col_delta
= abs (edit
->column2
- edit
->column1
);
2410 edit_insert_column_of_text (edit
, copy_buf
, size
, col_delta
, &mark1
, &mark2
, &c1
, &c2
);
2415 edit_insert_ahead (edit
, copy_buf
[size
]);
2419 edit_scroll_screen_over_cursor (edit
);
2421 if (edit
->column_highlight
)
2422 edit_set_markers (edit
, edit
->curs1
, mark2
, c1
, c2
);
2423 else if (start_mark
< current
&& end_mark
> current
)
2424 edit_set_markers (edit
, start_mark
, end_mark
+ end_mark
- start_mark
, 0, 0);
2426 edit
->force
|= REDRAW_PAGE
;
2430 /* --------------------------------------------------------------------------------------------- */
2433 edit_block_move_cmd (WEdit
* edit
)
2436 unsigned char *copy_buf
= NULL
;
2437 off_t start_mark
, end_mark
;
2440 if (eval_marks (edit
, &start_mark
, &end_mark
))
2443 if (!edit
->column_highlight
&& edit
->curs1
> start_mark
&& edit
->curs1
< end_mark
)
2446 line
= edit
->curs_line
;
2447 if (edit
->mark2
< 0)
2448 edit_mark_cmd (edit
, FALSE
);
2449 edit_push_markers (edit
);
2451 if (edit
->column_highlight
)
2455 long c1
, c2
, b_width
;
2458 c1
= min (edit
->column1
, edit
->column2
);
2459 c2
= max (edit
->column1
, edit
->column2
);
2462 edit_update_curs_col (edit
);
2465 x2
= x
+ edit
->over_col
;
2467 /* do nothing when cursor inside first line of selected area */
2468 if ((edit_eol (edit
, edit
->curs1
) == edit_eol (edit
, start_mark
)) && x2
> c1
&& x2
<= c2
)
2471 if (edit
->curs1
> start_mark
&& edit
->curs1
< edit_eol (edit
, end_mark
))
2475 else if (x
> c1
&& x
<= c2
)
2478 /* save current selection into buffer */
2479 copy_buf
= edit_get_block (edit
, start_mark
, end_mark
, &size
);
2481 /* remove current selection */
2482 edit_block_delete_cmd (edit
);
2484 edit
->over_col
= max (0, edit
->over_col
- b_width
);
2485 /* calculate the cursor pos after delete block */
2486 current
= edit_move_forward3 (edit
, edit_bol (edit
, edit
->curs1
), x
, 0);
2487 edit_cursor_move (edit
, current
- edit
->curs1
);
2488 edit_scroll_screen_over_cursor (edit
);
2490 /* add TWS if need before block insertion */
2491 if (option_cursor_beyond_eol
&& edit
->over_col
> 0)
2492 edit_insert_over (edit
);
2494 edit_insert_column_of_text (edit
, copy_buf
, size
, b_width
, &mark1
, &mark2
, &c1
, &c2
);
2495 edit_set_markers (edit
, mark1
, mark2
, c1
, c2
);
2501 current
= edit
->curs1
;
2502 copy_buf
= g_malloc0 (end_mark
- start_mark
);
2503 edit_cursor_move (edit
, start_mark
- edit
->curs1
);
2504 edit_scroll_screen_over_cursor (edit
);
2506 for (count
= start_mark
; count
< end_mark
; count
++)
2507 copy_buf
[end_mark
- count
- 1] = edit_delete (edit
, TRUE
);
2509 edit_scroll_screen_over_cursor (edit
);
2510 edit_cursor_move (edit
,
2511 current
- edit
->curs1
-
2512 (((current
- edit
->curs1
) > 0) ? end_mark
- start_mark
: 0));
2513 edit_scroll_screen_over_cursor (edit
);
2514 while (count
-- > start_mark
)
2515 edit_insert_ahead (edit
, copy_buf
[end_mark
- count
- 1]);
2516 edit_set_markers (edit
, edit
->curs1
, edit
->curs1
+ end_mark
- start_mark
, 0, 0);
2519 edit_scroll_screen_over_cursor (edit
);
2521 edit
->force
|= REDRAW_PAGE
;
2524 /* --------------------------------------------------------------------------------------------- */
2525 /** returns 1 if canceelled by user */
2528 edit_block_delete_cmd (WEdit
* edit
)
2530 off_t start_mark
, end_mark
;
2531 if (eval_marks (edit
, &start_mark
, &end_mark
))
2533 edit_delete_line (edit
);
2536 return edit_block_delete (edit
);
2539 /* --------------------------------------------------------------------------------------------- */
2540 /** call with edit = 0 before shutdown to close memory leaks */
2543 edit_replace_cmd (WEdit
* edit
, int again
)
2545 /* 1 = search string, 2 = replace with */
2546 static char *saved1
= NULL
; /* saved default[123] */
2547 static char *saved2
= NULL
;
2548 char *input1
= NULL
; /* user input from the dialog */
2549 char *input2
= NULL
;
2550 GString
*input2_str
= NULL
;
2553 long times_replaced
= 0;
2554 gboolean once_found
= FALSE
;
2558 g_free (saved1
), saved1
= NULL
;
2559 g_free (saved2
), saved2
= NULL
;
2563 edit
->force
|= REDRAW_COMPLETELY
;
2565 if (again
&& !saved1
&& !saved2
)
2570 input1
= g_strdup (saved1
? saved1
: "");
2571 input2
= g_strdup (saved2
? saved2
: "");
2575 char *tmp_inp1
, *tmp_inp2
;
2577 disp1
= edit_replace_cmd__conv_to_display (saved1
? saved1
: (char *) "");
2578 disp2
= edit_replace_cmd__conv_to_display (saved2
? saved2
: (char *) "");
2580 edit_push_undo_action (edit
, KEY_PRESS
+ edit
->start_display
);
2582 editcmd_dialog_replace_show (edit
, disp1
, disp2
, &input1
, &input2
);
2587 if (input1
== NULL
|| *input1
== '\0')
2589 edit
->force
= REDRAW_COMPLETELY
;
2595 input1
= edit_replace_cmd__conv_to_input (input1
);
2596 input2
= edit_replace_cmd__conv_to_input (input2
);
2600 g_free (saved1
), saved1
= g_strdup (input1
);
2601 g_free (saved2
), saved2
= g_strdup (input2
);
2603 mc_search_free (edit
->search
);
2604 edit
->search
= NULL
;
2607 input2_str
= g_string_new (input2
);
2611 edit
->search
= mc_search_new (input1
, -1);
2612 if (edit
->search
== NULL
)
2614 edit
->search_start
= edit
->curs1
;
2617 edit
->search
->search_type
= edit_search_options
.type
;
2618 edit
->search
->is_all_charsets
= edit_search_options
.all_codepages
;
2619 edit
->search
->is_case_sensitive
= edit_search_options
.case_sens
;
2620 edit
->search
->whole_words
= edit_search_options
.whole_words
;
2621 edit
->search
->search_fn
= edit_search_cmd_callback
;
2622 edit
->search_line_type
= edit_get_search_line_type (edit
->search
);
2623 edit_search_fix_search_start_if_selection (edit
);
2626 if (edit
->found_len
&& edit
->search_start
== edit
->found_start
+ 1
2627 && edit_search_options
.backwards
)
2628 edit
->search_start
--;
2630 if (edit
->found_len
&& edit
->search_start
== edit
->found_start
- 1
2631 && !edit_search_options
.backwards
)
2632 edit
->search_start
++;
2638 if (!editcmd_find (edit
, &len
))
2640 if (!(edit
->search
->error
== MC_SEARCH_E_OK
||
2641 (once_found
&& edit
->search
->error
== MC_SEARCH_E_NOTFOUND
)))
2643 edit_error_dialog (_("Search"), edit
->search
->error_str
);
2649 edit
->search_start
= edit
->search
->normal_offset
;
2650 /*returns negative on not found or error in pattern */
2652 if ((edit
->search_start
>= 0) && (edit
->search_start
< edit
->last_byte
))
2657 edit
->found_start
= edit
->search_start
;
2658 i
= edit
->found_len
= len
;
2660 edit_cursor_move (edit
, edit
->search_start
- edit
->curs1
);
2661 edit_scroll_screen_over_cursor (edit
);
2663 if (edit
->replace_mode
== 0)
2668 l
= (long) (edit
->curs_row
- edit
->widget
.lines
/ 3);
2670 edit_scroll_downward (edit
, l
);
2672 edit_scroll_upward (edit
, -l
);
2674 edit_scroll_screen_over_cursor (edit
);
2675 edit
->force
|= REDRAW_PAGE
;
2676 edit_render_keypress (edit
);
2678 /*so that undo stops at each query */
2679 edit_push_key_press (edit
);
2680 /* and prompt 2/3 down */
2681 disp1
= edit_replace_cmd__conv_to_display (saved1
);
2682 disp2
= edit_replace_cmd__conv_to_display (saved2
);
2683 prompt
= editcmd_dialog_replace_prompt_show (edit
, disp1
, disp2
, -1, -1);
2687 if (prompt
== B_REPLACE_ALL
)
2688 edit
->replace_mode
= 1;
2689 else if (prompt
== B_SKIP_REPLACE
)
2691 if (edit_search_options
.backwards
)
2692 edit
->search_start
--;
2694 edit
->search_start
++;
2695 continue; /* loop */
2697 else if (prompt
== B_CANCEL
)
2699 edit
->replace_mode
= -1;
2704 repl_str
= mc_search_prepare_replace_str (edit
->search
, input2_str
);
2706 if (edit
->search
->error
!= MC_SEARCH_E_OK
)
2708 edit_error_dialog (_("Replace"), edit
->search
->error_str
);
2709 g_string_free (repl_str
, TRUE
);
2713 /* delete then insert new */
2714 for (i
= 0; i
< len
; i
++)
2715 edit_delete (edit
, TRUE
);
2717 for (i
= 0; i
< repl_str
->len
; i
++)
2718 edit_insert (edit
, repl_str
->str
[i
]);
2720 edit
->found_len
= repl_str
->len
;
2721 g_string_free (repl_str
, TRUE
);
2724 /* so that we don't find the same string again */
2725 if (edit_search_options
.backwards
)
2727 edit
->search_start
--;
2731 edit
->search_start
+= edit
->found_len
+ (len
== 0 ? 1 : 0);
2733 if (edit
->search_start
>= edit
->last_byte
)
2737 edit_scroll_screen_over_cursor (edit
);
2741 /* try and find from right here for next search */
2742 edit
->search_start
= edit
->curs1
;
2743 edit_update_curs_col (edit
);
2745 edit
->force
|= REDRAW_PAGE
;
2746 edit_render_keypress (edit
);
2748 if (times_replaced
== 0)
2749 query_dialog (_("Replace"), _("Search string not found"), D_NORMAL
, 1, _("&OK"));
2753 while (edit
->replace_mode
>= 0);
2755 edit_scroll_screen_over_cursor (edit
);
2756 edit
->force
|= REDRAW_COMPLETELY
;
2757 edit_render_keypress (edit
);
2759 if ((edit
->replace_mode
== 1) && (times_replaced
!= 0))
2760 message (D_NORMAL
, _("Replace"), _("%ld replacements made"), times_replaced
);
2765 if (input2_str
!= NULL
)
2766 g_string_free (input2_str
, TRUE
);
2769 /* --------------------------------------------------------------------------------------------- */
2772 edit_search_cmd_callback (const void *user_data
, gsize char_offset
, int *current_char
)
2774 *current_char
= edit_get_byte ((WEdit
*) user_data
, (off_t
) char_offset
);
2775 return MC_SEARCH_CB_OK
;
2778 /* --------------------------------------------------------------------------------------------- */
2781 edit_search_cmd (WEdit
* edit
, gboolean again
)
2789 else if (edit
->last_search_string
!= NULL
)
2790 edit_do_search (edit
);
2793 /* find last search string in history */
2796 history
= history_get (MC_HISTORY_SHARED_SEARCH
);
2797 if (history
!= NULL
&& history
->data
!= NULL
)
2799 edit
->last_search_string
= (char *) history
->data
;
2800 history
->data
= NULL
;
2801 history
= g_list_first (history
);
2802 g_list_foreach (history
, (GFunc
) g_free
, NULL
);
2803 g_list_free (history
);
2805 edit
->search
= mc_search_new (edit
->last_search_string
, -1);
2806 if (edit
->search
== NULL
)
2808 /* if not... then ask for an expression */
2809 g_free (edit
->last_search_string
);
2810 edit
->last_search_string
= NULL
;
2815 edit
->search
->search_type
= edit_search_options
.type
;
2816 edit
->search
->is_all_charsets
= edit_search_options
.all_codepages
;
2817 edit
->search
->is_case_sensitive
= edit_search_options
.case_sens
;
2818 edit
->search
->whole_words
= edit_search_options
.whole_words
;
2819 edit
->search
->search_fn
= edit_search_cmd_callback
;
2820 edit
->search_line_type
= edit_get_search_line_type (edit
->search
);
2821 edit_do_search (edit
);
2826 /* if not... then ask for an expression */
2827 g_free (edit
->last_search_string
);
2828 edit
->last_search_string
= NULL
;
2835 /* --------------------------------------------------------------------------------------------- */
2837 * Check if it's OK to close the file. If there are unsaved changes, ask user.
2839 * @return TRUE if it's OK to exit, FALSE to continue editing.
2843 edit_ok_to_exit (WEdit
* edit
)
2845 char *fname
= (char *) N_("[NoName]");
2849 if (!edit
->modified
)
2852 if (edit
->filename_vpath
!= NULL
)
2853 fname
= vfs_path_to_str (edit
->filename_vpath
);
2856 fname
= g_strdup (_(fname
));
2859 fname
= g_strdup (fname
);
2862 if (!mc_global
.midnight_shutdown
)
2864 if (!edit_check_newline (edit
))
2872 msg
= g_strdup_printf (_("File %s was modified.\nSave before close?"), fname
);
2873 act
= edit_query_dialog3 (_("Close file"), msg
, _("&Yes"), _("&No"), _("&Cancel"));
2877 msg
= g_strdup_printf (_("Midnight Commander is being shut down.\nSave modified file %s?"),
2879 act
= edit_query_dialog2 (_("Quit"), msg
, _("&Yes"), _("&No"));
2892 edit_push_markers (edit
);
2893 edit_set_markers (edit
, 0, 0, 0, 0);
2894 if (!edit_save_cmd (edit
) || mc_global
.midnight_shutdown
)
2895 return mc_global
.midnight_shutdown
;
2899 case 2: /* Cancel quit */
2907 /* --------------------------------------------------------------------------------------------- */
2908 /** save block, returns TRUE on success */
2911 edit_save_block (WEdit
* edit
, const char *filename
, off_t start
, off_t finish
)
2917 vpath
= vfs_path_from_str (filename
);
2918 file
= mc_open (vpath
, O_CREAT
| O_WRONLY
| O_TRUNC
,
2919 S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IROTH
| O_BINARY
);
2920 vfs_path_free (vpath
);
2924 if (edit
->column_highlight
)
2928 r
= mc_write (file
, VERTICAL_MAGIC
, sizeof (VERTICAL_MAGIC
));
2931 unsigned char *block
, *p
;
2933 p
= block
= edit_get_block (edit
, start
, finish
, &len
);
2936 r
= mc_write (file
, p
, len
);
2951 len
= finish
- start
;
2952 buf
= g_malloc0 (TEMP_BUF_LEN
);
2953 while (start
!= finish
)
2955 end
= min (finish
, start
+ TEMP_BUF_LEN
);
2956 for (; i
< end
; i
++)
2957 buf
[i
- start
] = edit_get_byte (edit
, i
);
2958 len
-= mc_write (file
, (char *) buf
, end
- start
);
2968 /* --------------------------------------------------------------------------------------------- */
2971 edit_paste_from_history (WEdit
* edit
)
2974 edit_error_dialog (_("Error"), _("This function is not implemented"));
2977 /* --------------------------------------------------------------------------------------------- */
2980 edit_copy_to_X_buf_cmd (WEdit
* edit
)
2982 off_t start_mark
, end_mark
;
2984 if (eval_marks (edit
, &start_mark
, &end_mark
))
2986 if (!edit_save_block_to_clip_file (edit
, start_mark
, end_mark
))
2988 edit_error_dialog (_("Copy to clipboard"), get_sys_error (_("Unable to save to file")));
2991 /* try use external clipboard utility */
2992 mc_event_raise (MCEVENT_GROUP_CORE
, "clipboard_file_to_ext_clip", NULL
);
2997 /* --------------------------------------------------------------------------------------------- */
3000 edit_cut_to_X_buf_cmd (WEdit
* edit
)
3002 off_t start_mark
, end_mark
;
3004 if (eval_marks (edit
, &start_mark
, &end_mark
))
3006 if (!edit_save_block_to_clip_file (edit
, start_mark
, end_mark
))
3008 edit_error_dialog (_("Cut to clipboard"), _("Unable to save to file"));
3011 /* try use external clipboard utility */
3012 mc_event_raise (MCEVENT_GROUP_CORE
, "clipboard_file_to_ext_clip", NULL
);
3014 edit_block_delete_cmd (edit
);
3015 edit_mark_cmd (edit
, TRUE
);
3020 /* --------------------------------------------------------------------------------------------- */
3023 edit_paste_from_X_buf_cmd (WEdit
* edit
)
3028 /* try use external clipboard utility */
3029 mc_event_raise (MCEVENT_GROUP_CORE
, "clipboard_file_from_ext_clip", NULL
);
3030 tmp
= mc_config_get_full_vpath (EDIT_CLIP_FILE
);
3031 ret
= (edit_insert_file (edit
, tmp
) >= 0);
3032 vfs_path_free (tmp
);
3037 /* --------------------------------------------------------------------------------------------- */
3039 * Ask user for the line and go to that line.
3040 * Negative numbers mean line from the end (i.e. -1 is the last line).
3044 edit_goto_cmd (WEdit
* edit
)
3047 static long line
= 0; /* line as typed, saved as default */
3052 g_snprintf (s
, sizeof (s
), "%ld", line
);
3053 f
= input_dialog (_("Goto line"), _("Enter line:"), MC_HISTORY_EDIT_GOTO_LINE
, line
? s
: "");
3063 l
= strtol (f
, &error
, 0);
3072 l
= edit
->total_lines
+ l
+ 2;
3073 edit_move_display (edit
, l
- edit
->widget
.lines
/ 2 - 1);
3074 edit_move_to_line (edit
, l
- 1);
3075 edit
->force
|= REDRAW_COMPLETELY
;
3080 /* --------------------------------------------------------------------------------------------- */
3081 /** Return TRUE on success */
3084 edit_save_block_cmd (WEdit
* edit
)
3086 off_t start_mark
, end_mark
;
3088 gboolean ret
= FALSE
;
3090 if (eval_marks (edit
, &start_mark
, &end_mark
))
3093 tmp
= mc_config_get_full_path (EDIT_CLIP_FILE
);
3095 input_expand_dialog (_("Save block"), _("Enter file name:"),
3096 MC_HISTORY_EDIT_SAVE_BLOCK
, tmp
);
3098 edit_push_undo_action (edit
, KEY_PRESS
+ edit
->start_display
);
3100 if (exp
!= NULL
&& *exp
!= '\0')
3102 if (edit_save_block (edit
, exp
, start_mark
, end_mark
))
3105 edit_error_dialog (_("Save block"), get_sys_error (_("Cannot save file")));
3107 edit
->force
|= REDRAW_COMPLETELY
;
3116 /* --------------------------------------------------------------------------------------------- */
3118 /** returns TRUE on success */
3120 edit_insert_file_cmd (WEdit
* edit
)
3124 gboolean ret
= FALSE
;
3126 tmp
= mc_config_get_full_path (EDIT_CLIP_FILE
);
3127 exp
= input_expand_dialog (_("Insert file"), _("Enter file name:"),
3128 MC_HISTORY_EDIT_INSERT_FILE
, tmp
);
3131 edit_push_undo_action (edit
, KEY_PRESS
+ edit
->start_display
);
3133 if (exp
!= NULL
&& *exp
!= '\0')
3135 vfs_path_t
*exp_vpath
;
3137 exp_vpath
= vfs_path_from_str (exp
);
3138 ret
= (edit_insert_file (edit
, exp_vpath
) >= 0);
3139 vfs_path_free (exp_vpath
);
3142 edit_error_dialog (_("Insert file"), get_sys_error (_("Cannot insert file")));
3147 edit
->force
|= REDRAW_COMPLETELY
;
3151 /* --------------------------------------------------------------------------------------------- */
3152 /** sorts a block, returns -1 on system fail, 1 on cancel and 0 on success */
3155 edit_sort_cmd (WEdit
* edit
)
3157 static char *old
= 0;
3158 char *exp
, *tmp
, *tmp_edit_block_name
, *tmp_edit_temp_name
;
3159 off_t start_mark
, end_mark
;
3162 if (eval_marks (edit
, &start_mark
, &end_mark
))
3164 edit_error_dialog (_("Sort block"), _("You must first highlight a block of text"));
3168 tmp
= mc_config_get_full_path (EDIT_BLOCK_FILE
);
3169 edit_save_block (edit
, tmp
, start_mark
, end_mark
);
3172 exp
= input_dialog (_("Run sort"),
3173 _("Enter sort options (see manpage) separated by whitespace:"),
3174 MC_HISTORY_EDIT_SORT
, (old
!= NULL
) ? old
: "");
3180 tmp_edit_block_name
= mc_config_get_full_path (EDIT_BLOCK_FILE
);
3181 tmp_edit_temp_name
= mc_config_get_full_path (EDIT_TEMP_FILE
);
3183 g_strconcat (" sort ", exp
, " ", tmp_edit_block_name
,
3184 " > ", tmp_edit_temp_name
, (char *) NULL
);
3185 g_free (tmp_edit_temp_name
);
3186 g_free (tmp_edit_block_name
);
3192 if (e
== -1 || e
== 127)
3194 edit_error_dialog (_("Sort"), get_sys_error (_("Cannot execute sort command")));
3199 sprintf (q
, "%d ", e
);
3200 tmp
= g_strdup_printf (_("Sort returned non-zero: %s"), q
);
3201 edit_error_dialog (_("Sort"), tmp
);
3207 edit
->force
|= REDRAW_COMPLETELY
;
3209 if (edit_block_delete_cmd (edit
))
3213 vfs_path_t
*tmp_vpath
;
3215 tmp_vpath
= mc_config_get_full_vpath (EDIT_TEMP_FILE
);
3216 edit_insert_file (edit
, tmp_vpath
);
3217 vfs_path_free (tmp_vpath
);
3222 /* --------------------------------------------------------------------------------------------- */
3224 * Ask user for a command, execute it and paste its output back to the
3229 edit_ext_cmd (WEdit
* edit
)
3231 char *exp
, *tmp
, *tmp_edit_temp_file
;
3235 input_dialog (_("Paste output of external command"),
3236 _("Enter shell command(s):"), MC_HISTORY_EDIT_PASTE_EXTCMD
, NULL
);
3241 tmp_edit_temp_file
= mc_config_get_full_path (EDIT_TEMP_FILE
);
3242 tmp
= g_strconcat (exp
, " > ", tmp_edit_temp_file
, (char *) NULL
);
3243 g_free (tmp_edit_temp_file
);
3250 edit_error_dialog (_("External command"), get_sys_error (_("Cannot execute command")));
3254 edit
->force
|= REDRAW_COMPLETELY
;
3257 vfs_path_t
*tmp_vpath
;
3259 tmp_vpath
= mc_config_get_full_vpath (EDIT_TEMP_FILE
);
3260 edit_insert_file (edit
, tmp_vpath
);
3261 vfs_path_free (tmp_vpath
);
3266 /* --------------------------------------------------------------------------------------------- */
3267 /** if block is 1, a block must be highlighted and the shell command
3268 processes it. If block is 0 the shell command is a straight system
3269 command, that just produces some output which is to be inserted */
3272 edit_block_process_cmd (WEdit
* edit
, int macro_number
)
3275 char *macros_fname
= NULL
;
3277 fname
= g_strdup_printf ("%s.%i.sh", MC_EXTMACRO_FILE
, macro_number
);
3278 macros_fname
= g_build_filename (mc_config_get_data_path (), fname
, (char *) NULL
);
3279 user_menu (edit
, macros_fname
, 0);
3281 g_free (macros_fname
);
3282 edit
->force
|= REDRAW_COMPLETELY
;
3285 /* --------------------------------------------------------------------------------------------- */
3288 edit_mail_dialog (WEdit
* edit
)
3291 char *tmail_subject
;
3294 static char *mail_cc_last
= 0;
3295 static char *mail_subject_last
= 0;
3296 static char *mail_to_last
= 0;
3298 QuickWidget quick_widgets
[] = {
3299 /* 0 */ QUICK_BUTTON (6, 10, 9, MAIL_DLG_HEIGHT
, N_("&Cancel"), B_CANCEL
, NULL
),
3300 /* 1 */ QUICK_BUTTON (2, 10, 9, MAIL_DLG_HEIGHT
, N_("&OK"), B_ENTER
, NULL
),
3301 /* 2 */ QUICK_INPUT (3, 50, 8, MAIL_DLG_HEIGHT
, "", 44, 0, "mail-dlg-input", &tmail_cc
),
3302 /* 3 */ QUICK_LABEL (3, 50, 7, MAIL_DLG_HEIGHT
, N_("Copies to")),
3303 /* 4 */ QUICK_INPUT (3, 50, 6, MAIL_DLG_HEIGHT
, "", 44, 0, "mail-dlg-input-2",
3305 /* 5 */ QUICK_LABEL (3, 50, 5, MAIL_DLG_HEIGHT
, N_("Subject")),
3306 /* 6 */ QUICK_INPUT (3, 50, 4, MAIL_DLG_HEIGHT
, "", 44, 0, "mail-dlg-input-3", &tmail_to
),
3307 /* 7 */ QUICK_LABEL (3, 50, 3, MAIL_DLG_HEIGHT
, N_("To")),
3308 /* 8 */ QUICK_LABEL (3, 50, 2, MAIL_DLG_HEIGHT
, N_("mail -s <subject> -c <cc> <to>")),
3312 QuickDialog Quick_input
= {
3313 50, MAIL_DLG_HEIGHT
, -1, -1, N_("Mail"),
3314 "[Input Line Keys]", quick_widgets
, NULL
, NULL
, FALSE
3317 quick_widgets
[2].u
.input
.text
= mail_cc_last
? mail_cc_last
: "";
3318 quick_widgets
[4].u
.input
.text
= mail_subject_last
? mail_subject_last
: "";
3319 quick_widgets
[6].u
.input
.text
= mail_to_last
? mail_to_last
: "";
3321 if (quick_dialog (&Quick_input
) != B_CANCEL
)
3323 g_free (mail_cc_last
);
3324 g_free (mail_subject_last
);
3325 g_free (mail_to_last
);
3326 mail_cc_last
= tmail_cc
;
3327 mail_subject_last
= tmail_subject
;
3328 mail_to_last
= tmail_to
;
3329 pipe_mail (edit
, mail_to_last
, mail_subject_last
, mail_cc_last
);
3334 /*******************/
3335 /* Word Completion */
3336 /*******************/
3338 /* --------------------------------------------------------------------------------------------- */
3340 * Complete current word using regular expression search
3341 * backwards beginning at the current cursor position.
3345 edit_complete_word_cmd (WEdit
* edit
)
3347 gsize i
, max_len
, word_len
= 0, num_compl
= 0;
3348 off_t word_start
= 0;
3349 unsigned char *bufpos
;
3351 struct selection
compl[MAX_WORD_COMPLETIONS
]; /* completions */
3353 /* search start of word to be completed */
3354 if (!edit_find_word_start (edit
, &word_start
, &word_len
))
3357 /* prepare match expression */
3358 bufpos
= &edit
->buffers1
[word_start
>> S_EDIT_BUF_SIZE
][word_start
& M_EDIT_BUF_SIZE
];
3360 /* match_expr = g_strdup_printf ("\\b%.*s[a-zA-Z_0-9]+", word_len, bufpos); */
3363 ("(^|\\s+|\\b)%.*s[^\\s\\.=\\+\\[\\]\\(\\)\\,\\;\\:\\\"\\'\\-\\?\\/\\|\\\\\\{\\}\\*\\&\\^\\%%\\$#@\\!]+",
3364 (int) word_len
, bufpos
);
3366 /* collect the possible completions */
3367 /* start search from begin to end of file */
3369 edit_collect_completions (edit
, word_start
, word_len
, match_expr
,
3370 (struct selection
*) &compl, &num_compl
);
3374 /* insert completed word if there is only one match */
3377 for (i
= word_len
; i
< compl[0].len
; i
++)
3378 edit_insert (edit
, *(compl[0].text
+ i
));
3380 /* more than one possible completion => ask the user */
3383 /* !!! usually only a beep is expected and when <ALT-TAB> is !!! */
3384 /* !!! pressed again the selection dialog pops up, but that !!! */
3385 /* !!! seems to require a further internal state !!! */
3388 /* let the user select the preferred completion */
3389 editcmd_dialog_completion_show (edit
, max_len
, word_len
,
3390 (struct selection
*) &compl, num_compl
);
3394 g_free (match_expr
);
3395 /* release memory before return */
3396 for (i
= 0; i
< num_compl
; i
++)
3397 g_free (compl[i
].text
);
3400 /* --------------------------------------------------------------------------------------------- */
3404 edit_select_codepage_cmd (WEdit
* edit
)
3406 if (do_select_codepage ())
3407 edit_set_codeset (edit
);
3409 edit
->force
= REDRAW_PAGE
;
3410 send_message ((Widget
*) edit
, WIDGET_DRAW
, 0);
3414 /* --------------------------------------------------------------------------------------------- */
3417 edit_insert_literal_cmd (WEdit
* edit
)
3419 int char_for_insertion
;
3421 char_for_insertion
= editcmd_dialog_raw_key_query (_("Insert literal"),
3422 _("Press any key:"), FALSE
);
3423 edit_execute_key_command (edit
, -1, ascii_alpha_to_cntrl (char_for_insertion
));
3426 /* --------------------------------------------------------------------------------------------- */
3429 edit_begin_end_macro_cmd (WEdit
* edit
)
3431 /* edit is a pointer to the widget */
3434 unsigned long command
= macro_index
< 0 ? CK_MacroStartRecord
: CK_MacroStopRecord
;
3435 edit_execute_key_command (edit
, command
, -1);
3439 /* --------------------------------------------------------------------------------------------- */
3442 edit_begin_end_repeat_cmd (WEdit
* edit
)
3444 /* edit is a pointer to the widget */
3447 unsigned long command
= macro_index
< 0 ? CK_RepeatStartRecord
: CK_RepeatStopRecord
;
3448 edit_execute_key_command (edit
, command
, -1);
3452 /* --------------------------------------------------------------------------------------------- */
3455 edit_load_forward_cmd (WEdit
* edit
)
3458 && edit_query_dialog2 (_("Warning"),
3459 _("Current text was modified without a file save.\n"
3460 "Continue discards these changes"), _("C&ontinue"),
3463 edit
->force
|= REDRAW_COMPLETELY
;
3467 if (edit_stack_iterator
+ 1 >= MAX_HISTORY_MOVETO
)
3470 if (edit_history_moveto
[edit_stack_iterator
+ 1].line
< 1)
3473 edit_stack_iterator
++;
3474 if (edit_history_moveto
[edit_stack_iterator
].filename_vpath
!= NULL
)
3475 return edit_reload_line (edit
, edit_history_moveto
[edit_stack_iterator
].filename_vpath
,
3476 edit_history_moveto
[edit_stack_iterator
].line
);
3481 /* --------------------------------------------------------------------------------------------- */
3484 edit_load_back_cmd (WEdit
* edit
)
3487 && edit_query_dialog2 (_("Warning"),
3488 _("Current text was modified without a file save.\n"
3489 "Continue discards these changes"), _("C&ontinue"),
3492 edit
->force
|= REDRAW_COMPLETELY
;
3496 /* we are in the bottom of the stack, NO WAY! */
3497 if (edit_stack_iterator
== 0)
3500 edit_stack_iterator
--;
3501 if (edit_history_moveto
[edit_stack_iterator
].filename_vpath
!= NULL
)
3502 return edit_reload_line (edit
, edit_history_moveto
[edit_stack_iterator
].filename_vpath
,
3503 edit_history_moveto
[edit_stack_iterator
].line
);
3508 /* --------------------------------------------------------------------------------------------- */
3511 edit_get_match_keyword_cmd (WEdit
* edit
)
3513 gsize word_len
= 0, max_len
= 0;
3516 off_t word_start
= 0;
3517 unsigned char *bufpos
;
3521 char *tagfile
= NULL
;
3523 etags_hash_t def_hash
[MAX_DEFINITIONS
];
3525 for (i
= 0; i
< MAX_DEFINITIONS
; i
++)
3527 def_hash
[i
].filename
= NULL
;
3530 /* search start of word to be completed */
3531 if (!edit_find_word_start (edit
, &word_start
, &word_len
))
3534 /* prepare match expression */
3535 bufpos
= &edit
->buffers1
[word_start
>> S_EDIT_BUF_SIZE
][word_start
& M_EDIT_BUF_SIZE
];
3536 match_expr
= g_strdup_printf ("%.*s", (int) word_len
, bufpos
);
3538 ptr
= g_get_current_dir ();
3539 path
= g_strconcat (ptr
, G_DIR_SEPARATOR_S
, (char *) NULL
);
3542 /* Recursive search file 'TAGS' in parent dirs */
3545 ptr
= g_path_get_dirname (path
);
3549 tagfile
= mc_build_filename (path
, TAGS_NAME
, (char *) NULL
);
3550 if (exist_file (tagfile
))
3553 while (strcmp (path
, G_DIR_SEPARATOR_S
) != 0);
3558 etags_set_definition_hash (tagfile
, path
, match_expr
, (etags_hash_t
*) & def_hash
);
3563 max_len
= MAX_WIDTH_DEF_DIALOG
;
3567 editcmd_dialog_select_definition_show (edit
, match_expr
, max_len
, word_len
,
3568 (etags_hash_t
*) & def_hash
, num_def
);
3570 g_free (match_expr
);
3573 /* --------------------------------------------------------------------------------------------- */
3577 edit_suggest_current_word (WEdit
* edit
)
3581 off_t word_start
= 0;
3582 int retval
= B_SKIP_WORD
;
3585 /* search start of word to spell check */
3586 match_word
= edit_get_word_from_pos (edit
, edit
->curs1
, &word_start
, &word_len
, &cut_len
);
3589 if (mc_global
.source_codepage
>= 0 && (mc_global
.source_codepage
!= mc_global
.display_codepage
))
3593 tmp_word
= str_convert_to_display (match_word
);
3594 g_free (match_word
);
3595 match_word
= g_string_free (tmp_word
, FALSE
);
3598 if (!aspell_check (match_word
, (int) word_len
))
3603 suggest
= g_array_new (TRUE
, FALSE
, sizeof (char *));
3605 res
= aspell_suggest (suggest
, match_word
, (int) word_len
);
3608 char *new_word
= NULL
;
3610 edit
->found_start
= word_start
;
3611 edit
->found_len
= word_len
;
3612 edit
->force
|= REDRAW_PAGE
;
3613 edit_scroll_screen_over_cursor (edit
);
3614 edit_render_keypress (edit
);
3616 retval
= spell_dialog_spell_suggest_show (edit
, match_word
, &new_word
, suggest
);
3617 edit_cursor_move (edit
, word_len
- cut_len
);
3619 if (retval
== B_ENTER
&& new_word
!= NULL
)
3625 if (mc_global
.source_codepage
>= 0 &&
3626 (mc_global
.source_codepage
!= mc_global
.display_codepage
))
3630 tmp_word
= str_convert_to_input (new_word
);
3632 new_word
= g_string_free (tmp_word
, FALSE
);
3636 for (i
= 0; i
< word_len
; i
++)
3637 edit_backspace (edit
, TRUE
);
3638 for (; *new_word
; new_word
++)
3639 edit_insert (edit
, *new_word
);
3642 else if (retval
== B_ADD_WORD
&& match_word
!= NULL
)
3643 aspell_add_to_dict (match_word
, (int) word_len
);
3646 g_array_free (suggest
, TRUE
);
3647 edit
->found_start
= 0;
3648 edit
->found_len
= 0;
3650 g_free (match_word
);
3654 /* --------------------------------------------------------------------------------------------- */
3657 edit_spellcheck_file (WEdit
* edit
)
3659 if (edit
->curs_line
> 0)
3661 edit_cursor_move (edit
, -edit
->curs1
);
3662 edit_move_to_prev_col (edit
, 0);
3663 edit_update_curs_row (edit
);
3670 c2
= edit_get_byte (edit
, edit
->curs1
);
3674 if (edit
->curs1
>= edit
->last_byte
)
3678 edit_cursor_move (edit
, 1);
3679 c2
= edit_get_byte (edit
, edit
->curs1
);
3681 while (is_break_char (c1
) || is_break_char (c2
));
3683 while (edit_suggest_current_word (edit
) != B_CANCEL
);
3686 /* --------------------------------------------------------------------------------------------- */
3689 edit_set_spell_lang (void)
3693 lang_list
= g_array_new (TRUE
, FALSE
, sizeof (char *));
3694 if (aspell_get_lang_list (lang_list
) != 0)
3698 lang
= spell_dialog_lang_list_show (lang_list
);
3700 (void) aspell_set_lang (lang
);
3702 aspell_array_clean (lang_list
);
3704 #endif /* HAVE_ASPELL */
3706 /* --------------------------------------------------------------------------------------------- */