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 Widget
*w
= WIDGET (h
);
519 return edit_add_window (h
, w
->y
+ 1, w
->x
, w
->lines
- 2, w
->cols
, vpath
, 0);
522 /* --------------------------------------------------------------------------------------------- */
525 edit_delete_column_of_text (WEdit
* edit
)
532 eval_marks (edit
, &m1
, &m2
);
533 n
= edit_move_forward (edit
, m1
, 0, m2
) + 1;
534 c
= (long) edit_move_forward3 (edit
, edit_bol (edit
, m1
), 0, m1
);
535 d
= (long) edit_move_forward3 (edit
, edit_bol (edit
, m2
), 0, m2
);
536 b
= max (min (c
, d
), min (edit
->column1
, edit
->column2
));
537 c
= max (c
, max (edit
->column1
, edit
->column2
));
541 r
= edit_bol (edit
, edit
->curs1
);
542 p
= edit_move_forward3 (edit
, r
, b
, 0);
543 q
= edit_move_forward3 (edit
, r
, c
, 0);
548 edit_cursor_move (edit
, p
- edit
->curs1
);
551 /* delete line between margins */
552 if (edit_get_byte (edit
, edit
->curs1
) != '\n')
553 edit_delete (edit
, TRUE
);
557 /* move to next line except on the last delete */
558 edit_cursor_move (edit
, edit_move_forward (edit
, edit
->curs1
, 1, 0) - edit
->curs1
);
562 /* --------------------------------------------------------------------------------------------- */
563 /** if success return 0 */
566 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
= 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
;
624 for (count
= start_mark
; count
< end_mark
; count
++)
625 edit_delete (edit
, TRUE
);
628 edit_set_markers (edit
, 0, 0, 0, 0);
629 edit
->force
|= REDRAW_PAGE
;
633 /* --------------------------------------------------------------------------------------------- */
635 * Get EOL symbol for searching.
637 * @param edit editor object
642 edit_search_get_current_end_line_char (const WEdit
* edit
)
653 /* --------------------------------------------------------------------------------------------- */
655 * Checking if search condition have BOL(^) or EOL ($) regexp special characters.
657 * @param search search object
658 * @return result of checks.
661 static edit_search_line_t
662 edit_get_search_line_type (mc_search_t
* search
)
664 edit_search_line_t search_line_type
= 0;
666 if (search
->search_type
!= MC_SEARCH_T_REGEX
)
667 return search_line_type
;
669 if (*search
->original
== '^')
670 search_line_type
|= AT_START_LINE
;
672 if (search
->original
[search
->original_len
- 1] == '$')
673 search_line_type
|= AT_END_LINE
;
674 return search_line_type
;
677 /* --------------------------------------------------------------------------------------------- */
679 * Calculating the start position of next line.
681 * @param edit editor object
682 * @param current_pos current position
683 * @param max_pos max position
684 * @param end_string_symbol end of line symbol
685 * @return start position of next line
689 edit_calculate_start_of_next_line (WEdit
* edit
, off_t current_pos
, off_t max_pos
,
690 char end_string_symbol
)
694 for (i
= current_pos
; i
< max_pos
; i
++)
697 if (edit_get_byte (edit
, i
) == end_string_symbol
)
704 /* --------------------------------------------------------------------------------------------- */
706 * Calculating the end position of previous line.
708 * @param edit editor object
709 * @param current_pos current position
710 * @param end_string_symbol end of line symbol
711 * @return end position of previous line
715 edit_calculate_end_of_previous_line (WEdit
* edit
, off_t current_pos
, char end_string_symbol
)
719 for (i
= current_pos
- 1; i
>= 0; i
--)
720 if (edit_get_byte (edit
, i
) == end_string_symbol
)
726 /* --------------------------------------------------------------------------------------------- */
728 * Calculating the start position of previous line.
730 * @param edit editor object
731 * @param current_pos current position
732 * @param end_string_symbol end of line symbol
733 * @return start position of previous line
737 edit_calculate_start_of_previous_line (WEdit
* edit
, off_t current_pos
, char end_string_symbol
)
739 current_pos
= edit_calculate_end_of_previous_line (edit
, current_pos
, end_string_symbol
);
740 current_pos
= edit_calculate_end_of_previous_line (edit
, current_pos
, end_string_symbol
);
742 return (current_pos
+ 1);
745 /* --------------------------------------------------------------------------------------------- */
747 * Calculating the start position of current line.
749 * @param edit editor object
750 * @param current_pos current position
751 * @param end_string_symbol end of line symbol
752 * @return start position of current line
756 edit_calculate_start_of_current_line (WEdit
* edit
, off_t current_pos
, char end_string_symbol
)
758 current_pos
= edit_calculate_end_of_previous_line (edit
, current_pos
, end_string_symbol
);
760 return (current_pos
+ 1);
763 /* --------------------------------------------------------------------------------------------- */
765 * Fixing (if needed) search start position if 'only in selection' option present.
767 * @param edit editor object
771 edit_search_fix_search_start_if_selection (WEdit
* edit
)
773 off_t start_mark
= 0;
776 if (!edit_search_options
.only_in_selection
)
779 if (eval_marks (edit
, &start_mark
, &end_mark
) != 0)
782 if (edit_search_options
.backwards
)
784 if (edit
->search_start
> end_mark
|| edit
->search_start
<= start_mark
)
785 edit
->search_start
= end_mark
;
789 if (edit
->search_start
< start_mark
|| edit
->search_start
>= end_mark
)
790 edit
->search_start
= start_mark
;
794 /* --------------------------------------------------------------------------------------------- */
797 editcmd_find (WEdit
* edit
, gsize
* len
)
799 off_t search_start
= edit
->search_start
;
801 off_t start_mark
= 0;
802 off_t end_mark
= edit
->last_byte
;
804 char end_string_symbol
;
806 end_string_symbol
= edit_search_get_current_end_line_char (edit
);
808 /* prepare for search */
809 if (edit_search_options
.only_in_selection
)
811 mark_res
= eval_marks (edit
, &start_mark
, &end_mark
);
814 edit
->search
->error
= MC_SEARCH_E_NOTFOUND
;
815 edit
->search
->error_str
= g_strdup (_("Search string not found"));
819 /* fix the start and the end of search block positions */
820 if ((edit
->search_line_type
& AT_START_LINE
) != 0
821 && (start_mark
!= 0 || edit_get_byte (edit
, start_mark
- 1) != end_string_symbol
))
824 edit_calculate_start_of_next_line (edit
, start_mark
, edit
->last_byte
,
827 if ((edit
->search_line_type
& AT_END_LINE
) != 0
828 && (end_mark
- 1 != edit
->last_byte
829 || edit_get_byte (edit
, end_mark
) != end_string_symbol
))
831 end_mark
= edit_calculate_end_of_previous_line (edit
, end_mark
, end_string_symbol
);
833 if (start_mark
>= end_mark
)
835 edit
->search
->error
= MC_SEARCH_E_NOTFOUND
;
836 edit
->search
->error_str
= g_strdup (_("Search string not found"));
842 if (edit_search_options
.backwards
)
843 end_mark
= max (1, edit
->curs1
) - 1;
847 if (edit_search_options
.backwards
)
849 /* backward search */
850 search_end
= end_mark
;
852 if ((edit
->search_line_type
& AT_START_LINE
) != 0)
854 edit_calculate_start_of_current_line (edit
, search_start
, end_string_symbol
);
856 while ((int) search_start
>= start_mark
)
858 if (search_end
> (off_t
) (search_start
+ edit
->search
->original_len
)
859 && mc_search_is_fixed_search_str (edit
->search
))
861 search_end
= search_start
+ edit
->search
->original_len
;
863 if (mc_search_run (edit
->search
, (void *) edit
, search_start
, search_end
, len
)
864 && edit
->search
->normal_offset
== search_start
)
870 if ((edit
->search_line_type
& AT_START_LINE
) != 0)
872 edit_calculate_start_of_previous_line (edit
, search_start
, end_string_symbol
);
876 edit
->search
->error_str
= g_strdup (_("Search string not found"));
881 if ((edit
->search_line_type
& AT_START_LINE
) != 0 && search_start
!= start_mark
)
883 edit_calculate_start_of_next_line (edit
, search_start
, end_mark
, end_string_symbol
);
884 return mc_search_run (edit
->search
, (void *) edit
, search_start
, end_mark
, len
);
889 /* --------------------------------------------------------------------------------------------- */
892 edit_replace_cmd__conv_to_display (char *str
)
897 tmp
= str_convert_to_display (str
);
901 return g_string_free (tmp
, FALSE
);
902 g_string_free (tmp
, TRUE
);
905 return g_strdup (str
);
908 /* --------------------------------------------------------------------------------------------- */
911 edit_replace_cmd__conv_to_input (char *str
)
916 tmp
= str_convert_to_input (str
);
920 return g_string_free (tmp
, FALSE
);
921 g_string_free (tmp
, TRUE
);
924 return g_strdup (str
);
927 /* --------------------------------------------------------------------------------------------- */
930 edit_do_search (WEdit
* edit
)
934 if (edit
->search
== NULL
)
935 edit
->search_start
= edit
->curs1
;
937 edit_push_undo_action (edit
, KEY_PRESS
+ edit
->start_display
);
939 if (search_create_bookmark
)
941 int found
= 0, books
= 0;
942 long l
= 0, l_last
= -1;
945 search_create_bookmark
= FALSE
;
946 book_mark_flush (edit
, -1);
948 while (mc_search_run (edit
->search
, (void *) edit
, q
, edit
->last_byte
, &len
))
951 edit
->search_start
= edit
->search
->normal_offset
;
953 l
+= edit_count_lines (edit
, q
, edit
->search
->normal_offset
);
956 book_mark_insert (edit
, l
, BOOK_MARK_FOUND_COLOR
);
960 q
= edit
->search
->normal_offset
+ 1;
964 edit_error_dialog (_("Search"), _("Search string not found"));
966 edit_cursor_move (edit
, edit
->search_start
- edit
->curs1
);
970 if (edit
->found_len
!= 0 && edit
->search_start
== edit
->found_start
+ 1
971 && edit_search_options
.backwards
)
972 edit
->search_start
--;
974 if (edit
->found_len
!= 0 && edit
->search_start
== edit
->found_start
- 1
975 && !edit_search_options
.backwards
)
976 edit
->search_start
++;
978 if (editcmd_find (edit
, &len
))
980 edit
->found_start
= edit
->search_start
= edit
->search
->normal_offset
;
981 edit
->found_len
= len
;
983 edit_cursor_move (edit
, edit
->search_start
- edit
->curs1
);
984 edit_scroll_screen_over_cursor (edit
);
985 if (edit_search_options
.backwards
)
986 edit
->search_start
--;
988 edit
->search_start
++;
992 edit
->search_start
= edit
->curs1
;
993 if (edit
->search
->error_str
!= NULL
)
994 edit_error_dialog (_("Search"), edit
->search
->error_str
);
998 edit
->force
|= REDRAW_COMPLETELY
;
999 edit_scroll_screen_over_cursor (edit
);
1002 /* --------------------------------------------------------------------------------------------- */
1005 edit_search (WEdit
* edit
)
1007 if (editcmd_dialog_search_show (edit
))
1009 edit
->search_line_type
= edit_get_search_line_type (edit
->search
);
1010 edit_search_fix_search_start_if_selection (edit
);
1011 edit_do_search (edit
);
1015 /* --------------------------------------------------------------------------------------------- */
1016 /** Return a null terminated length of text. Result must be g_free'd */
1018 static unsigned char *
1019 edit_get_block (WEdit
* edit
, off_t start
, off_t finish
, off_t
* l
)
1021 unsigned char *s
, *r
;
1023 r
= s
= g_malloc0 (finish
- start
+ 1);
1024 if (edit
->column_highlight
)
1027 /* copy from buffer, excluding chars that are out of the column 'margins' */
1028 while (start
< finish
)
1033 x
= edit_move_forward3 (edit
, edit_bol (edit
, start
), 0, start
);
1034 c
= edit_get_byte (edit
, start
);
1035 if ((x
>= edit
->column1
&& x
< edit
->column2
)
1036 || (x
>= edit
->column2
&& x
< edit
->column1
) || c
== '\n')
1046 *l
= finish
- start
;
1047 while (start
< finish
)
1048 *s
++ = edit_get_byte (edit
, start
++);
1054 /* --------------------------------------------------------------------------------------------- */
1055 /** copies a block to clipboard file */
1058 edit_save_block_to_clip_file (WEdit
* edit
, off_t start
, off_t finish
)
1063 tmp
= mc_config_get_full_path (EDIT_CLIP_FILE
);
1064 ret
= edit_save_block (edit
, tmp
, start
, finish
);
1069 /* --------------------------------------------------------------------------------------------- */
1072 pipe_mail (WEdit
* edit
, char *to
, char *subject
, char *cc
)
1077 to
= name_quote (to
, 0);
1078 subject
= name_quote (subject
, 0);
1079 cc
= name_quote (cc
, 0);
1080 s
= g_strconcat ("mail -s ", subject
, *cc
? " -c " : "", cc
, " ", to
, (char *) NULL
);
1094 for (i
= 0; i
< edit
->last_byte
; i
++)
1095 fputc (edit_get_byte (edit
, i
), p
);
1100 /* --------------------------------------------------------------------------------------------- */
1101 /** find first character of current word */
1104 edit_find_word_start (WEdit
* edit
, off_t
* word_start
, gsize
* word_len
)
1109 /* return if at begin of file */
1110 if (edit
->curs1
<= 0)
1113 c
= edit_get_byte (edit
, edit
->curs1
- 1);
1114 /* return if not at end or in word */
1115 if (is_break_char (c
))
1118 /* search start of word to be completed */
1121 /* return if at begin of file */
1122 if ((gsize
) edit
->curs1
< i
)
1126 c
= edit_get_byte (edit
, edit
->curs1
- i
);
1128 if (is_break_char (c
))
1130 /* return if word starts with digit */
1134 *word_start
= edit
->curs1
- (i
- 1); /* start found */
1143 /* --------------------------------------------------------------------------------------------- */
1145 * Get current word under cursor
1147 * @param edit editor object
1148 * @param srch mc_search object
1149 * @param word_start start word position
1151 * @return newly allocated string or NULL if no any words under cursor
1155 edit_collect_completions_get_current_word (WEdit
* edit
, mc_search_t
* srch
, off_t word_start
)
1161 if (!mc_search_run (srch
, (void *) edit
, word_start
, edit
->last_byte
, &len
))
1164 temp
= g_string_sized_new (len
);
1166 for (i
= 0; i
< (off_t
) len
; i
++)
1170 chr
= edit_get_byte (edit
, word_start
+ i
);
1172 g_string_append_c (temp
, chr
);
1175 return g_string_free (temp
, temp
->len
== 0);
1178 /* --------------------------------------------------------------------------------------------- */
1179 /** collect the possible completions */
1182 edit_collect_completions (WEdit
* edit
, off_t word_start
, gsize word_len
,
1183 char *match_expr
, struct selection
*compl, gsize
* num
)
1191 off_t last_byte
, start
= -1;
1194 srch
= mc_search_new (match_expr
, -1);
1198 if (mc_config_get_bool
1199 (mc_main_config
, CONFIG_APP_SECTION
, "editor_wordcompletion_collect_entire_file", 0))
1201 last_byte
= edit
->last_byte
;
1205 last_byte
= word_start
;
1208 srch
->search_type
= MC_SEARCH_T_REGEX
;
1209 srch
->is_case_sensitive
= TRUE
;
1210 srch
->search_fn
= edit_search_cmd_callback
;
1212 current_word
= edit_collect_completions_get_current_word (edit
, srch
, word_start
);
1214 temp
= g_string_new ("");
1216 /* collect max MAX_WORD_COMPLETIONS completions */
1217 while (mc_search_run (srch
, (void *) edit
, start
+ 1, last_byte
, &len
))
1219 g_string_set_size (temp
, 0);
1220 start
= srch
->normal_offset
;
1222 /* add matched completion if not yet added */
1223 for (i
= 0; i
< len
; i
++)
1225 skip
= edit_get_byte (edit
, start
+ i
);
1229 /* skip current word */
1230 if (start
+ (off_t
) i
== word_start
)
1233 g_string_append_c (temp
, skip
);
1239 if (current_word
!= NULL
&& strcmp (current_word
, temp
->str
) == 0)
1244 for (i
= 0; i
< *num
; i
++)
1247 ((char *) &compl[i
].text
[word_len
],
1248 (char *) &temp
->str
[word_len
], max (len
, compl[i
].len
) - word_len
) == 0)
1250 struct selection
this = compl[i
];
1251 for (++i
; i
< *num
; i
++)
1253 compl[i
- 1] = compl[i
];
1255 compl[*num
- 1] = this;
1257 break; /* skip it, already added */
1263 if (*num
== MAX_WORD_COMPLETIONS
)
1265 g_free (compl[0].text
);
1266 for (i
= 1; i
< *num
; i
++)
1268 compl[i
- 1] = compl[i
];
1275 recoded
= str_convert_to_display (temp
->str
);
1277 if (recoded
&& recoded
->len
)
1278 g_string_assign (temp
, recoded
->str
);
1280 g_string_free (recoded
, TRUE
);
1283 compl[*num
].text
= g_strndup (temp
->str
, temp
->len
);
1284 compl[*num
].len
= temp
->len
;
1288 /* note the maximal length needed for the completion dialog */
1293 mc_search_free (srch
);
1294 g_string_free (temp
, TRUE
);
1295 g_free (current_word
);
1300 /* --------------------------------------------------------------------------------------------- */
1303 edit_insert_column_of_text (WEdit
* edit
, unsigned char *data
, off_t size
, long width
,
1304 off_t
* start_pos
, off_t
* end_pos
, long *col1
, long *col2
)
1309 cursor
= edit
->curs1
;
1310 col
= edit_get_col (edit
);
1312 for (i
= 0; i
< size
; i
++)
1314 if (data
[i
] != '\n')
1315 edit_insert (edit
, data
[i
]);
1317 { /* fill in and move to next line */
1321 if (edit_get_byte (edit
, edit
->curs1
) != '\n')
1323 for (l
= width
- (edit_get_col (edit
) - col
); l
> 0; l
-= space_width
)
1324 edit_insert (edit
, ' ');
1326 for (p
= edit
->curs1
;; p
++)
1328 if (p
== edit
->last_byte
)
1330 edit_cursor_move (edit
, edit
->last_byte
- edit
->curs1
);
1331 edit_insert_ahead (edit
, '\n');
1335 if (edit_get_byte (edit
, p
) == '\n')
1341 edit_cursor_move (edit
, edit_move_forward3 (edit
, p
, col
, 0) - edit
->curs1
);
1343 for (l
= col
- edit_get_col (edit
); l
>= space_width
; l
-= space_width
)
1344 edit_insert (edit
, ' ');
1349 *col2
= col
+ width
;
1350 *start_pos
= cursor
;
1351 *end_pos
= edit
->curs1
;
1352 edit_cursor_move (edit
, cursor
- edit
->curs1
);
1355 /* --------------------------------------------------------------------------------------------- */
1358 edit_macro_comparator (gconstpointer
* macro1
, gconstpointer
* macro2
)
1360 const macros_t
*m1
= (const macros_t
*) macro1
;
1361 const macros_t
*m2
= (const macros_t
*) macro2
;
1363 return m1
->hotkey
- m2
->hotkey
;
1366 /* --------------------------------------------------------------------------------------------- */
1369 edit_macro_sort_by_hotkey (void)
1371 if (macros_list
!= NULL
&& macros_list
->len
!= 0)
1372 g_array_sort (macros_list
, (GCompareFunc
) edit_macro_comparator
);
1375 /* --------------------------------------------------------------------------------------------- */
1378 edit_get_macro (WEdit
* edit
, int hotkey
, const macros_t
** macros
, guint
* indx
)
1380 const macros_t
*array_start
= &g_array_index (macros_list
, struct macros_t
, 0);
1382 macros_t search_macro
;
1386 search_macro
.hotkey
= hotkey
;
1387 result
= bsearch (&search_macro
, macros_list
->data
, macros_list
->len
,
1388 sizeof (macros_t
), (GCompareFunc
) edit_macro_comparator
);
1390 if (result
!= NULL
&& result
->macro
!= NULL
)
1392 *indx
= (result
- array_start
);
1400 /* --------------------------------------------------------------------------------------------- */
1401 /** returns FALSE on error */
1404 edit_delete_macro (WEdit
* edit
, int hotkey
)
1406 mc_config_t
*macros_config
= NULL
;
1407 const char *section_name
= "editor";
1408 gchar
*macros_fname
;
1411 const macros_t
*macros
= NULL
;
1413 /* clear array of actions for current hotkey */
1414 while (edit_get_macro (edit
, hotkey
, ¯os
, &indx
))
1416 if (macros
->macro
!= NULL
)
1417 g_array_free (macros
->macro
, TRUE
);
1419 g_array_remove_index (macros_list
, indx
);
1420 edit_macro_sort_by_hotkey ();
1423 macros_fname
= mc_config_get_full_path (MC_MACRO_FILE
);
1424 macros_config
= mc_config_init (macros_fname
, FALSE
);
1425 g_free (macros_fname
);
1427 if (macros_config
== NULL
)
1430 skeyname
= lookup_key_by_code (hotkey
);
1431 while (mc_config_del_key (macros_config
, section_name
, skeyname
))
1434 mc_config_save_file (macros_config
, NULL
);
1435 mc_config_deinit (macros_config
);
1439 /* --------------------------------------------------------------------------------------------- */
1441 * Callback for the iteration of objects in the 'editors' array.
1442 * Toggle syntax highlighting in editor object.
1444 * @param data probably WEdit object
1445 * @param user_data unused
1449 edit_syntax_onoff_cb (void *data
, void *user_data
)
1453 if (edit_widget_is_editor ((const Widget
*) data
))
1455 WEdit
*edit
= (WEdit
*) data
;
1457 if (option_syntax_highlighting
)
1458 edit_load_syntax (edit
, NULL
, edit
->syntax_type
);
1459 edit
->force
|= REDRAW_PAGE
;
1463 /* --------------------------------------------------------------------------------------------- */
1465 * Callback for the iteration of objects in the 'editors' array.
1466 * Redraw editor object.
1468 * @param data probably WEdit object
1469 * @param user_data unused
1473 edit_redraw_page_cb (void *data
, void *user_data
)
1477 if (edit_widget_is_editor ((const Widget
*) data
))
1478 ((WEdit
*) data
)->force
|= REDRAW_PAGE
;
1481 /* --------------------------------------------------------------------------------------------- */
1482 /*** public functions ****************************************************************************/
1483 /* --------------------------------------------------------------------------------------------- */
1486 edit_refresh_cmd (void)
1493 /* --------------------------------------------------------------------------------------------- */
1495 * Toggle syntax highlighting in all editor windows.
1497 * @param h root widget for all windows
1501 edit_syntax_onoff_cmd (Dlg_head
* h
)
1503 option_syntax_highlighting
= !option_syntax_highlighting
;
1504 g_list_foreach (h
->widgets
, edit_syntax_onoff_cb
, NULL
);
1508 /* --------------------------------------------------------------------------------------------- */
1510 * Toggle tabs showing in all editor windows.
1512 * @param h root widget for all windows
1516 edit_show_tabs_tws_cmd (Dlg_head
* h
)
1518 enable_show_tabs_tws
= !enable_show_tabs_tws
;
1519 g_list_foreach (h
->widgets
, edit_redraw_page_cb
, NULL
);
1523 /* --------------------------------------------------------------------------------------------- */
1525 * Toggle right margin showing in all editor windows.
1527 * @param h root widget for all windows
1531 edit_show_margin_cmd (Dlg_head
* h
)
1533 show_right_margin
= !show_right_margin
;
1534 g_list_foreach (h
->widgets
, edit_redraw_page_cb
, NULL
);
1538 /* --------------------------------------------------------------------------------------------- */
1540 * Toggle line numbers showing in all editor windows.
1542 * @param h root widget for all windows
1546 edit_show_numbers_cmd (Dlg_head
* h
)
1548 option_line_state
= !option_line_state
;
1549 option_line_state_width
= option_line_state
? LINE_STATE_WIDTH
: 0;
1550 g_list_foreach (h
->widgets
, edit_redraw_page_cb
, NULL
);
1554 /* --------------------------------------------------------------------------------------------- */
1557 edit_save_mode_cmd (void)
1560 const int DLG_X
= 38;
1561 const int DLG_Y
= 13;
1565 const char *str
[] = {
1568 N_("&Do backups with following extension:")
1571 QuickWidget widgets
[] = {
1573 QUICK_BUTTON (18, DLG_X
, DLG_Y
- 3, DLG_Y
, N_("&Cancel"), B_CANCEL
, NULL
),
1575 QUICK_BUTTON (6, DLG_X
, DLG_Y
- 3, DLG_Y
, N_("&OK"), B_ENTER
, NULL
),
1577 QUICK_CHECKBOX (4, DLG_X
, 8, DLG_Y
, N_("Check &POSIX new line"), &option_check_nl_at_eof
),
1579 QUICK_INPUT (8, DLG_X
, 6, DLG_Y
, option_backup_ext
, 9, 0, "edit-backup-ext", &str_result
),
1581 QUICK_RADIO (4, DLG_X
, 3, DLG_Y
, 3, str
, &option_save_mode
),
1585 QuickDialog dialog
= {
1586 DLG_X
, DLG_Y
, -1, -1, N_("Edit Save Mode"),
1587 "[Edit Save Mode]", widgets
, NULL
, NULL
, FALSE
1592 size_t w0
, w1
, b_len
, w3
;
1594 #ifdef HAVE_ASSERT_H
1595 assert (option_backup_ext
!= NULL
);
1598 /* OK/Cancel buttons */
1599 w0
= str_term_width1 (_(widgets
[0].u
.button
.text
)) + 3;
1600 w1
= str_term_width1 (_(widgets
[1].u
.button
.text
)) + 5; /* default button */
1601 b_len
= w0
+ w1
+ 3;
1603 maxlen
= max (b_len
, (size_t) str_term_width1 (_(dialog
.title
)) + 2);
1606 for (i
= 0; i
< 3; i
++)
1611 w3
= max (w3
, (size_t) str_term_width1 (str
[i
]));
1614 maxlen
= max (maxlen
, w3
+ 4);
1616 dialog
.xlen
= min ((size_t) COLS
, maxlen
+ 8);
1618 widgets
[3].u
.input
.len
= w3
;
1619 widgets
[1].relative_x
= (dialog
.xlen
- b_len
) / 2;
1620 widgets
[0].relative_x
= widgets
[1].relative_x
+ w0
+ 2;
1622 for (i
= 0; i
< sizeof (widgets
) / sizeof (widgets
[0]); i
++)
1623 widgets
[i
].x_divisions
= dialog
.xlen
;
1625 if (quick_dialog (&dialog
) != B_CANCEL
)
1627 g_free (option_backup_ext
);
1628 option_backup_ext
= str_result
;
1632 /* --------------------------------------------------------------------------------------------- */
1635 edit_set_filename (WEdit
* edit
, const vfs_path_t
* name_vpath
)
1637 vfs_path_free (edit
->filename_vpath
);
1638 edit
->filename_vpath
= vfs_path_clone (name_vpath
);
1640 if (edit
->dir_vpath
== NULL
)
1641 edit
->dir_vpath
= vfs_path_clone (vfs_get_raw_current_dir ());
1644 /* --------------------------------------------------------------------------------------------- */
1645 /* Here we want to warn the users of overwriting an existing file,
1646 but only if they have made a change to the filename */
1647 /* returns TRUE on success */
1649 edit_save_as_cmd (WEdit
* edit
)
1651 /* This heads the 'Save As' dialog box */
1652 vfs_path_t
*exp_vpath
;
1654 int different_filename
= 0;
1656 if (!edit_check_newline (edit
))
1659 exp_vpath
= edit_get_save_file_as (edit
);
1660 edit_push_undo_action (edit
, KEY_PRESS
+ edit
->start_display
);
1662 if (exp_vpath
!= NULL
)
1664 if (vfs_path_len (exp_vpath
) == 0)
1670 if (vfs_path_cmp (edit
->filename_vpath
, exp_vpath
) != 0)
1675 if (mc_stat (exp_vpath
, &sb
) == 0 && !S_ISREG (sb
.st_mode
))
1677 edit_error_dialog (_("Save as"),
1679 ("Cannot save: destination is not a regular file")));
1683 different_filename
= 1;
1684 file
= mc_open (exp_vpath
, O_RDONLY
| O_BINARY
);
1688 /* the file exists */
1690 /* Overwrite the current file or cancel the operation */
1691 if (edit_query_dialog2
1693 _("A file already exists with this name"), _("&Overwrite"), _("&Cancel")))
1698 edit
->stat1
.st_mode
|= S_IWUSR
;
1700 save_lock
= lock_file (exp_vpath
);
1704 /* filenames equal, check if already locked */
1705 if (!edit
->locked
&& !edit
->delete_file
)
1706 save_lock
= lock_file (exp_vpath
);
1709 if (different_filename
)
1712 * Allow user to write into saved (under another name) file
1713 * even if original file had r/o user permissions.
1715 edit
->stat1
.st_mode
|= S_IWRITE
;
1718 rv
= edit_save_file (edit
, exp_vpath
);
1722 /* Succesful, so unlock both files */
1723 if (different_filename
)
1726 unlock_file (exp_vpath
);
1728 edit
->locked
= unlock_file (edit
->filename_vpath
);
1732 if (edit
->locked
|| save_lock
)
1733 edit
->locked
= unlock_file (edit
->filename_vpath
);
1736 edit_set_filename (edit
, exp_vpath
);
1737 if (edit
->lb
!= LB_ASIS
)
1738 edit_reload (edit
, exp_vpath
);
1740 edit
->delete_file
= 0;
1741 if (different_filename
)
1742 edit_load_syntax (edit
, NULL
, edit
->syntax_type
);
1743 vfs_path_free (exp_vpath
);
1744 edit
->force
|= REDRAW_COMPLETELY
;
1747 edit_error_dialog (_("Save as"), get_sys_error (_("Cannot save file")));
1750 /* Failed, so maintain modify (not save) lock */
1752 unlock_file (exp_vpath
);
1759 vfs_path_free (exp_vpath
);
1760 edit
->force
|= REDRAW_COMPLETELY
;
1764 /* {{{ Macro stuff starts here */
1765 /* --------------------------------------------------------------------------------------------- */
1768 edit_delete_macro_cmd (WEdit
* edit
)
1772 hotkey
= editcmd_dialog_raw_key_query (_("Delete macro"), _("Press macro hotkey:"), TRUE
);
1774 if (hotkey
!= 0 && !edit_delete_macro (edit
, hotkey
))
1775 message (D_ERROR
, _("Delete macro"), _("Macro not deleted"));
1778 /* --------------------------------------------------------------------------------------------- */
1780 /** returns FALSE on error */
1782 edit_execute_macro (WEdit
* edit
, int hotkey
)
1784 gboolean res
= FALSE
;
1788 const macros_t
*macros
;
1791 if (edit_get_macro (edit
, hotkey
, ¯os
, &indx
) &&
1792 macros
->macro
!= NULL
&& macros
->macro
->len
!= 0)
1796 edit
->force
|= REDRAW_PAGE
;
1798 for (i
= 0; i
< macros
->macro
->len
; i
++)
1800 const macro_action_t
*m_act
;
1802 m_act
= &g_array_index (macros
->macro
, struct macro_action_t
, i
);
1803 edit_execute_cmd (edit
, m_act
->action
, m_act
->ch
);
1812 /* --------------------------------------------------------------------------------------------- */
1814 /** returns FALSE on error */
1816 edit_store_macro_cmd (WEdit
* edit
)
1820 GString
*marcros_string
;
1821 mc_config_t
*macros_config
= NULL
;
1822 const char *section_name
= "editor";
1823 gchar
*macros_fname
;
1824 GArray
*macros
; /* current macro */
1826 gboolean have_macro
= FALSE
;
1827 char *skeyname
= NULL
;
1830 editcmd_dialog_raw_key_query (_("Save macro"), _("Press the macro's new hotkey:"), TRUE
);
1831 if (hotkey
== ESC_CHAR
)
1834 tmp_act
= keybind_lookup_keymap_command (editor_map
, hotkey
);
1836 /* return FALSE if try assign macro into restricted hotkeys */
1837 if (tmp_act
== CK_MacroStartRecord
1838 || tmp_act
== CK_MacroStopRecord
|| tmp_act
== CK_MacroStartStopRecord
)
1841 edit_delete_macro (edit
, hotkey
);
1843 macros_fname
= mc_config_get_full_path (MC_MACRO_FILE
);
1844 macros_config
= mc_config_init (macros_fname
, FALSE
);
1845 g_free (macros_fname
);
1847 if (macros_config
== NULL
)
1850 edit_push_undo_action (edit
, KEY_PRESS
+ edit
->start_display
);
1852 marcros_string
= g_string_sized_new (250);
1853 macros
= g_array_new (TRUE
, FALSE
, sizeof (macro_action_t
));
1855 skeyname
= lookup_key_by_code (hotkey
);
1857 for (i
= 0; i
< macro_index
; i
++)
1859 macro_action_t m_act
;
1860 const char *action_name
;
1862 action_name
= keybind_lookup_actionname (record_macro_buf
[i
].action
);
1864 if (action_name
== NULL
)
1867 m_act
.action
= record_macro_buf
[i
].action
;
1868 m_act
.ch
= record_macro_buf
[i
].ch
;
1869 g_array_append_val (macros
, m_act
);
1871 g_string_append_printf (marcros_string
, "%s:%i;", action_name
,
1872 (int) record_macro_buf
[i
].ch
);
1877 macro
.hotkey
= hotkey
;
1878 macro
.macro
= macros
;
1879 g_array_append_val (macros_list
, macro
);
1880 mc_config_set_string (macros_config
, section_name
, skeyname
, marcros_string
->str
);
1883 mc_config_del_key (macros_config
, section_name
, skeyname
);
1886 edit_macro_sort_by_hotkey ();
1888 g_string_free (marcros_string
, TRUE
);
1889 mc_config_save_file (macros_config
, NULL
);
1890 mc_config_deinit (macros_config
);
1894 /* --------------------------------------------------------------------------------------------- */
1897 edit_repeat_macro_cmd (WEdit
* edit
)
1904 f
= input_dialog (_("Repeat last commands"), _("Repeat times:"), MC_HISTORY_EDIT_REPEAT
, NULL
);
1905 if (f
== NULL
|| *f
== '\0')
1911 count_repeat
= strtol (f
, &error
, 0);
1921 edit_push_undo_action (edit
, KEY_PRESS
+ edit
->start_display
);
1922 edit
->force
|= REDRAW_PAGE
;
1924 for (j
= 0; j
< count_repeat
; j
++)
1925 for (i
= 0; i
< macro_index
; i
++)
1926 edit_execute_cmd (edit
, record_macro_buf
[i
].action
, record_macro_buf
[i
].ch
);
1927 edit_update_screen (edit
);
1931 /* --------------------------------------------------------------------------------------------- */
1932 /** return FALSE on error */
1935 edit_load_macro_cmd (WEdit
* edit
)
1937 mc_config_t
*macros_config
= NULL
;
1938 gchar
**profile_keys
, **keys
;
1939 gchar
**values
, **curr_values
;
1940 gsize len
, values_len
;
1941 const char *section_name
= "editor";
1942 gchar
*macros_fname
;
1947 macros_fname
= mc_config_get_full_path (MC_MACRO_FILE
);
1948 macros_config
= mc_config_init (macros_fname
, TRUE
);
1949 g_free (macros_fname
);
1951 if (macros_config
== NULL
)
1954 profile_keys
= keys
= mc_config_get_keys (macros_config
, section_name
, &len
);
1955 while (*profile_keys
!= NULL
)
1957 gboolean have_macro
;
1961 macros
= g_array_new (TRUE
, FALSE
, sizeof (macro_action_t
));
1963 curr_values
= values
= mc_config_get_string_list (macros_config
, section_name
,
1964 *profile_keys
, &values_len
);
1965 hotkey
= lookup_key (*profile_keys
, NULL
);
1968 while (*curr_values
!= NULL
&& *curr_values
[0] != '\0')
1970 char **macro_pair
= NULL
;
1972 macro_pair
= g_strsplit (*curr_values
, ":", 2);
1974 if (macro_pair
!= NULL
)
1976 macro_action_t m_act
;
1977 if (macro_pair
[0] == NULL
|| macro_pair
[0][0] == '\0')
1981 m_act
.action
= keybind_lookup_action (macro_pair
[0]);
1982 g_free (macro_pair
[0]);
1983 macro_pair
[0] = NULL
;
1985 if (macro_pair
[1] == NULL
|| macro_pair
[1][0] == '\0')
1989 m_act
.ch
= strtol (macro_pair
[1], NULL
, 0);
1990 g_free (macro_pair
[1]);
1991 macro_pair
[1] = NULL
;
1993 if (m_act
.action
!= 0)
1995 /* a shell command */
1996 if ((m_act
.action
/ CK_PipeBlock (0)) == 1)
1998 m_act
.action
= CK_PipeBlock (0) + (m_act
.ch
> 0 ? m_act
.ch
: 0);
2001 g_array_append_val (macros
, m_act
);
2004 g_strfreev (macro_pair
);
2011 macro
.hotkey
= hotkey
;
2012 macro
.macro
= macros
;
2013 g_array_append_val (macros_list
, macro
);
2016 g_strfreev (values
);
2019 mc_config_deinit (macros_config
);
2020 edit_macro_sort_by_hotkey ();
2024 /* }}} Macro stuff end here */
2026 /* --------------------------------------------------------------------------------------------- */
2027 /** returns TRUE on success */
2030 edit_save_confirm_cmd (WEdit
* edit
)
2034 if (edit
->filename_vpath
== NULL
)
2035 return edit_save_as_cmd (edit
);
2037 if (!edit_check_newline (edit
))
2040 if (edit_confirm_save
)
2045 filename
= vfs_path_to_str (edit
->filename_vpath
);
2046 f
= g_strdup_printf (_("Confirm save file: \"%s\""), filename
);
2048 ok
= (edit_query_dialog2 (_("Save file"), f
, _("&Save"), _("&Cancel")) == 0);
2053 return edit_save_cmd (edit
);
2056 /* --------------------------------------------------------------------------------------------- */
2058 * Ask file to edit and load it.
2060 * @return TRUE on success or cancel of ask.
2064 edit_load_cmd (Dlg_head
* h
)
2067 gboolean ret
= TRUE
; /* possible cancel */
2069 exp
= input_expand_dialog (_("Load"), _("Enter file name:"),
2070 MC_HISTORY_EDIT_LOAD
, INPUT_LAST_TEXT
);
2072 if (exp
!= NULL
&& *exp
!= '\0')
2074 vfs_path_t
*exp_vpath
;
2076 exp_vpath
= vfs_path_from_str (exp
);
2077 ret
= edit_load_file_from_filename (h
, exp_vpath
);
2078 vfs_path_free (exp_vpath
);
2086 /* --------------------------------------------------------------------------------------------- */
2088 * Load syntax file to edit.
2090 * @return TRUE on success
2094 edit_load_syntax_file (Dlg_head
* h
)
2096 vfs_path_t
*extdir_vpath
;
2098 gboolean ret
= FALSE
;
2100 if (geteuid () == 0)
2101 dir
= query_dialog (_("Syntax file edit"),
2102 _("Which syntax file you want to edit?"), D_NORMAL
, 2,
2103 _("&User"), _("&System wide"));
2106 vfs_path_build_filename (mc_global
.sysconfig_dir
, "syntax", "Syntax", (char *) NULL
);
2107 if (!exist_file (vfs_path_get_last_path_str (extdir_vpath
)))
2109 vfs_path_free (extdir_vpath
);
2111 vfs_path_build_filename (mc_global
.share_data_dir
, "syntax", "Syntax", (char *) NULL
);
2116 vfs_path_t
*user_syntax_file_vpath
;
2118 user_syntax_file_vpath
= mc_config_get_full_vpath (EDIT_SYNTAX_FILE
);
2119 check_for_default (extdir_vpath
, user_syntax_file_vpath
);
2120 ret
= edit_load_file_from_filename (h
, user_syntax_file_vpath
);
2121 vfs_path_free (user_syntax_file_vpath
);
2124 ret
= edit_load_file_from_filename (h
, extdir_vpath
);
2126 vfs_path_free (extdir_vpath
);
2131 /* --------------------------------------------------------------------------------------------- */
2133 * Load menu file to edit.
2135 * @return TRUE on success
2139 edit_load_menu_file (Dlg_head
* h
)
2141 vfs_path_t
*buffer_vpath
;
2142 vfs_path_t
*menufile_vpath
;
2146 dir
= query_dialog (_("Menu edit"),
2147 _("Which menu file do you want to edit?"), D_NORMAL
,
2148 geteuid () != 0 ? 2 : 3, _("&Local"), _("&User"), _("&System wide"));
2150 menufile_vpath
= vfs_path_build_filename (mc_global
.sysconfig_dir
, EDIT_GLOBAL_MENU
, NULL
);
2151 if (!exist_file (vfs_path_get_last_path_str (menufile_vpath
)))
2153 vfs_path_free (menufile_vpath
);
2154 menufile_vpath
= vfs_path_build_filename (mc_global
.share_data_dir
, EDIT_GLOBAL_MENU
, NULL
);
2160 buffer_vpath
= vfs_path_from_str (EDIT_LOCAL_MENU
);
2161 check_for_default (menufile_vpath
, buffer_vpath
);
2162 chmod (vfs_path_get_last_path_str (buffer_vpath
), 0600);
2166 buffer_vpath
= mc_config_get_full_vpath (EDIT_HOME_MENU
);
2167 check_for_default (menufile_vpath
, buffer_vpath
);
2171 buffer_vpath
= vfs_path_build_filename (mc_global
.sysconfig_dir
, EDIT_GLOBAL_MENU
, NULL
);
2172 if (!exist_file (vfs_path_get_last_path_str (buffer_vpath
)))
2174 vfs_path_free (buffer_vpath
);
2176 vfs_path_build_filename (mc_global
.share_data_dir
, EDIT_GLOBAL_MENU
, NULL
);
2181 vfs_path_free (menufile_vpath
);
2185 ret
= edit_load_file_from_filename (h
, buffer_vpath
);
2187 vfs_path_free (buffer_vpath
);
2188 vfs_path_free (menufile_vpath
);
2193 /* --------------------------------------------------------------------------------------------- */
2195 * Close window with opened file.
2197 * @return TRUE if file was closed.
2201 edit_close_cmd (WEdit
* edit
)
2205 ret
= (edit
!= NULL
) && edit_ok_to_exit (edit
);
2209 Dlg_head
*h
= WIDGET (edit
)->owner
;
2211 if (edit
->locked
!= 0)
2212 unlock_file (edit
->filename_vpath
);
2216 if (edit_widget_is_editor (WIDGET (h
->current
->data
)))
2217 edit
= (WEdit
*) h
->current
->data
;
2220 edit
= find_editor (h
);
2222 dlg_set_top_widget (edit
);
2227 edit
->force
|= REDRAW_COMPLETELY
;
2232 /* --------------------------------------------------------------------------------------------- */
2234 if mark2 is -1 then marking is from mark1 to the cursor.
2235 Otherwise its between the markers. This handles this.
2236 Returns 1 if no text is marked.
2240 eval_marks (WEdit
* edit
, off_t
* start_mark
, off_t
* end_mark
)
2242 if (edit
->mark1
!= edit
->mark2
)
2244 off_t start_bol
, start_eol
;
2245 off_t end_bol
, end_eol
;
2250 if (edit
->end_mark_curs
< 0)
2251 end_mark_curs
= edit
->curs1
;
2253 end_mark_curs
= edit
->end_mark_curs
;
2255 if (edit
->mark2
>= 0)
2257 *start_mark
= min (edit
->mark1
, edit
->mark2
);
2258 *end_mark
= max (edit
->mark1
, edit
->mark2
);
2262 *start_mark
= min (edit
->mark1
, end_mark_curs
);
2263 *end_mark
= max (edit
->mark1
, end_mark_curs
);
2264 edit
->column2
= edit
->curs_col
+ edit
->over_col
;
2267 if (edit
->column_highlight
2268 && (((edit
->mark1
> end_mark_curs
) && (edit
->column1
< edit
->column2
))
2269 || ((edit
->mark1
< end_mark_curs
) && (edit
->column1
> edit
->column2
))))
2271 start_bol
= edit_bol (edit
, *start_mark
);
2272 start_eol
= edit_eol (edit
, start_bol
- 1) + 1;
2273 end_bol
= edit_bol (edit
, *end_mark
);
2274 end_eol
= edit_eol (edit
, *end_mark
);
2275 col1
= min (edit
->column1
, edit
->column2
);
2276 col2
= max (edit
->column1
, edit
->column2
);
2278 diff1
= edit_move_forward3 (edit
, start_bol
, col2
, 0) -
2279 edit_move_forward3 (edit
, start_bol
, col1
, 0);
2280 diff2
= edit_move_forward3 (edit
, end_bol
, col2
, 0) -
2281 edit_move_forward3 (edit
, end_bol
, col1
, 0);
2283 *start_mark
-= diff1
;
2285 *start_mark
= max (*start_mark
, start_eol
);
2286 *end_mark
= min (*end_mark
, end_eol
);
2292 *start_mark
= *end_mark
= 0;
2293 edit
->column2
= edit
->column1
= 0;
2298 /* --------------------------------------------------------------------------------------------- */
2301 edit_insert_over (WEdit
* edit
)
2305 for (i
= 0; i
< edit
->over_col
; i
++)
2307 edit_insert (edit
, ' ');
2312 /* --------------------------------------------------------------------------------------------- */
2315 edit_insert_column_of_text_from_file (WEdit
* edit
, int file
,
2316 off_t
* start_pos
, off_t
* end_pos
, long *col1
, long *col2
)
2320 off_t blocklen
= -1, width
= 0;
2321 unsigned char *data
;
2323 cursor
= edit
->curs1
;
2324 col
= edit_get_col (edit
);
2325 data
= g_malloc0 (TEMP_BUF_LEN
);
2327 while ((blocklen
= mc_read (file
, (char *) data
, TEMP_BUF_LEN
)) > 0)
2330 for (width
= 0; width
< blocklen
; width
++)
2332 if (data
[width
] == '\n')
2335 for (i
= 0; i
< blocklen
; i
++)
2337 if (data
[i
] == '\n')
2338 { /* fill in and move to next line */
2341 if (edit_get_byte (edit
, edit
->curs1
) != '\n')
2343 l
= width
- (edit_get_col (edit
) - col
);
2346 edit_insert (edit
, ' ');
2350 for (p
= edit
->curs1
;; p
++)
2352 if (p
== edit
->last_byte
)
2354 edit_cursor_move (edit
, edit
->last_byte
- edit
->curs1
);
2355 edit_insert_ahead (edit
, '\n');
2359 if (edit_get_byte (edit
, p
) == '\n')
2365 edit_cursor_move (edit
, edit_move_forward3 (edit
, p
, col
, 0) - edit
->curs1
);
2366 l
= col
- edit_get_col (edit
);
2367 while (l
>= space_width
)
2369 edit_insert (edit
, ' ');
2374 edit_insert (edit
, data
[i
]);
2378 *col2
= col
+ width
;
2379 *start_pos
= cursor
;
2380 *end_pos
= edit
->curs1
;
2381 edit_cursor_move (edit
, cursor
- edit
->curs1
);
2387 /* --------------------------------------------------------------------------------------------- */
2390 edit_block_copy_cmd (WEdit
* edit
)
2392 off_t start_mark
, end_mark
, current
= edit
->curs1
;
2397 unsigned char *copy_buf
;
2399 edit_update_curs_col (edit
);
2400 if (eval_marks (edit
, &start_mark
, &end_mark
))
2403 copy_buf
= edit_get_block (edit
, start_mark
, end_mark
, &size
);
2405 /* all that gets pushed are deletes hence little space is used on the stack */
2407 edit_push_markers (edit
);
2409 if (edit
->column_highlight
)
2411 col_delta
= abs (edit
->column2
- edit
->column1
);
2412 edit_insert_column_of_text (edit
, copy_buf
, size
, col_delta
, &mark1
, &mark2
, &c1
, &c2
);
2417 edit_insert_ahead (edit
, copy_buf
[size
]);
2421 edit_scroll_screen_over_cursor (edit
);
2423 if (edit
->column_highlight
)
2424 edit_set_markers (edit
, edit
->curs1
, mark2
, c1
, c2
);
2425 else if (start_mark
< current
&& end_mark
> current
)
2426 edit_set_markers (edit
, start_mark
, end_mark
+ end_mark
- start_mark
, 0, 0);
2428 edit
->force
|= REDRAW_PAGE
;
2432 /* --------------------------------------------------------------------------------------------- */
2435 edit_block_move_cmd (WEdit
* edit
)
2438 unsigned char *copy_buf
= NULL
;
2439 off_t start_mark
, end_mark
;
2442 if (eval_marks (edit
, &start_mark
, &end_mark
))
2445 if (!edit
->column_highlight
&& edit
->curs1
> start_mark
&& edit
->curs1
< end_mark
)
2448 line
= edit
->curs_line
;
2449 if (edit
->mark2
< 0)
2450 edit_mark_cmd (edit
, FALSE
);
2451 edit_push_markers (edit
);
2453 if (edit
->column_highlight
)
2457 long c1
, c2
, b_width
;
2460 c1
= min (edit
->column1
, edit
->column2
);
2461 c2
= max (edit
->column1
, edit
->column2
);
2464 edit_update_curs_col (edit
);
2467 x2
= x
+ edit
->over_col
;
2469 /* do nothing when cursor inside first line of selected area */
2470 if ((edit_eol (edit
, edit
->curs1
) == edit_eol (edit
, start_mark
)) && x2
> c1
&& x2
<= c2
)
2473 if (edit
->curs1
> start_mark
&& edit
->curs1
< edit_eol (edit
, end_mark
))
2477 else if (x
> c1
&& x
<= c2
)
2480 /* save current selection into buffer */
2481 copy_buf
= edit_get_block (edit
, start_mark
, end_mark
, &size
);
2483 /* remove current selection */
2484 edit_block_delete_cmd (edit
);
2486 edit
->over_col
= max (0, edit
->over_col
- b_width
);
2487 /* calculate the cursor pos after delete block */
2488 current
= edit_move_forward3 (edit
, edit_bol (edit
, edit
->curs1
), x
, 0);
2489 edit_cursor_move (edit
, current
- edit
->curs1
);
2490 edit_scroll_screen_over_cursor (edit
);
2492 /* add TWS if need before block insertion */
2493 if (option_cursor_beyond_eol
&& edit
->over_col
> 0)
2494 edit_insert_over (edit
);
2496 edit_insert_column_of_text (edit
, copy_buf
, size
, b_width
, &mark1
, &mark2
, &c1
, &c2
);
2497 edit_set_markers (edit
, mark1
, mark2
, c1
, c2
);
2503 current
= edit
->curs1
;
2504 copy_buf
= g_malloc0 (end_mark
- start_mark
);
2505 edit_cursor_move (edit
, start_mark
- edit
->curs1
);
2506 edit_scroll_screen_over_cursor (edit
);
2508 for (count
= start_mark
; count
< end_mark
; count
++)
2509 copy_buf
[end_mark
- count
- 1] = edit_delete (edit
, TRUE
);
2511 edit_scroll_screen_over_cursor (edit
);
2512 edit_cursor_move (edit
,
2513 current
- edit
->curs1
-
2514 (((current
- edit
->curs1
) > 0) ? end_mark
- start_mark
: 0));
2515 edit_scroll_screen_over_cursor (edit
);
2516 while (count
-- > start_mark
)
2517 edit_insert_ahead (edit
, copy_buf
[end_mark
- count
- 1]);
2518 edit_set_markers (edit
, edit
->curs1
, edit
->curs1
+ end_mark
- start_mark
, 0, 0);
2521 edit_scroll_screen_over_cursor (edit
);
2523 edit
->force
|= REDRAW_PAGE
;
2526 /* --------------------------------------------------------------------------------------------- */
2527 /** returns 1 if canceelled by user */
2530 edit_block_delete_cmd (WEdit
* edit
)
2532 off_t start_mark
, end_mark
;
2533 if (eval_marks (edit
, &start_mark
, &end_mark
))
2535 edit_delete_line (edit
);
2538 return edit_block_delete (edit
);
2541 /* --------------------------------------------------------------------------------------------- */
2542 /** call with edit = 0 before shutdown to close memory leaks */
2545 edit_replace_cmd (WEdit
* edit
, int again
)
2547 /* 1 = search string, 2 = replace with */
2548 static char *saved1
= NULL
; /* saved default[123] */
2549 static char *saved2
= NULL
;
2550 char *input1
= NULL
; /* user input from the dialog */
2551 char *input2
= NULL
;
2552 GString
*input2_str
= NULL
;
2555 long times_replaced
= 0;
2556 gboolean once_found
= FALSE
;
2560 g_free (saved1
), saved1
= NULL
;
2561 g_free (saved2
), saved2
= NULL
;
2565 edit
->force
|= REDRAW_COMPLETELY
;
2567 if (again
&& !saved1
&& !saved2
)
2572 input1
= g_strdup (saved1
? saved1
: "");
2573 input2
= g_strdup (saved2
? saved2
: "");
2577 char *tmp_inp1
, *tmp_inp2
;
2579 disp1
= edit_replace_cmd__conv_to_display (saved1
? saved1
: (char *) "");
2580 disp2
= edit_replace_cmd__conv_to_display (saved2
? saved2
: (char *) "");
2582 edit_push_undo_action (edit
, KEY_PRESS
+ edit
->start_display
);
2584 editcmd_dialog_replace_show (edit
, disp1
, disp2
, &input1
, &input2
);
2589 if (input1
== NULL
|| *input1
== '\0')
2591 edit
->force
= REDRAW_COMPLETELY
;
2597 input1
= edit_replace_cmd__conv_to_input (input1
);
2598 input2
= edit_replace_cmd__conv_to_input (input2
);
2602 g_free (saved1
), saved1
= g_strdup (input1
);
2603 g_free (saved2
), saved2
= g_strdup (input2
);
2605 mc_search_free (edit
->search
);
2606 edit
->search
= NULL
;
2609 input2_str
= g_string_new (input2
);
2613 edit
->search
= mc_search_new (input1
, -1);
2614 if (edit
->search
== NULL
)
2616 edit
->search_start
= edit
->curs1
;
2619 edit
->search
->search_type
= edit_search_options
.type
;
2620 edit
->search
->is_all_charsets
= edit_search_options
.all_codepages
;
2621 edit
->search
->is_case_sensitive
= edit_search_options
.case_sens
;
2622 edit
->search
->whole_words
= edit_search_options
.whole_words
;
2623 edit
->search
->search_fn
= edit_search_cmd_callback
;
2624 edit
->search_line_type
= edit_get_search_line_type (edit
->search
);
2625 edit_search_fix_search_start_if_selection (edit
);
2628 if (edit
->found_len
&& edit
->search_start
== edit
->found_start
+ 1
2629 && edit_search_options
.backwards
)
2630 edit
->search_start
--;
2632 if (edit
->found_len
&& edit
->search_start
== edit
->found_start
- 1
2633 && !edit_search_options
.backwards
)
2634 edit
->search_start
++;
2640 if (!editcmd_find (edit
, &len
))
2642 if (!(edit
->search
->error
== MC_SEARCH_E_OK
||
2643 (once_found
&& edit
->search
->error
== MC_SEARCH_E_NOTFOUND
)))
2645 edit_error_dialog (_("Search"), edit
->search
->error_str
);
2651 edit
->search_start
= edit
->search
->normal_offset
;
2652 /*returns negative on not found or error in pattern */
2654 if ((edit
->search_start
>= 0) && (edit
->search_start
< edit
->last_byte
))
2659 edit
->found_start
= edit
->search_start
;
2660 i
= edit
->found_len
= len
;
2662 edit_cursor_move (edit
, edit
->search_start
- edit
->curs1
);
2663 edit_scroll_screen_over_cursor (edit
);
2665 if (edit
->replace_mode
== 0)
2670 l
= edit
->curs_row
- WIDGET (edit
)->lines
/ 3;
2672 edit_scroll_downward (edit
, l
);
2674 edit_scroll_upward (edit
, -l
);
2676 edit_scroll_screen_over_cursor (edit
);
2677 edit
->force
|= REDRAW_PAGE
;
2678 edit_render_keypress (edit
);
2680 /*so that undo stops at each query */
2681 edit_push_key_press (edit
);
2682 /* and prompt 2/3 down */
2683 disp1
= edit_replace_cmd__conv_to_display (saved1
);
2684 disp2
= edit_replace_cmd__conv_to_display (saved2
);
2685 prompt
= editcmd_dialog_replace_prompt_show (edit
, disp1
, disp2
, -1, -1);
2689 if (prompt
== B_REPLACE_ALL
)
2690 edit
->replace_mode
= 1;
2691 else if (prompt
== B_SKIP_REPLACE
)
2693 if (edit_search_options
.backwards
)
2694 edit
->search_start
--;
2696 edit
->search_start
++;
2697 continue; /* loop */
2699 else if (prompt
== B_CANCEL
)
2701 edit
->replace_mode
= -1;
2706 repl_str
= mc_search_prepare_replace_str (edit
->search
, input2_str
);
2708 if (edit
->search
->error
!= MC_SEARCH_E_OK
)
2710 edit_error_dialog (_("Replace"), edit
->search
->error_str
);
2711 g_string_free (repl_str
, TRUE
);
2715 /* delete then insert new */
2716 for (i
= 0; i
< len
; i
++)
2717 edit_delete (edit
, TRUE
);
2719 for (i
= 0; i
< repl_str
->len
; i
++)
2720 edit_insert (edit
, repl_str
->str
[i
]);
2722 edit
->found_len
= repl_str
->len
;
2723 g_string_free (repl_str
, TRUE
);
2726 /* so that we don't find the same string again */
2727 if (edit_search_options
.backwards
)
2729 edit
->search_start
--;
2733 edit
->search_start
+= edit
->found_len
+ (len
== 0 ? 1 : 0);
2735 if (edit
->search_start
>= edit
->last_byte
)
2739 edit_scroll_screen_over_cursor (edit
);
2743 /* try and find from right here for next search */
2744 edit
->search_start
= edit
->curs1
;
2745 edit_update_curs_col (edit
);
2747 edit
->force
|= REDRAW_PAGE
;
2748 edit_render_keypress (edit
);
2750 if (times_replaced
== 0)
2751 query_dialog (_("Replace"), _("Search string not found"), D_NORMAL
, 1, _("&OK"));
2755 while (edit
->replace_mode
>= 0);
2757 edit_scroll_screen_over_cursor (edit
);
2758 edit
->force
|= REDRAW_COMPLETELY
;
2759 edit_render_keypress (edit
);
2761 if ((edit
->replace_mode
== 1) && (times_replaced
!= 0))
2762 message (D_NORMAL
, _("Replace"), _("%ld replacements made"), times_replaced
);
2767 if (input2_str
!= NULL
)
2768 g_string_free (input2_str
, TRUE
);
2771 /* --------------------------------------------------------------------------------------------- */
2774 edit_search_cmd_callback (const void *user_data
, gsize char_offset
, int *current_char
)
2776 *current_char
= edit_get_byte ((WEdit
*) user_data
, (off_t
) char_offset
);
2777 return MC_SEARCH_CB_OK
;
2780 /* --------------------------------------------------------------------------------------------- */
2783 edit_search_cmd (WEdit
* edit
, gboolean again
)
2791 else if (edit
->last_search_string
!= NULL
)
2792 edit_do_search (edit
);
2795 /* find last search string in history */
2798 history
= history_get (MC_HISTORY_SHARED_SEARCH
);
2799 if (history
!= NULL
&& history
->data
!= NULL
)
2801 edit
->last_search_string
= (char *) history
->data
;
2802 history
->data
= NULL
;
2803 history
= g_list_first (history
);
2804 g_list_foreach (history
, (GFunc
) g_free
, NULL
);
2805 g_list_free (history
);
2807 edit
->search
= mc_search_new (edit
->last_search_string
, -1);
2808 if (edit
->search
== NULL
)
2810 /* if not... then ask for an expression */
2811 g_free (edit
->last_search_string
);
2812 edit
->last_search_string
= NULL
;
2817 edit
->search
->search_type
= edit_search_options
.type
;
2818 edit
->search
->is_all_charsets
= edit_search_options
.all_codepages
;
2819 edit
->search
->is_case_sensitive
= edit_search_options
.case_sens
;
2820 edit
->search
->whole_words
= edit_search_options
.whole_words
;
2821 edit
->search
->search_fn
= edit_search_cmd_callback
;
2822 edit
->search_line_type
= edit_get_search_line_type (edit
->search
);
2823 edit_do_search (edit
);
2828 /* if not... then ask for an expression */
2829 g_free (edit
->last_search_string
);
2830 edit
->last_search_string
= NULL
;
2837 /* --------------------------------------------------------------------------------------------- */
2839 * Check if it's OK to close the file. If there are unsaved changes, ask user.
2841 * @return TRUE if it's OK to exit, FALSE to continue editing.
2845 edit_ok_to_exit (WEdit
* edit
)
2847 char *fname
= (char *) N_("[NoName]");
2851 if (!edit
->modified
)
2854 if (edit
->filename_vpath
!= NULL
)
2855 fname
= vfs_path_to_str (edit
->filename_vpath
);
2858 fname
= g_strdup (_(fname
));
2861 fname
= g_strdup (fname
);
2864 if (!mc_global
.midnight_shutdown
)
2866 if (!edit_check_newline (edit
))
2874 msg
= g_strdup_printf (_("File %s was modified.\nSave before close?"), fname
);
2875 act
= edit_query_dialog3 (_("Close file"), msg
, _("&Yes"), _("&No"), _("&Cancel"));
2879 msg
= g_strdup_printf (_("Midnight Commander is being shut down.\nSave modified file %s?"),
2881 act
= edit_query_dialog2 (_("Quit"), msg
, _("&Yes"), _("&No"));
2894 edit_push_markers (edit
);
2895 edit_set_markers (edit
, 0, 0, 0, 0);
2896 if (!edit_save_cmd (edit
) || mc_global
.midnight_shutdown
)
2897 return mc_global
.midnight_shutdown
;
2901 case 2: /* Cancel quit */
2909 /* --------------------------------------------------------------------------------------------- */
2910 /** save block, returns TRUE on success */
2913 edit_save_block (WEdit
* edit
, const char *filename
, off_t start
, off_t finish
)
2919 vpath
= vfs_path_from_str (filename
);
2920 file
= mc_open (vpath
, O_CREAT
| O_WRONLY
| O_TRUNC
,
2921 S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IROTH
| O_BINARY
);
2922 vfs_path_free (vpath
);
2926 if (edit
->column_highlight
)
2930 r
= mc_write (file
, VERTICAL_MAGIC
, sizeof (VERTICAL_MAGIC
));
2933 unsigned char *block
, *p
;
2935 p
= block
= edit_get_block (edit
, start
, finish
, &len
);
2938 r
= mc_write (file
, p
, len
);
2953 len
= finish
- start
;
2954 buf
= g_malloc0 (TEMP_BUF_LEN
);
2955 while (start
!= finish
)
2957 end
= min (finish
, start
+ TEMP_BUF_LEN
);
2958 for (; i
< end
; i
++)
2959 buf
[i
- start
] = edit_get_byte (edit
, i
);
2960 len
-= mc_write (file
, (char *) buf
, end
- start
);
2970 /* --------------------------------------------------------------------------------------------- */
2973 edit_paste_from_history (WEdit
* edit
)
2976 edit_error_dialog (_("Error"), _("This function is not implemented"));
2979 /* --------------------------------------------------------------------------------------------- */
2982 edit_copy_to_X_buf_cmd (WEdit
* edit
)
2984 off_t start_mark
, end_mark
;
2986 if (eval_marks (edit
, &start_mark
, &end_mark
))
2988 if (!edit_save_block_to_clip_file (edit
, start_mark
, end_mark
))
2990 edit_error_dialog (_("Copy to clipboard"), get_sys_error (_("Unable to save to file")));
2993 /* try use external clipboard utility */
2994 mc_event_raise (MCEVENT_GROUP_CORE
, "clipboard_file_to_ext_clip", NULL
);
2999 /* --------------------------------------------------------------------------------------------- */
3002 edit_cut_to_X_buf_cmd (WEdit
* edit
)
3004 off_t start_mark
, end_mark
;
3006 if (eval_marks (edit
, &start_mark
, &end_mark
))
3008 if (!edit_save_block_to_clip_file (edit
, start_mark
, end_mark
))
3010 edit_error_dialog (_("Cut to clipboard"), _("Unable to save to file"));
3013 /* try use external clipboard utility */
3014 mc_event_raise (MCEVENT_GROUP_CORE
, "clipboard_file_to_ext_clip", NULL
);
3016 edit_block_delete_cmd (edit
);
3017 edit_mark_cmd (edit
, TRUE
);
3022 /* --------------------------------------------------------------------------------------------- */
3025 edit_paste_from_X_buf_cmd (WEdit
* edit
)
3030 /* try use external clipboard utility */
3031 mc_event_raise (MCEVENT_GROUP_CORE
, "clipboard_file_from_ext_clip", NULL
);
3032 tmp
= mc_config_get_full_vpath (EDIT_CLIP_FILE
);
3033 ret
= (edit_insert_file (edit
, tmp
) >= 0);
3034 vfs_path_free (tmp
);
3039 /* --------------------------------------------------------------------------------------------- */
3041 * Ask user for the line and go to that line.
3042 * Negative numbers mean line from the end (i.e. -1 is the last line).
3046 edit_goto_cmd (WEdit
* edit
)
3049 static long line
= 0; /* line as typed, saved as default */
3054 g_snprintf (s
, sizeof (s
), "%ld", line
);
3055 f
= input_dialog (_("Goto line"), _("Enter line:"), MC_HISTORY_EDIT_GOTO_LINE
, line
? s
: "");
3065 l
= strtol (f
, &error
, 0);
3074 l
= edit
->total_lines
+ l
+ 2;
3075 edit_move_display (edit
, l
- WIDGET (edit
)->lines
/ 2 - 1);
3076 edit_move_to_line (edit
, l
- 1);
3077 edit
->force
|= REDRAW_COMPLETELY
;
3082 /* --------------------------------------------------------------------------------------------- */
3083 /** Return TRUE on success */
3086 edit_save_block_cmd (WEdit
* edit
)
3088 off_t start_mark
, end_mark
;
3090 gboolean ret
= FALSE
;
3092 if (eval_marks (edit
, &start_mark
, &end_mark
))
3095 tmp
= mc_config_get_full_path (EDIT_CLIP_FILE
);
3097 input_expand_dialog (_("Save block"), _("Enter file name:"),
3098 MC_HISTORY_EDIT_SAVE_BLOCK
, tmp
);
3100 edit_push_undo_action (edit
, KEY_PRESS
+ edit
->start_display
);
3102 if (exp
!= NULL
&& *exp
!= '\0')
3104 if (edit_save_block (edit
, exp
, start_mark
, end_mark
))
3107 edit_error_dialog (_("Save block"), get_sys_error (_("Cannot save file")));
3109 edit
->force
|= REDRAW_COMPLETELY
;
3118 /* --------------------------------------------------------------------------------------------- */
3120 /** returns TRUE on success */
3122 edit_insert_file_cmd (WEdit
* edit
)
3126 gboolean ret
= FALSE
;
3128 tmp
= mc_config_get_full_path (EDIT_CLIP_FILE
);
3129 exp
= input_expand_dialog (_("Insert file"), _("Enter file name:"),
3130 MC_HISTORY_EDIT_INSERT_FILE
, tmp
);
3133 edit_push_undo_action (edit
, KEY_PRESS
+ edit
->start_display
);
3135 if (exp
!= NULL
&& *exp
!= '\0')
3137 vfs_path_t
*exp_vpath
;
3139 exp_vpath
= vfs_path_from_str (exp
);
3140 ret
= (edit_insert_file (edit
, exp_vpath
) >= 0);
3141 vfs_path_free (exp_vpath
);
3144 edit_error_dialog (_("Insert file"), get_sys_error (_("Cannot insert file")));
3149 edit
->force
|= REDRAW_COMPLETELY
;
3153 /* --------------------------------------------------------------------------------------------- */
3154 /** sorts a block, returns -1 on system fail, 1 on cancel and 0 on success */
3157 edit_sort_cmd (WEdit
* edit
)
3159 static char *old
= 0;
3160 char *exp
, *tmp
, *tmp_edit_block_name
, *tmp_edit_temp_name
;
3161 off_t start_mark
, end_mark
;
3164 if (eval_marks (edit
, &start_mark
, &end_mark
))
3166 edit_error_dialog (_("Sort block"), _("You must first highlight a block of text"));
3170 tmp
= mc_config_get_full_path (EDIT_BLOCK_FILE
);
3171 edit_save_block (edit
, tmp
, start_mark
, end_mark
);
3174 exp
= input_dialog (_("Run sort"),
3175 _("Enter sort options (see manpage) separated by whitespace:"),
3176 MC_HISTORY_EDIT_SORT
, (old
!= NULL
) ? old
: "");
3182 tmp_edit_block_name
= mc_config_get_full_path (EDIT_BLOCK_FILE
);
3183 tmp_edit_temp_name
= mc_config_get_full_path (EDIT_TEMP_FILE
);
3185 g_strconcat (" sort ", exp
, " ", tmp_edit_block_name
,
3186 " > ", tmp_edit_temp_name
, (char *) NULL
);
3187 g_free (tmp_edit_temp_name
);
3188 g_free (tmp_edit_block_name
);
3194 if (e
== -1 || e
== 127)
3196 edit_error_dialog (_("Sort"), get_sys_error (_("Cannot execute sort command")));
3201 sprintf (q
, "%d ", e
);
3202 tmp
= g_strdup_printf (_("Sort returned non-zero: %s"), q
);
3203 edit_error_dialog (_("Sort"), tmp
);
3209 edit
->force
|= REDRAW_COMPLETELY
;
3211 if (edit_block_delete_cmd (edit
))
3215 vfs_path_t
*tmp_vpath
;
3217 tmp_vpath
= mc_config_get_full_vpath (EDIT_TEMP_FILE
);
3218 edit_insert_file (edit
, tmp_vpath
);
3219 vfs_path_free (tmp_vpath
);
3224 /* --------------------------------------------------------------------------------------------- */
3226 * Ask user for a command, execute it and paste its output back to the
3231 edit_ext_cmd (WEdit
* edit
)
3233 char *exp
, *tmp
, *tmp_edit_temp_file
;
3237 input_dialog (_("Paste output of external command"),
3238 _("Enter shell command(s):"), MC_HISTORY_EDIT_PASTE_EXTCMD
, NULL
);
3243 tmp_edit_temp_file
= mc_config_get_full_path (EDIT_TEMP_FILE
);
3244 tmp
= g_strconcat (exp
, " > ", tmp_edit_temp_file
, (char *) NULL
);
3245 g_free (tmp_edit_temp_file
);
3252 edit_error_dialog (_("External command"), get_sys_error (_("Cannot execute command")));
3256 edit
->force
|= REDRAW_COMPLETELY
;
3259 vfs_path_t
*tmp_vpath
;
3261 tmp_vpath
= mc_config_get_full_vpath (EDIT_TEMP_FILE
);
3262 edit_insert_file (edit
, tmp_vpath
);
3263 vfs_path_free (tmp_vpath
);
3268 /* --------------------------------------------------------------------------------------------- */
3269 /** if block is 1, a block must be highlighted and the shell command
3270 processes it. If block is 0 the shell command is a straight system
3271 command, that just produces some output which is to be inserted */
3274 edit_block_process_cmd (WEdit
* edit
, int macro_number
)
3277 char *macros_fname
= NULL
;
3279 fname
= g_strdup_printf ("%s.%i.sh", MC_EXTMACRO_FILE
, macro_number
);
3280 macros_fname
= g_build_filename (mc_config_get_data_path (), fname
, (char *) NULL
);
3281 user_menu (edit
, macros_fname
, 0);
3283 g_free (macros_fname
);
3284 edit
->force
|= REDRAW_COMPLETELY
;
3287 /* --------------------------------------------------------------------------------------------- */
3290 edit_mail_dialog (WEdit
* edit
)
3293 char *tmail_subject
;
3296 static char *mail_cc_last
= 0;
3297 static char *mail_subject_last
= 0;
3298 static char *mail_to_last
= 0;
3300 QuickWidget quick_widgets
[] = {
3301 /* 0 */ QUICK_BUTTON (6, 10, 9, MAIL_DLG_HEIGHT
, N_("&Cancel"), B_CANCEL
, NULL
),
3302 /* 1 */ QUICK_BUTTON (2, 10, 9, MAIL_DLG_HEIGHT
, N_("&OK"), B_ENTER
, NULL
),
3303 /* 2 */ QUICK_INPUT (3, 50, 8, MAIL_DLG_HEIGHT
, "", 44, 0, "mail-dlg-input", &tmail_cc
),
3304 /* 3 */ QUICK_LABEL (3, 50, 7, MAIL_DLG_HEIGHT
, N_("Copies to")),
3305 /* 4 */ QUICK_INPUT (3, 50, 6, MAIL_DLG_HEIGHT
, "", 44, 0, "mail-dlg-input-2",
3307 /* 5 */ QUICK_LABEL (3, 50, 5, MAIL_DLG_HEIGHT
, N_("Subject")),
3308 /* 6 */ QUICK_INPUT (3, 50, 4, MAIL_DLG_HEIGHT
, "", 44, 0, "mail-dlg-input-3", &tmail_to
),
3309 /* 7 */ QUICK_LABEL (3, 50, 3, MAIL_DLG_HEIGHT
, N_("To")),
3310 /* 8 */ QUICK_LABEL (3, 50, 2, MAIL_DLG_HEIGHT
, N_("mail -s <subject> -c <cc> <to>")),
3314 QuickDialog Quick_input
= {
3315 50, MAIL_DLG_HEIGHT
, -1, -1, N_("Mail"),
3316 "[Input Line Keys]", quick_widgets
, NULL
, NULL
, FALSE
3319 quick_widgets
[2].u
.input
.text
= mail_cc_last
? mail_cc_last
: "";
3320 quick_widgets
[4].u
.input
.text
= mail_subject_last
? mail_subject_last
: "";
3321 quick_widgets
[6].u
.input
.text
= mail_to_last
? mail_to_last
: "";
3323 if (quick_dialog (&Quick_input
) != B_CANCEL
)
3325 g_free (mail_cc_last
);
3326 g_free (mail_subject_last
);
3327 g_free (mail_to_last
);
3328 mail_cc_last
= tmail_cc
;
3329 mail_subject_last
= tmail_subject
;
3330 mail_to_last
= tmail_to
;
3331 pipe_mail (edit
, mail_to_last
, mail_subject_last
, mail_cc_last
);
3336 /*******************/
3337 /* Word Completion */
3338 /*******************/
3340 /* --------------------------------------------------------------------------------------------- */
3342 * Complete current word using regular expression search
3343 * backwards beginning at the current cursor position.
3347 edit_complete_word_cmd (WEdit
* edit
)
3349 gsize i
, max_len
, word_len
= 0, num_compl
= 0;
3350 off_t word_start
= 0;
3351 unsigned char *bufpos
;
3353 struct selection
compl[MAX_WORD_COMPLETIONS
]; /* completions */
3355 /* search start of word to be completed */
3356 if (!edit_find_word_start (edit
, &word_start
, &word_len
))
3359 /* prepare match expression */
3360 bufpos
= &edit
->buffers1
[word_start
>> S_EDIT_BUF_SIZE
][word_start
& M_EDIT_BUF_SIZE
];
3362 /* match_expr = g_strdup_printf ("\\b%.*s[a-zA-Z_0-9]+", word_len, bufpos); */
3365 ("(^|\\s+|\\b)%.*s[^\\s\\.=\\+\\[\\]\\(\\)\\,\\;\\:\\\"\\'\\-\\?\\/\\|\\\\\\{\\}\\*\\&\\^\\%%\\$#@\\!]+",
3366 (int) word_len
, bufpos
);
3368 /* collect the possible completions */
3369 /* start search from begin to end of file */
3371 edit_collect_completions (edit
, word_start
, word_len
, match_expr
,
3372 (struct selection
*) &compl, &num_compl
);
3376 /* insert completed word if there is only one match */
3379 for (i
= word_len
; i
< compl[0].len
; i
++)
3380 edit_insert (edit
, *(compl[0].text
+ i
));
3382 /* more than one possible completion => ask the user */
3385 /* !!! usually only a beep is expected and when <ALT-TAB> is !!! */
3386 /* !!! pressed again the selection dialog pops up, but that !!! */
3387 /* !!! seems to require a further internal state !!! */
3390 /* let the user select the preferred completion */
3391 editcmd_dialog_completion_show (edit
, max_len
, word_len
,
3392 (struct selection
*) &compl, num_compl
);
3396 g_free (match_expr
);
3397 /* release memory before return */
3398 for (i
= 0; i
< num_compl
; i
++)
3399 g_free (compl[i
].text
);
3402 /* --------------------------------------------------------------------------------------------- */
3406 edit_select_codepage_cmd (WEdit
* edit
)
3408 if (do_select_codepage ())
3409 edit_set_codeset (edit
);
3411 edit
->force
= REDRAW_PAGE
;
3412 send_message (WIDGET (edit
), NULL
, WIDGET_DRAW
, 0, NULL
);
3416 /* --------------------------------------------------------------------------------------------- */
3419 edit_insert_literal_cmd (WEdit
* edit
)
3421 int char_for_insertion
;
3423 char_for_insertion
= editcmd_dialog_raw_key_query (_("Insert literal"),
3424 _("Press any key:"), FALSE
);
3425 edit_execute_key_command (edit
, -1, ascii_alpha_to_cntrl (char_for_insertion
));
3428 /* --------------------------------------------------------------------------------------------- */
3431 edit_begin_end_macro_cmd (WEdit
* edit
)
3433 /* edit is a pointer to the widget */
3436 unsigned long command
= macro_index
< 0 ? CK_MacroStartRecord
: CK_MacroStopRecord
;
3437 edit_execute_key_command (edit
, command
, -1);
3441 /* --------------------------------------------------------------------------------------------- */
3444 edit_begin_end_repeat_cmd (WEdit
* edit
)
3446 /* edit is a pointer to the widget */
3449 unsigned long command
= macro_index
< 0 ? CK_RepeatStartRecord
: CK_RepeatStopRecord
;
3450 edit_execute_key_command (edit
, command
, -1);
3454 /* --------------------------------------------------------------------------------------------- */
3457 edit_load_forward_cmd (WEdit
* edit
)
3460 && edit_query_dialog2 (_("Warning"),
3461 _("Current text was modified without a file save.\n"
3462 "Continue discards these changes"), _("C&ontinue"),
3465 edit
->force
|= REDRAW_COMPLETELY
;
3469 if (edit_stack_iterator
+ 1 >= MAX_HISTORY_MOVETO
)
3472 if (edit_history_moveto
[edit_stack_iterator
+ 1].line
< 1)
3475 edit_stack_iterator
++;
3476 if (edit_history_moveto
[edit_stack_iterator
].filename_vpath
!= NULL
)
3477 return edit_reload_line (edit
, edit_history_moveto
[edit_stack_iterator
].filename_vpath
,
3478 edit_history_moveto
[edit_stack_iterator
].line
);
3483 /* --------------------------------------------------------------------------------------------- */
3486 edit_load_back_cmd (WEdit
* edit
)
3489 && edit_query_dialog2 (_("Warning"),
3490 _("Current text was modified without a file save.\n"
3491 "Continue discards these changes"), _("C&ontinue"),
3494 edit
->force
|= REDRAW_COMPLETELY
;
3498 /* we are in the bottom of the stack, NO WAY! */
3499 if (edit_stack_iterator
== 0)
3502 edit_stack_iterator
--;
3503 if (edit_history_moveto
[edit_stack_iterator
].filename_vpath
!= NULL
)
3504 return edit_reload_line (edit
, edit_history_moveto
[edit_stack_iterator
].filename_vpath
,
3505 edit_history_moveto
[edit_stack_iterator
].line
);
3510 /* --------------------------------------------------------------------------------------------- */
3513 edit_get_match_keyword_cmd (WEdit
* edit
)
3515 gsize word_len
= 0, max_len
= 0;
3518 off_t word_start
= 0;
3519 unsigned char *bufpos
;
3523 char *tagfile
= NULL
;
3525 etags_hash_t def_hash
[MAX_DEFINITIONS
];
3527 for (i
= 0; i
< MAX_DEFINITIONS
; i
++)
3529 def_hash
[i
].filename
= NULL
;
3532 /* search start of word to be completed */
3533 if (!edit_find_word_start (edit
, &word_start
, &word_len
))
3536 /* prepare match expression */
3537 bufpos
= &edit
->buffers1
[word_start
>> S_EDIT_BUF_SIZE
][word_start
& M_EDIT_BUF_SIZE
];
3538 match_expr
= g_strdup_printf ("%.*s", (int) word_len
, bufpos
);
3540 ptr
= g_get_current_dir ();
3541 path
= g_strconcat (ptr
, G_DIR_SEPARATOR_S
, (char *) NULL
);
3544 /* Recursive search file 'TAGS' in parent dirs */
3547 ptr
= g_path_get_dirname (path
);
3551 tagfile
= mc_build_filename (path
, TAGS_NAME
, (char *) NULL
);
3552 if (exist_file (tagfile
))
3555 while (strcmp (path
, G_DIR_SEPARATOR_S
) != 0);
3560 etags_set_definition_hash (tagfile
, path
, match_expr
, (etags_hash_t
*) & def_hash
);
3565 max_len
= MAX_WIDTH_DEF_DIALOG
;
3569 editcmd_dialog_select_definition_show (edit
, match_expr
, max_len
, word_len
,
3570 (etags_hash_t
*) & def_hash
, num_def
);
3572 g_free (match_expr
);
3575 /* --------------------------------------------------------------------------------------------- */
3579 edit_suggest_current_word (WEdit
* edit
)
3583 off_t word_start
= 0;
3584 int retval
= B_SKIP_WORD
;
3587 /* search start of word to spell check */
3588 match_word
= edit_get_word_from_pos (edit
, edit
->curs1
, &word_start
, &word_len
, &cut_len
);
3591 if (mc_global
.source_codepage
>= 0 && (mc_global
.source_codepage
!= mc_global
.display_codepage
))
3595 tmp_word
= str_convert_to_display (match_word
);
3596 g_free (match_word
);
3597 match_word
= g_string_free (tmp_word
, FALSE
);
3600 if (!aspell_check (match_word
, (int) word_len
))
3605 suggest
= g_array_new (TRUE
, FALSE
, sizeof (char *));
3607 res
= aspell_suggest (suggest
, match_word
, (int) word_len
);
3610 char *new_word
= NULL
;
3612 edit
->found_start
= word_start
;
3613 edit
->found_len
= word_len
;
3614 edit
->force
|= REDRAW_PAGE
;
3615 edit_scroll_screen_over_cursor (edit
);
3616 edit_render_keypress (edit
);
3618 retval
= spell_dialog_spell_suggest_show (edit
, match_word
, &new_word
, suggest
);
3619 edit_cursor_move (edit
, word_len
- cut_len
);
3621 if (retval
== B_ENTER
&& new_word
!= NULL
)
3627 if (mc_global
.source_codepage
>= 0 &&
3628 (mc_global
.source_codepage
!= mc_global
.display_codepage
))
3632 tmp_word
= str_convert_to_input (new_word
);
3634 new_word
= g_string_free (tmp_word
, FALSE
);
3638 for (i
= 0; i
< word_len
; i
++)
3639 edit_backspace (edit
, TRUE
);
3640 for (; *new_word
; new_word
++)
3641 edit_insert (edit
, *new_word
);
3644 else if (retval
== B_ADD_WORD
&& match_word
!= NULL
)
3645 aspell_add_to_dict (match_word
, (int) word_len
);
3648 g_array_free (suggest
, TRUE
);
3649 edit
->found_start
= 0;
3650 edit
->found_len
= 0;
3652 g_free (match_word
);
3656 /* --------------------------------------------------------------------------------------------- */
3659 edit_spellcheck_file (WEdit
* edit
)
3661 if (edit
->curs_line
> 0)
3663 edit_cursor_move (edit
, -edit
->curs1
);
3664 edit_move_to_prev_col (edit
, 0);
3665 edit_update_curs_row (edit
);
3672 c2
= edit_get_byte (edit
, edit
->curs1
);
3676 if (edit
->curs1
>= edit
->last_byte
)
3680 edit_cursor_move (edit
, 1);
3681 c2
= edit_get_byte (edit
, edit
->curs1
);
3683 while (is_break_char (c1
) || is_break_char (c2
));
3685 while (edit_suggest_current_word (edit
) != B_CANCEL
);
3688 /* --------------------------------------------------------------------------------------------- */
3691 edit_set_spell_lang (void)
3695 lang_list
= g_array_new (TRUE
, FALSE
, sizeof (char *));
3696 if (aspell_get_lang_list (lang_list
) != 0)
3700 lang
= spell_dialog_lang_list_show (lang_list
);
3702 (void) aspell_set_lang (lang
);
3704 aspell_array_clean (lang_list
);
3706 #endif /* HAVE_ASPELL */
3708 /* --------------------------------------------------------------------------------------------- */