2 Editor high level editing commands
4 Copyright (C) 1996-2016
5 Free Software Foundation, Inc.
9 Andrew Borodin <aborodin@vmail.ru>, 2012-2014
10 Ilia Maslakov <il.smind@gmail.com>, 2012
12 This file is part of the Midnight Commander.
14 The Midnight Commander is free software: you can redistribute it
15 and/or modify it under the terms of the GNU General Public License as
16 published by the Free Software Foundation, either version 3 of the License,
17 or (at your option) any later version.
19 The Midnight Commander is distributed in the hope that it will be useful,
20 but WITHOUT ANY WARRANTY; without even the implied warranty of
21 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 GNU General Public License for more details.
24 You should have received a copy of the GNU General Public License
25 along with this program. If not, see <http://www.gnu.org/licenses/>.
29 * \brief Source: editor high level editing commands
34 /* #define PIPE_BLOCKS_SO_READ_BYTE_BY_BYTE */
45 #include <sys/types.h>
52 #include "lib/global.h"
53 #include "lib/tty/tty.h"
54 #include "lib/tty/key.h" /* XCTRL */
55 #include "lib/mcconfig.h"
57 #include "lib/strutil.h" /* utf string functions */
58 #include "lib/fileloc.h"
60 #include "lib/util.h" /* tilde_expand() */
61 #include "lib/vfs/vfs.h"
62 #include "lib/widget.h"
63 #include "lib/event.h" /* mc_event_raise() */
65 #include "lib/charsets.h"
68 #include "src/history.h"
69 #include "src/setup.h" /* option_tab_spacing */
71 #include "src/selcodepage.h"
73 #include "src/keybind-defaults.h"
74 #include "src/util.h" /* check_for_default() */
76 #include "edit-impl.h"
77 #include "editwidget.h"
78 #include "editcmd_dialogs.h"
81 #include "spell_dialogs.h"
85 /*** global variables ****************************************************************************/
87 /* search and replace: */
88 int search_create_bookmark
= FALSE
;
90 /* queries on a save */
91 int edit_confirm_save
= 1;
93 /* whether we need to drop selection on copy to buffer */
94 int option_drop_selection_on_copy
= 1;
96 /*** file scope macro definitions ****************************************************************/
100 #define TEMP_BUF_LEN 1024
102 #define INPUT_INDEX 9
104 /* thanks to Liviu Daia <daia@stoilow.imar.ro> for getting this
105 (and the above) routines to work properly - paul */
107 #define is_digit(x) ((x) >= '0' && (x) <= '9')
109 #define MAX_WORD_COMPLETIONS 100 /* in listbox */
111 /*** file scope type declarations ****************************************************************/
115 simple_status_msg_t status_msg
; /* base class */
120 } edit_search_status_msg_t
;
122 /*** file scope variables ************************************************************************/
124 static unsigned long edit_save_mode_radio_id
, edit_save_mode_input_id
;
126 /* --------------------------------------------------------------------------------------------- */
127 /*** file scope functions ************************************************************************/
128 /* --------------------------------------------------------------------------------------------- */
131 edit_search_status_update_cb (status_msg_t
* sm
)
133 simple_status_msg_t
*ssm
= SIMPLE_STATUS_MSG (sm
);
134 edit_search_status_msg_t
*esm
= (edit_search_status_msg_t
*) sm
;
135 Widget
*wd
= WIDGET (sm
->dlg
);
138 label_set_textv (ssm
->label
, _("Searching %s: %3d%%"), esm
->edit
->last_search_string
,
139 edit_buffer_calc_percent (&esm
->edit
->buffer
, esm
->offset
));
141 label_set_textv (ssm
->label
, _("Searching %s"), esm
->edit
->last_search_string
);
146 Widget
*lw
= WIDGET (ssm
->label
);
148 wd_width
= max (wd
->cols
, lw
->cols
+ 6);
149 widget_set_size (wd
, wd
->y
, wd
->x
, wd
->lines
, wd_width
);
150 widget_set_size (lw
, lw
->y
, wd
->x
+ (wd
->cols
- lw
->cols
) / 2, lw
->lines
, lw
->cols
);
154 return status_msg_common_update (sm
);
157 /* --------------------------------------------------------------------------------------------- */
160 edit_save_mode_callback (Widget
* w
, Widget
* sender
, widget_msg_t msg
, int parm
, void *data
)
165 if (sender
!= NULL
&& sender
->id
== edit_save_mode_radio_id
)
169 ww
= dlg_find_by_id (DIALOG (w
), edit_save_mode_input_id
);
170 widget_disable (ww
, RADIO (sender
)->sel
!= 2);
174 return MSG_NOT_HANDLED
;
177 return dlg_default_callback (w
, sender
, msg
, parm
, data
);
181 /* --------------------------------------------------------------------------------------------- */
183 /* If 0 (quick save) then a) create/truncate <filename> file,
184 b) save to <filename>;
185 if 1 (safe save) then a) save to <tempnam>,
186 b) rename <tempnam> to <filename>;
187 if 2 (do backups) then a) save to <tempnam>,
188 b) rename <filename> to <filename.backup_ext>,
189 c) rename <tempnam> to <filename>. */
191 /* returns 0 on error, -1 on abort */
194 edit_save_file (WEdit
* edit
, const vfs_path_t
* filename_vpath
)
199 int this_save_mode
, rv
, fd
= -1;
200 vfs_path_t
*real_filename_vpath
;
201 vfs_path_t
*savename_vpath
= NULL
;
202 const char *start_filename
;
203 const vfs_path_element_t
*vpath_element
;
206 vpath_element
= vfs_path_get_by_index (filename_vpath
, 0);
207 if (vpath_element
== NULL
)
210 start_filename
= vpath_element
->path
;
211 if (*start_filename
== '\0')
214 if (!IS_PATH_SEP (*start_filename
) && edit
->dir_vpath
!= NULL
)
215 real_filename_vpath
= vfs_path_append_vpath_new (edit
->dir_vpath
, filename_vpath
, NULL
);
217 real_filename_vpath
= vfs_path_clone (filename_vpath
);
219 this_save_mode
= option_save_mode
;
220 if (this_save_mode
!= EDIT_QUICK_SAVE
)
222 if (!vfs_file_is_local (real_filename_vpath
)
223 || (fd
= mc_open (real_filename_vpath
, O_RDONLY
| O_BINARY
)) == -1)
226 * The file does not exists yet, so no safe save or
227 * backup are necessary.
229 this_save_mode
= EDIT_QUICK_SAVE
;
235 rv
= mc_stat (real_filename_vpath
, &sb
);
238 if (this_save_mode
== EDIT_QUICK_SAVE
&& !edit
->skip_detach_prompt
&& sb
.st_nlink
> 1)
240 rv
= edit_query_dialog3 (_("Warning"),
241 _("File has hard-links. Detach before saving?"),
242 _("&Yes"), _("&No"), _("&Cancel"));
246 this_save_mode
= EDIT_SAFE_SAVE
;
249 edit
->skip_detach_prompt
= 1;
252 vfs_path_free (real_filename_vpath
);
257 /* Prevent overwriting changes from other editor sessions. */
258 if (edit
->stat1
.st_mtime
!= 0 && edit
->stat1
.st_mtime
!= sb
.st_mtime
)
260 /* The default action is "Cancel". */
263 rv
= edit_query_dialog2 (_("Warning"),
264 _("The file has been modified in the meantime. Save anyway?"),
265 _("&Yes"), _("&Cancel"));
268 vfs_path_free (real_filename_vpath
);
274 if (this_save_mode
!= EDIT_QUICK_SAVE
)
276 char *savedir
, *saveprefix
;
278 savedir
= vfs_path_tokens_get (real_filename_vpath
, 0, -1);
280 savedir
= g_strdup (".");
282 /* Token-related function never return leading slash, so we need add it manually */
283 saveprefix
= mc_build_filename (PATH_SEP_STR
, savedir
, "cooledit", NULL
);
285 fd
= mc_mkstemps (&savename_vpath
, saveprefix
, NULL
);
287 if (savename_vpath
== NULL
)
289 vfs_path_free (real_filename_vpath
);
293 * Close for now because mc_mkstemps use pure open system call
294 * to create temporary file and it needs to be reopened by
295 * VFS-aware mc_open().
300 savename_vpath
= vfs_path_clone (real_filename_vpath
);
302 (void) mc_chown (savename_vpath
, edit
->stat1
.st_uid
, edit
->stat1
.st_gid
);
303 (void) mc_chmod (savename_vpath
, edit
->stat1
.st_mode
);
305 fd
= mc_open (savename_vpath
, O_CREAT
| O_WRONLY
| O_TRUNC
| O_BINARY
, edit
->stat1
.st_mode
);
310 p
= edit_get_write_filter (savename_vpath
, real_filename_vpath
);
316 file
= (FILE *) popen (p
, "w");
320 filelen
= edit_write_stream (edit
, file
);
324 if (pclose (file
) != 0)
326 tmp
= g_strdup_printf (_("Error writing to pipe: %s"), p
);
327 edit_error_dialog (_("Error"), tmp
);
336 tmp
= g_strdup_printf (_("Cannot open pipe for writing: %s"), p
);
337 edit_error_dialog (_("Error"), get_sys_error (tmp
));
344 else if (edit
->lb
== LB_ASIS
)
345 { /* do not change line breaks */
346 filelen
= edit_buffer_write_file (&edit
->buffer
, fd
);
348 if (filelen
!= edit
->buffer
.size
)
353 if (mc_close (fd
) != 0)
355 /* Update the file information, especially the mtime. */
356 if (mc_stat (savename_vpath
, &edit
->stat1
) == -1)
360 { /* change line breaks */
362 const vfs_path_element_t
*path_element
;
366 path_element
= vfs_path_get_by_index (savename_vpath
, -1);
367 file
= (FILE *) fopen (path_element
->path
, "w");
370 filelen
= edit_write_stream (edit
, file
);
377 msg
= g_strdup_printf (_("Cannot open file for writing: %s"), path_element
->path
);
378 edit_error_dialog (_("Error"), msg
);
384 if (filelen
!= edit
->buffer
.size
)
387 if (this_save_mode
== EDIT_DO_BACKUP
)
389 char *tmp_store_filename
;
390 vfs_path_element_t
*last_vpath_element
;
391 vfs_path_t
*tmp_vpath
;
395 assert (option_backup_ext
!= NULL
);
397 /* add backup extension to the path */
398 tmp_vpath
= vfs_path_clone (real_filename_vpath
);
399 last_vpath_element
= (vfs_path_element_t
*) vfs_path_get_by_index (tmp_vpath
, -1);
400 tmp_store_filename
= last_vpath_element
->path
;
401 last_vpath_element
->path
= g_strdup_printf ("%s%s", tmp_store_filename
, option_backup_ext
);
402 g_free (tmp_store_filename
);
404 ok
= (mc_rename (real_filename_vpath
, tmp_vpath
) != -1);
405 vfs_path_free (tmp_vpath
);
410 if (this_save_mode
!= EDIT_QUICK_SAVE
)
411 if (mc_rename (savename_vpath
, real_filename_vpath
) == -1)
414 vfs_path_free (real_filename_vpath
);
415 vfs_path_free (savename_vpath
);
418 /* FIXME: Is this safe ?
419 * if (this_save_mode != EDIT_QUICK_SAVE)
420 * mc_unlink (savename);
422 vfs_path_free (real_filename_vpath
);
423 vfs_path_free (savename_vpath
);
427 /* --------------------------------------------------------------------------------------------- */
430 edit_check_newline (const edit_buffer_t
* buf
)
432 return !(option_check_nl_at_eof
&& buf
->size
> 0
433 && edit_buffer_get_byte (buf
, buf
->size
- 1) != '\n'
434 && edit_query_dialog2 (_("Warning"),
435 _("The file you are saving does not end with a newline."),
436 _("C&ontinue"), _("&Cancel")) != 0);
439 /* --------------------------------------------------------------------------------------------- */
442 edit_get_save_file_as (WEdit
* edit
)
444 static LineBreaks cur_lb
= LB_ASIS
;
446 vfs_path_t
*ret_vpath
= NULL
;
448 const char *lb_names
[LB_NAMES
] = {
449 N_("&Do not change"),
450 N_("&Unix format (LF)"),
451 N_("&Windows/DOS format (CR LF)"),
452 N_("&Macintosh format (CR)")
455 quick_widget_t quick_widgets
[] = {
457 QUICK_LABELED_INPUT (N_("Enter file name:"), input_label_above
,
458 vfs_path_as_str (edit
->filename_vpath
), "save-as",
459 &filename_res
, NULL
, FALSE
, FALSE
, INPUT_COMPLETE_FILENAMES
),
460 QUICK_SEPARATOR (TRUE
),
461 QUICK_LABEL (N_("Change line breaks to:"), NULL
),
462 QUICK_RADIO (LB_NAMES
, lb_names
, (int *) &cur_lb
, NULL
),
463 QUICK_BUTTONS_OK_CANCEL
,
468 quick_dialog_t qdlg
= {
470 N_("Save As"), "[Save File As]",
471 quick_widgets
, NULL
, NULL
474 if (quick_dialog (&qdlg
) != B_CANCEL
)
479 fname
= tilde_expand (filename_res
);
480 g_free (filename_res
);
481 ret_vpath
= vfs_path_from_str (fname
);
488 /* --------------------------------------------------------------------------------------------- */
490 /** returns TRUE on success */
493 edit_save_cmd (WEdit
* edit
)
495 int res
, save_lock
= 0;
497 if (!edit
->locked
&& !edit
->delete_file
)
498 save_lock
= lock_file (edit
->filename_vpath
);
499 res
= edit_save_file (edit
, edit
->filename_vpath
);
501 /* Maintain modify (not save) lock on failure */
502 if ((res
> 0 && edit
->locked
) || save_lock
)
503 edit
->locked
= unlock_file (edit
->filename_vpath
);
505 /* On failure try 'save as', it does locking on its own */
507 return edit_save_as_cmd (edit
);
508 edit
->force
|= REDRAW_COMPLETELY
;
511 edit
->delete_file
= 0;
518 /* --------------------------------------------------------------------------------------------- */
522 * @param h screen the owner of editor window
523 * @param vpath vfs file path
524 * @return TRUE if file content was successfully loaded, FALSE otherwise
527 static inline gboolean
528 edit_load_file_from_filename (WDialog
* h
, const vfs_path_t
* vpath
)
530 Widget
*w
= WIDGET (h
);
532 return edit_add_window (h
, w
->y
+ 1, w
->x
, w
->lines
- 2, w
->cols
, vpath
, 0);
535 /* --------------------------------------------------------------------------------------------- */
538 edit_delete_column_of_text (WEdit
* edit
)
545 eval_marks (edit
, &m1
, &m2
);
546 n
= edit_buffer_get_forward_offset (&edit
->buffer
, m1
, 0, m2
) + 1;
547 c
= (long) edit_move_forward3 (edit
, edit_buffer_get_bol (&edit
->buffer
, m1
), 0, m1
);
548 d
= (long) edit_move_forward3 (edit
, edit_buffer_get_bol (&edit
->buffer
, m2
), 0, m2
);
549 b
= max (min (c
, d
), min (edit
->column1
, edit
->column2
));
550 c
= max (c
, max (edit
->column1
, edit
->column2
));
554 r
= edit_buffer_get_current_bol (&edit
->buffer
);
555 p
= edit_move_forward3 (edit
, r
, b
, 0);
556 q
= edit_move_forward3 (edit
, r
, c
, 0);
561 edit_cursor_move (edit
, p
- edit
->buffer
.curs1
);
564 /* delete line between margins */
565 if (edit_buffer_get_current_byte (&edit
->buffer
) != '\n')
566 edit_delete (edit
, TRUE
);
570 /* move to next line except on the last delete */
571 edit_cursor_move (edit
,
572 edit_buffer_get_forward_offset (&edit
->buffer
, edit
->buffer
.curs1
, 1,
573 0) - edit
->buffer
.curs1
);
577 /* --------------------------------------------------------------------------------------------- */
578 /** if success return 0 */
581 edit_block_delete (WEdit
* edit
)
583 off_t start_mark
, end_mark
;
585 long curs_line
, c1
, c2
;
587 if (!eval_marks (edit
, &start_mark
, &end_mark
))
590 if (edit
->column_highlight
&& edit
->mark2
< 0)
591 edit_mark_cmd (edit
, FALSE
);
592 if ((end_mark
- start_mark
) > option_max_undo
/ 2)
594 /* Warning message with a query to continue or cancel the operation */
595 if (edit_query_dialog2
598 ("Block is large, you may not be able to undo this action"),
599 _("C&ontinue"), _("&Cancel")))
604 c1
= min (edit
->column1
, edit
->column2
);
605 c2
= max (edit
->column1
, edit
->column2
);
609 edit_push_markers (edit
);
611 curs_line
= edit
->buffer
.curs_line
;
613 curs_pos
= edit
->curs_col
+ edit
->over_col
;
615 /* move cursor to start of selection */
616 edit_cursor_move (edit
, start_mark
- edit
->buffer
.curs1
);
617 edit_scroll_screen_over_cursor (edit
);
619 if (start_mark
< end_mark
)
621 if (edit
->column_highlight
)
626 edit_mark_cmd (edit
, FALSE
);
627 edit_delete_column_of_text (edit
);
628 /* move cursor to the saved position */
629 edit_move_to_line (edit
, curs_line
);
630 /* calculate line width and cursor position before cut */
631 line_width
= edit_move_forward3 (edit
, edit_buffer_get_current_bol (&edit
->buffer
), 0,
632 edit_buffer_get_current_eol (&edit
->buffer
));
633 if (option_cursor_beyond_eol
&& curs_pos
> line_width
)
634 edit
->over_col
= curs_pos
- line_width
;
640 for (count
= start_mark
; count
< end_mark
; count
++)
641 edit_delete (edit
, TRUE
);
644 edit_set_markers (edit
, 0, 0, 0, 0);
645 edit
->force
|= REDRAW_PAGE
;
649 /* --------------------------------------------------------------------------------------------- */
651 * Get EOL symbol for searching.
653 * @param edit editor object
658 edit_search_get_current_end_line_char (const WEdit
* edit
)
669 /* --------------------------------------------------------------------------------------------- */
671 * Checking if search condition have BOL(^) or EOL ($) regexp special characters.
673 * @param search search object
674 * @return result of checks.
677 static edit_search_line_t
678 edit_get_search_line_type (mc_search_t
* search
)
680 edit_search_line_t search_line_type
= 0;
682 if (search
->search_type
!= MC_SEARCH_T_REGEX
)
683 return search_line_type
;
685 if (*search
->original
== '^')
686 search_line_type
|= AT_START_LINE
;
688 if (search
->original
[search
->original_len
- 1] == '$')
689 search_line_type
|= AT_END_LINE
;
690 return search_line_type
;
693 /* --------------------------------------------------------------------------------------------- */
695 * Calculating the start position of next line.
697 * @param buf editor buffer object
698 * @param current_pos current position
699 * @param max_pos max position
700 * @param end_string_symbol end of line symbol
701 * @return start position of next line
705 edit_calculate_start_of_next_line (const edit_buffer_t
* buf
, off_t current_pos
, off_t max_pos
,
706 char end_string_symbol
)
710 for (i
= current_pos
; i
< max_pos
; i
++)
713 if (edit_buffer_get_byte (buf
, i
) == end_string_symbol
)
720 /* --------------------------------------------------------------------------------------------- */
722 * Calculating the end position of previous line.
724 * @param buf editor buffer object
725 * @param current_pos current position
726 * @param end_string_symbol end of line symbol
727 * @return end position of previous line
731 edit_calculate_end_of_previous_line (const edit_buffer_t
* buf
, off_t current_pos
,
732 char end_string_symbol
)
736 for (i
= current_pos
- 1; i
>= 0; i
--)
737 if (edit_buffer_get_byte (buf
, i
) == end_string_symbol
)
743 /* --------------------------------------------------------------------------------------------- */
745 * Calculating the start position of previous line.
747 * @param buf editor buffer object
748 * @param current_pos current position
749 * @param end_string_symbol end of line symbol
750 * @return start position of previous line
754 edit_calculate_start_of_previous_line (const edit_buffer_t
* buf
, off_t current_pos
,
755 char end_string_symbol
)
757 current_pos
= edit_calculate_end_of_previous_line (buf
, current_pos
, end_string_symbol
);
758 current_pos
= edit_calculate_end_of_previous_line (buf
, current_pos
, end_string_symbol
);
760 return (current_pos
+ 1);
763 /* --------------------------------------------------------------------------------------------- */
765 * Calculating the start position of current line.
767 * @param buf editor buffer object
768 * @param current_pos current position
769 * @param end_string_symbol end of line symbol
770 * @return start position of current line
774 edit_calculate_start_of_current_line (const edit_buffer_t
* buf
, off_t current_pos
,
775 char end_string_symbol
)
777 current_pos
= edit_calculate_end_of_previous_line (buf
, current_pos
, end_string_symbol
);
779 return (current_pos
+ 1);
782 /* --------------------------------------------------------------------------------------------- */
784 * Fixing (if needed) search start position if 'only in selection' option present.
786 * @param edit editor object
790 edit_search_fix_search_start_if_selection (WEdit
* edit
)
792 off_t start_mark
= 0;
795 if (!edit_search_options
.only_in_selection
)
798 if (!eval_marks (edit
, &start_mark
, &end_mark
))
801 if (edit_search_options
.backwards
)
803 if (edit
->search_start
> end_mark
|| edit
->search_start
<= start_mark
)
804 edit
->search_start
= end_mark
;
808 if (edit
->search_start
< start_mark
|| edit
->search_start
>= end_mark
)
809 edit
->search_start
= start_mark
;
813 /* --------------------------------------------------------------------------------------------- */
816 editcmd_find (edit_search_status_msg_t
* esm
, gsize
* len
)
818 WEdit
*edit
= esm
->edit
;
819 off_t search_start
= edit
->search_start
;
821 off_t start_mark
= 0;
822 off_t end_mark
= edit
->buffer
.size
;
823 char end_string_symbol
;
825 end_string_symbol
= edit_search_get_current_end_line_char (edit
);
827 /* prepare for search */
828 if (edit_search_options
.only_in_selection
)
830 if (!eval_marks (edit
, &start_mark
, &end_mark
))
832 edit
->search
->error
= MC_SEARCH_E_NOTFOUND
;
833 edit
->search
->error_str
= g_strdup (_(STR_E_NOTFOUND
));
837 /* fix the start and the end of search block positions */
838 if ((edit
->search_line_type
& AT_START_LINE
) != 0
840 || edit_buffer_get_byte (&edit
->buffer
, start_mark
- 1) != end_string_symbol
))
843 edit_calculate_start_of_next_line (&edit
->buffer
, start_mark
, edit
->buffer
.size
,
846 if ((edit
->search_line_type
& AT_END_LINE
) != 0
847 && (end_mark
- 1 != edit
->buffer
.size
848 || edit_buffer_get_byte (&edit
->buffer
, end_mark
) != end_string_symbol
))
850 edit_calculate_end_of_previous_line (&edit
->buffer
, end_mark
, end_string_symbol
);
851 if (start_mark
>= end_mark
)
853 edit
->search
->error
= MC_SEARCH_E_NOTFOUND
;
854 edit
->search
->error_str
= g_strdup (_(STR_E_NOTFOUND
));
860 if (edit_search_options
.backwards
)
861 end_mark
= max (1, edit
->buffer
.curs1
) - 1;
865 if (edit_search_options
.backwards
)
867 /* backward search */
868 search_end
= end_mark
;
870 if ((edit
->search_line_type
& AT_START_LINE
) != 0)
872 edit_calculate_start_of_current_line (&edit
->buffer
, search_start
,
875 while (search_start
>= start_mark
)
877 if (search_end
> (off_t
) (search_start
+ edit
->search
->original_len
)
878 && mc_search_is_fixed_search_str (edit
->search
))
880 search_end
= search_start
+ edit
->search
->original_len
;
882 if (mc_search_run (edit
->search
, (void *) esm
, search_start
, search_end
, len
)
883 && edit
->search
->normal_offset
== search_start
)
889 if ((edit
->search_line_type
& AT_START_LINE
) != 0)
891 edit_calculate_start_of_previous_line (&edit
->buffer
, search_start
,
896 edit
->search
->error_str
= g_strdup (_(STR_E_NOTFOUND
));
901 if ((edit
->search_line_type
& AT_START_LINE
) != 0 && search_start
!= start_mark
)
903 edit_calculate_start_of_next_line (&edit
->buffer
, search_start
, end_mark
,
905 return mc_search_run (edit
->search
, (void *) esm
, search_start
, end_mark
, len
);
910 /* --------------------------------------------------------------------------------------------- */
913 edit_replace_cmd__conv_to_display (const char *str
)
918 tmp
= str_convert_to_display (str
);
922 return g_string_free (tmp
, FALSE
);
923 g_string_free (tmp
, TRUE
);
926 return g_strdup (str
);
929 /* --------------------------------------------------------------------------------------------- */
932 edit_replace_cmd__conv_to_input (char *str
)
937 tmp
= str_convert_to_input (str
);
941 return g_string_free (tmp
, FALSE
);
942 g_string_free (tmp
, TRUE
);
945 return g_strdup (str
);
948 /* --------------------------------------------------------------------------------------------- */
951 edit_do_search (WEdit
* edit
)
953 edit_search_status_msg_t esm
;
956 if (edit
->search
== NULL
)
957 edit
->search_start
= edit
->buffer
.curs1
;
959 edit_push_undo_action (edit
, KEY_PRESS
+ edit
->start_display
);
963 esm
.offset
= edit
->search_start
;
965 status_msg_init (STATUS_MSG (&esm
), _("Search"), 1.0, simple_status_msg_init_cb
,
966 edit_search_status_update_cb
, NULL
);
968 if (search_create_bookmark
)
970 int found
= 0, books
= 0;
971 long l
= 0, l_last
= -1;
974 search_create_bookmark
= FALSE
;
975 book_mark_flush (edit
, -1);
977 while (mc_search_run (edit
->search
, (void *) &esm
, q
, edit
->buffer
.size
, &len
))
980 edit
->search_start
= edit
->search
->normal_offset
;
982 l
+= edit_buffer_count_lines (&edit
->buffer
, q
, edit
->search
->normal_offset
);
985 book_mark_insert (edit
, l
, BOOK_MARK_FOUND_COLOR
);
989 q
= edit
->search
->normal_offset
+ 1;
993 edit_error_dialog (_("Search"), _(STR_E_NOTFOUND
));
995 edit_cursor_move (edit
, edit
->search_start
- edit
->buffer
.curs1
);
999 if (edit
->found_len
!= 0 && edit
->search_start
== edit
->found_start
+ 1
1000 && edit_search_options
.backwards
)
1001 edit
->search_start
--;
1003 if (edit
->found_len
!= 0 && edit
->search_start
== edit
->found_start
- 1
1004 && !edit_search_options
.backwards
)
1005 edit
->search_start
++;
1007 if (editcmd_find (&esm
, &len
))
1009 edit
->found_start
= edit
->search_start
= edit
->search
->normal_offset
;
1010 edit
->found_len
= len
;
1012 edit_cursor_move (edit
, edit
->search_start
- edit
->buffer
.curs1
);
1013 edit_scroll_screen_over_cursor (edit
);
1014 if (edit_search_options
.backwards
)
1015 edit
->search_start
--;
1017 edit
->search_start
++;
1021 edit
->search_start
= edit
->buffer
.curs1
;
1022 if (edit
->search
->error_str
!= NULL
)
1023 edit_query_dialog (_("Search"), edit
->search
->error_str
);
1027 status_msg_deinit (STATUS_MSG (&esm
));
1029 edit
->force
|= REDRAW_COMPLETELY
;
1030 edit_scroll_screen_over_cursor (edit
);
1033 /* --------------------------------------------------------------------------------------------- */
1036 edit_search (WEdit
* edit
)
1038 if (editcmd_dialog_search_show (edit
))
1040 edit
->search_line_type
= edit_get_search_line_type (edit
->search
);
1041 edit_search_fix_search_start_if_selection (edit
);
1042 edit_do_search (edit
);
1046 /* --------------------------------------------------------------------------------------------- */
1047 /** Return a null terminated length of text. Result must be g_free'd */
1049 static unsigned char *
1050 edit_get_block (WEdit
* edit
, off_t start
, off_t finish
, off_t
* l
)
1052 unsigned char *s
, *r
;
1054 r
= s
= g_malloc0 (finish
- start
+ 1);
1055 if (edit
->column_highlight
)
1058 /* copy from buffer, excluding chars that are out of the column 'margins' */
1059 while (start
< finish
)
1064 x
= edit_move_forward3 (edit
, edit_buffer_get_bol (&edit
->buffer
, start
), 0, start
);
1065 c
= edit_buffer_get_byte (&edit
->buffer
, start
);
1066 if ((x
>= edit
->column1
&& x
< edit
->column2
)
1067 || (x
>= edit
->column2
&& x
< edit
->column1
) || c
== '\n')
1077 *l
= finish
- start
;
1078 while (start
< finish
)
1079 *s
++ = edit_buffer_get_byte (&edit
->buffer
, start
++);
1085 /* --------------------------------------------------------------------------------------------- */
1086 /** copies a block to clipboard file */
1089 edit_save_block_to_clip_file (WEdit
* edit
, off_t start
, off_t finish
)
1094 tmp
= mc_config_get_full_path (EDIT_CLIP_FILE
);
1095 ret
= edit_save_block (edit
, tmp
, start
, finish
);
1100 /* --------------------------------------------------------------------------------------------- */
1103 pipe_mail (const edit_buffer_t
* buf
, char *to
, char *subject
, char *cc
)
1108 to
= name_quote (to
, FALSE
);
1109 subject
= name_quote (subject
, FALSE
);
1110 cc
= name_quote (cc
, FALSE
);
1111 s
= g_strconcat ("mail -s ", subject
, *cc
? " -c " : "", cc
, " ", to
, (char *) NULL
);
1126 for (i
= 0; i
< buf
->size
; i
++)
1127 fputc (edit_buffer_get_byte (buf
, i
), p
);
1132 /* --------------------------------------------------------------------------------------------- */
1133 /** find first character of current word */
1136 edit_find_word_start (const edit_buffer_t
* buf
, off_t
* word_start
, gsize
* word_len
)
1141 /* return if at begin of file */
1142 if (buf
->curs1
<= 0)
1145 c
= edit_buffer_get_previous_byte (buf
);
1146 /* return if not at end or in word */
1147 if (is_break_char (c
))
1150 /* search start of word to be completed */
1156 c
= edit_buffer_get_byte (buf
, buf
->curs1
- i
- 1);
1158 if (is_break_char (c
))
1160 /* return if word starts with digit */
1169 *word_start
= buf
->curs1
- i
; /* start found */
1170 *word_len
= (gsize
) i
;
1174 /* --------------------------------------------------------------------------------------------- */
1176 * Get current word under cursor
1178 * @param esm status message window
1179 * @param srch mc_search object
1180 * @param word_start start word position
1182 * @return newly allocated string or NULL if no any words under cursor
1186 edit_collect_completions_get_current_word (edit_search_status_msg_t
* esm
, mc_search_t
* srch
,
1189 WEdit
*edit
= esm
->edit
;
1194 if (!mc_search_run (srch
, (void *) esm
, word_start
, edit
->buffer
.size
, &len
))
1197 temp
= g_string_sized_new (len
);
1199 for (i
= 0; i
< (off_t
) len
; i
++)
1203 chr
= edit_buffer_get_byte (&edit
->buffer
, word_start
+ i
);
1205 g_string_append_c (temp
, chr
);
1208 return g_string_free (temp
, temp
->len
== 0);
1211 /* --------------------------------------------------------------------------------------------- */
1212 /** collect the possible completions */
1215 edit_collect_completions (WEdit
* edit
, off_t word_start
, gsize word_len
,
1216 char *match_expr
, GString
** compl, gsize
* num
)
1224 off_t last_byte
, start
= -1;
1226 gboolean entire_file
;
1227 edit_search_status_msg_t esm
;
1230 srch
= mc_search_new (match_expr
, cp_source
);
1232 srch
= mc_search_new (match_expr
, NULL
);
1238 mc_config_get_bool (mc_main_config
, CONFIG_APP_SECTION
,
1239 "editor_wordcompletion_collect_entire_file", 0);
1241 last_byte
= entire_file
? edit
->buffer
.size
: word_start
;
1243 srch
->search_type
= MC_SEARCH_T_REGEX
;
1244 srch
->is_case_sensitive
= TRUE
;
1245 srch
->search_fn
= edit_search_cmd_callback
;
1246 srch
->update_fn
= edit_search_update_callback
;
1250 esm
.offset
= entire_file
? 0 : word_start
;
1252 status_msg_init (STATUS_MSG (&esm
), _("Collect completions"), 1.0, simple_status_msg_init_cb
,
1253 edit_search_status_update_cb
, NULL
);
1255 current_word
= edit_collect_completions_get_current_word (&esm
, srch
, word_start
);
1257 temp
= g_string_new ("");
1259 /* collect max MAX_WORD_COMPLETIONS completions */
1260 while (mc_search_run (srch
, (void *) &esm
, start
+ 1, last_byte
, &len
))
1262 g_string_set_size (temp
, 0);
1263 start
= srch
->normal_offset
;
1265 /* add matched completion if not yet added */
1266 for (i
= 0; i
< len
; i
++)
1268 skip
= edit_buffer_get_byte (&edit
->buffer
, start
+ i
);
1272 /* skip current word */
1273 if (start
+ (off_t
) i
== word_start
)
1276 g_string_append_c (temp
, skip
);
1282 if (current_word
!= NULL
&& strcmp (current_word
, temp
->str
) == 0)
1287 for (i
= 0; i
< *num
; i
++)
1290 ((char *) &compl[i
]->str
[word_len
],
1291 (char *) &temp
->str
[word_len
], max (len
, compl[i
]->len
) - word_len
) == 0)
1293 GString
*this = compl[i
];
1294 for (++i
; i
< *num
; i
++)
1295 compl[i
- 1] = compl[i
];
1296 compl[*num
- 1] = this;
1298 break; /* skip it, already added */
1304 if (*num
== MAX_WORD_COMPLETIONS
)
1306 g_string_free (compl[0], TRUE
);
1307 for (i
= 1; i
< *num
; i
++)
1308 compl[i
- 1] = compl[i
];
1314 recoded
= str_convert_to_display (temp
->str
);
1316 if (recoded
&& recoded
->len
)
1317 g_string_assign (temp
, recoded
->str
);
1319 g_string_free (recoded
, TRUE
);
1322 compl[(*num
)++] = g_string_new_len (temp
->str
, temp
->len
);
1325 /* note the maximal length needed for the completion dialog */
1330 status_msg_deinit (STATUS_MSG (&esm
));
1331 mc_search_free (srch
);
1332 g_string_free (temp
, TRUE
);
1333 g_free (current_word
);
1338 /* --------------------------------------------------------------------------------------------- */
1341 edit_insert_column_of_text (WEdit
* edit
, unsigned char *data
, off_t size
, long width
,
1342 off_t
* start_pos
, off_t
* end_pos
, long *col1
, long *col2
)
1347 cursor
= edit
->buffer
.curs1
;
1348 col
= edit_get_col (edit
);
1350 for (i
= 0; i
< size
; i
++)
1352 if (data
[i
] != '\n')
1353 edit_insert (edit
, data
[i
]);
1355 { /* fill in and move to next line */
1359 if (edit_buffer_get_current_byte (&edit
->buffer
) != '\n')
1361 for (l
= width
- (edit_get_col (edit
) - col
); l
> 0; l
-= space_width
)
1362 edit_insert (edit
, ' ');
1364 for (p
= edit
->buffer
.curs1
;; p
++)
1366 if (p
== edit
->buffer
.size
)
1368 edit_cursor_move (edit
, edit
->buffer
.size
- edit
->buffer
.curs1
);
1369 edit_insert_ahead (edit
, '\n');
1373 if (edit_buffer_get_byte (&edit
->buffer
, p
) == '\n')
1379 edit_cursor_move (edit
, edit_move_forward3 (edit
, p
, col
, 0) - edit
->buffer
.curs1
);
1381 for (l
= col
- edit_get_col (edit
); l
>= space_width
; l
-= space_width
)
1382 edit_insert (edit
, ' ');
1387 *col2
= col
+ width
;
1388 *start_pos
= cursor
;
1389 *end_pos
= edit
->buffer
.curs1
;
1390 edit_cursor_move (edit
, cursor
- edit
->buffer
.curs1
);
1393 /* --------------------------------------------------------------------------------------------- */
1396 edit_macro_comparator (gconstpointer
* macro1
, gconstpointer
* macro2
)
1398 const macros_t
*m1
= (const macros_t
*) macro1
;
1399 const macros_t
*m2
= (const macros_t
*) macro2
;
1401 return m1
->hotkey
- m2
->hotkey
;
1404 /* --------------------------------------------------------------------------------------------- */
1407 edit_macro_sort_by_hotkey (void)
1409 if (macros_list
!= NULL
&& macros_list
->len
!= 0)
1410 g_array_sort (macros_list
, (GCompareFunc
) edit_macro_comparator
);
1413 /* --------------------------------------------------------------------------------------------- */
1416 edit_get_macro (WEdit
* edit
, int hotkey
, const macros_t
** macros
, guint
* indx
)
1418 const macros_t
*array_start
= &g_array_index (macros_list
, struct macros_t
, 0);
1420 macros_t search_macro
;
1424 search_macro
.hotkey
= hotkey
;
1425 result
= bsearch (&search_macro
, macros_list
->data
, macros_list
->len
,
1426 sizeof (macros_t
), (GCompareFunc
) edit_macro_comparator
);
1428 if (result
!= NULL
&& result
->macro
!= NULL
)
1430 *indx
= (result
- array_start
);
1438 /* --------------------------------------------------------------------------------------------- */
1439 /** returns FALSE on error */
1442 edit_delete_macro (WEdit
* edit
, int hotkey
)
1444 mc_config_t
*macros_config
= NULL
;
1445 const char *section_name
= "editor";
1446 gchar
*macros_fname
;
1449 const macros_t
*macros
= NULL
;
1451 /* clear array of actions for current hotkey */
1452 while (edit_get_macro (edit
, hotkey
, ¯os
, &indx
))
1454 if (macros
->macro
!= NULL
)
1455 g_array_free (macros
->macro
, TRUE
);
1457 g_array_remove_index (macros_list
, indx
);
1458 edit_macro_sort_by_hotkey ();
1461 macros_fname
= mc_config_get_full_path (MC_MACRO_FILE
);
1462 macros_config
= mc_config_init (macros_fname
, FALSE
);
1463 g_free (macros_fname
);
1465 if (macros_config
== NULL
)
1468 skeyname
= lookup_key_by_code (hotkey
);
1469 while (mc_config_del_key (macros_config
, section_name
, skeyname
))
1472 mc_config_save_file (macros_config
, NULL
);
1473 mc_config_deinit (macros_config
);
1477 /* --------------------------------------------------------------------------------------------- */
1479 * Callback for the iteration of objects in the 'editors' array.
1480 * Toggle syntax highlighting in editor object.
1482 * @param data probably WEdit object
1483 * @param user_data unused
1487 edit_syntax_onoff_cb (void *data
, void *user_data
)
1491 if (edit_widget_is_editor (CONST_WIDGET (data
)))
1493 WEdit
*edit
= (WEdit
*) data
;
1495 if (option_syntax_highlighting
)
1496 edit_load_syntax (edit
, NULL
, edit
->syntax_type
);
1497 edit
->force
|= REDRAW_PAGE
;
1501 /* --------------------------------------------------------------------------------------------- */
1503 * Callback for the iteration of objects in the 'editors' array.
1504 * Redraw editor object.
1506 * @param data probably WEdit object
1507 * @param user_data unused
1511 edit_redraw_page_cb (void *data
, void *user_data
)
1515 if (edit_widget_is_editor (CONST_WIDGET (data
)))
1516 ((WEdit
*) data
)->force
|= REDRAW_PAGE
;
1519 /* --------------------------------------------------------------------------------------------- */
1521 * Insert autocompleted word into editor.
1523 * @param edit editor object
1524 * @param completion word for completion
1525 * @param word_len offset from beginning for insert
1529 edit_complete_word_insert_recoded_completion (WEdit
* edit
, char *completion
, gsize word_len
)
1534 temp
= str_convert_to_input (completion
);
1536 for (completion
= temp
->str
+ word_len
; *completion
!= '\0'; completion
++)
1537 edit_insert (edit
, *completion
);
1538 g_string_free (temp
, TRUE
);
1540 for (completion
+= word_len
; *completion
!= '\0'; completion
++)
1541 edit_insert (edit
, *completion
);
1545 /* --------------------------------------------------------------------------------------------- */
1546 /*** public functions ****************************************************************************/
1547 /* --------------------------------------------------------------------------------------------- */
1550 edit_refresh_cmd (void)
1557 /* --------------------------------------------------------------------------------------------- */
1559 * Toggle syntax highlighting in all editor windows.
1561 * @param h root widget for all windows
1565 edit_syntax_onoff_cmd (WDialog
* h
)
1567 option_syntax_highlighting
= !option_syntax_highlighting
;
1568 g_list_foreach (h
->widgets
, edit_syntax_onoff_cb
, NULL
);
1572 /* --------------------------------------------------------------------------------------------- */
1574 * Toggle tabs showing in all editor windows.
1576 * @param h root widget for all windows
1580 edit_show_tabs_tws_cmd (WDialog
* h
)
1582 enable_show_tabs_tws
= !enable_show_tabs_tws
;
1583 g_list_foreach (h
->widgets
, edit_redraw_page_cb
, NULL
);
1587 /* --------------------------------------------------------------------------------------------- */
1589 * Toggle right margin showing in all editor windows.
1591 * @param h root widget for all windows
1595 edit_show_margin_cmd (WDialog
* h
)
1597 show_right_margin
= !show_right_margin
;
1598 g_list_foreach (h
->widgets
, edit_redraw_page_cb
, NULL
);
1602 /* --------------------------------------------------------------------------------------------- */
1604 * Toggle line numbers showing in all editor windows.
1606 * @param h root widget for all windows
1610 edit_show_numbers_cmd (WDialog
* h
)
1612 option_line_state
= !option_line_state
;
1613 option_line_state_width
= option_line_state
? LINE_STATE_WIDTH
: 0;
1614 g_list_foreach (h
->widgets
, edit_redraw_page_cb
, NULL
);
1618 /* --------------------------------------------------------------------------------------------- */
1621 edit_save_mode_cmd (void)
1623 char *str_result
= NULL
;
1625 const char *str
[] = {
1628 N_("&Do backups with following extension:")
1631 #ifdef HAVE_ASSERT_H
1632 assert (option_backup_ext
!= NULL
);
1638 for (i
= 0; i
< 3; i
++)
1643 quick_widget_t quick_widgets
[] = {
1645 QUICK_RADIO (3, str
, &option_save_mode
, &edit_save_mode_radio_id
),
1646 QUICK_INPUT (option_backup_ext
, "edit-backup-ext", &str_result
,
1647 &edit_save_mode_input_id
, FALSE
, FALSE
, INPUT_COMPLETE_NONE
),
1648 QUICK_SEPARATOR (TRUE
),
1649 QUICK_CHECKBOX (N_("Check &POSIX new line"), &option_check_nl_at_eof
, NULL
),
1650 QUICK_BUTTONS_OK_CANCEL
,
1655 quick_dialog_t qdlg
= {
1657 N_("Edit Save Mode"), "[Edit Save Mode]",
1658 quick_widgets
, edit_save_mode_callback
, NULL
1661 if (quick_dialog (&qdlg
) != B_CANCEL
)
1663 g_free (option_backup_ext
);
1664 option_backup_ext
= str_result
;
1669 /* --------------------------------------------------------------------------------------------- */
1672 edit_set_filename (WEdit
* edit
, const vfs_path_t
* name_vpath
)
1674 vfs_path_free (edit
->filename_vpath
);
1675 edit
->filename_vpath
= vfs_path_clone (name_vpath
);
1677 if (edit
->dir_vpath
== NULL
)
1678 edit
->dir_vpath
= vfs_path_clone (vfs_get_raw_current_dir ());
1681 /* --------------------------------------------------------------------------------------------- */
1682 /* Here we want to warn the users of overwriting an existing file,
1683 but only if they have made a change to the filename */
1684 /* returns TRUE on success */
1686 edit_save_as_cmd (WEdit
* edit
)
1688 /* This heads the 'Save As' dialog box */
1689 vfs_path_t
*exp_vpath
;
1691 int different_filename
= 0;
1693 if (!edit_check_newline (&edit
->buffer
))
1696 exp_vpath
= edit_get_save_file_as (edit
);
1697 edit_push_undo_action (edit
, KEY_PRESS
+ edit
->start_display
);
1699 if (exp_vpath
!= NULL
)
1701 if (vfs_path_len (exp_vpath
) == 0)
1707 if (!vfs_path_equal (edit
->filename_vpath
, exp_vpath
))
1712 if (mc_stat (exp_vpath
, &sb
) == 0 && !S_ISREG (sb
.st_mode
))
1714 edit_error_dialog (_("Save as"),
1716 ("Cannot save: destination is not a regular file")));
1720 different_filename
= 1;
1721 file
= mc_open (exp_vpath
, O_RDONLY
| O_BINARY
);
1725 /* the file exists */
1727 /* Overwrite the current file or cancel the operation */
1728 if (edit_query_dialog2
1730 _("A file already exists with this name"), _("&Overwrite"), _("&Cancel")))
1735 edit
->stat1
.st_mode
|= S_IWUSR
;
1737 save_lock
= lock_file (exp_vpath
);
1741 /* filenames equal, check if already locked */
1742 if (!edit
->locked
&& !edit
->delete_file
)
1743 save_lock
= lock_file (exp_vpath
);
1746 if (different_filename
)
1749 * Allow user to write into saved (under another name) file
1750 * even if original file had r/o user permissions.
1752 edit
->stat1
.st_mode
|= S_IWRITE
;
1755 rv
= edit_save_file (edit
, exp_vpath
);
1759 /* Successful, so unlock both files */
1760 if (different_filename
)
1763 unlock_file (exp_vpath
);
1765 edit
->locked
= unlock_file (edit
->filename_vpath
);
1769 if (edit
->locked
|| save_lock
)
1770 edit
->locked
= unlock_file (edit
->filename_vpath
);
1773 edit_set_filename (edit
, exp_vpath
);
1774 if (edit
->lb
!= LB_ASIS
)
1775 edit_reload (edit
, exp_vpath
);
1777 edit
->delete_file
= 0;
1778 if (different_filename
)
1779 edit_load_syntax (edit
, NULL
, edit
->syntax_type
);
1780 vfs_path_free (exp_vpath
);
1781 edit
->force
|= REDRAW_COMPLETELY
;
1784 edit_error_dialog (_("Save as"), get_sys_error (_("Cannot save file")));
1787 /* Failed, so maintain modify (not save) lock */
1789 unlock_file (exp_vpath
);
1796 vfs_path_free (exp_vpath
);
1797 edit
->force
|= REDRAW_COMPLETELY
;
1801 /* {{{ Macro stuff starts here */
1802 /* --------------------------------------------------------------------------------------------- */
1805 edit_delete_macro_cmd (WEdit
* edit
)
1809 hotkey
= editcmd_dialog_raw_key_query (_("Delete macro"), _("Press macro hotkey:"), TRUE
);
1811 if (hotkey
!= 0 && !edit_delete_macro (edit
, hotkey
))
1812 message (D_ERROR
, _("Delete macro"), _("Macro not deleted"));
1815 /* --------------------------------------------------------------------------------------------- */
1817 /** returns FALSE on error */
1819 edit_execute_macro (WEdit
* edit
, int hotkey
)
1821 gboolean res
= FALSE
;
1825 const macros_t
*macros
;
1828 if (edit_get_macro (edit
, hotkey
, ¯os
, &indx
) &&
1829 macros
->macro
!= NULL
&& macros
->macro
->len
!= 0)
1833 edit
->force
|= REDRAW_PAGE
;
1835 for (i
= 0; i
< macros
->macro
->len
; i
++)
1837 const macro_action_t
*m_act
;
1839 m_act
= &g_array_index (macros
->macro
, struct macro_action_t
, i
);
1840 edit_execute_cmd (edit
, m_act
->action
, m_act
->ch
);
1849 /* --------------------------------------------------------------------------------------------- */
1851 /** returns FALSE on error */
1853 edit_store_macro_cmd (WEdit
* edit
)
1857 GString
*marcros_string
;
1858 mc_config_t
*macros_config
= NULL
;
1859 const char *section_name
= "editor";
1860 gchar
*macros_fname
;
1861 GArray
*macros
; /* current macro */
1863 gboolean have_macro
= FALSE
;
1864 char *skeyname
= NULL
;
1867 editcmd_dialog_raw_key_query (_("Save macro"), _("Press the macro's new hotkey:"), TRUE
);
1868 if (hotkey
== ESC_CHAR
)
1871 tmp_act
= keybind_lookup_keymap_command (editor_map
, hotkey
);
1873 /* return FALSE if try assign macro into restricted hotkeys */
1874 if (tmp_act
== CK_MacroStartRecord
1875 || tmp_act
== CK_MacroStopRecord
|| tmp_act
== CK_MacroStartStopRecord
)
1878 edit_delete_macro (edit
, hotkey
);
1880 macros_fname
= mc_config_get_full_path (MC_MACRO_FILE
);
1881 macros_config
= mc_config_init (macros_fname
, FALSE
);
1882 g_free (macros_fname
);
1884 if (macros_config
== NULL
)
1887 edit_push_undo_action (edit
, KEY_PRESS
+ edit
->start_display
);
1889 marcros_string
= g_string_sized_new (250);
1890 macros
= g_array_new (TRUE
, FALSE
, sizeof (macro_action_t
));
1892 skeyname
= lookup_key_by_code (hotkey
);
1894 for (i
= 0; i
< macro_index
; i
++)
1896 macro_action_t m_act
;
1897 const char *action_name
;
1899 action_name
= keybind_lookup_actionname (record_macro_buf
[i
].action
);
1901 if (action_name
== NULL
)
1904 m_act
.action
= record_macro_buf
[i
].action
;
1905 m_act
.ch
= record_macro_buf
[i
].ch
;
1906 g_array_append_val (macros
, m_act
);
1908 g_string_append_printf (marcros_string
, "%s:%i;", action_name
,
1909 (int) record_macro_buf
[i
].ch
);
1914 macro
.hotkey
= hotkey
;
1915 macro
.macro
= macros
;
1916 g_array_append_val (macros_list
, macro
);
1917 mc_config_set_string (macros_config
, section_name
, skeyname
, marcros_string
->str
);
1920 mc_config_del_key (macros_config
, section_name
, skeyname
);
1923 edit_macro_sort_by_hotkey ();
1925 g_string_free (marcros_string
, TRUE
);
1926 mc_config_save_file (macros_config
, NULL
);
1927 mc_config_deinit (macros_config
);
1931 /* --------------------------------------------------------------------------------------------- */
1934 edit_repeat_macro_cmd (WEdit
* edit
)
1941 f
= input_dialog (_("Repeat last commands"), _("Repeat times:"), MC_HISTORY_EDIT_REPEAT
, NULL
,
1942 INPUT_COMPLETE_NONE
);
1943 if (f
== NULL
|| *f
== '\0')
1949 count_repeat
= strtol (f
, &error
, 0);
1959 edit_push_undo_action (edit
, KEY_PRESS
+ edit
->start_display
);
1960 edit
->force
|= REDRAW_PAGE
;
1962 for (j
= 0; j
< count_repeat
; j
++)
1963 for (i
= 0; i
< macro_index
; i
++)
1964 edit_execute_cmd (edit
, record_macro_buf
[i
].action
, record_macro_buf
[i
].ch
);
1965 edit_update_screen (edit
);
1969 /* --------------------------------------------------------------------------------------------- */
1970 /** return FALSE on error */
1973 edit_load_macro_cmd (WEdit
* edit
)
1975 mc_config_t
*macros_config
= NULL
;
1976 gchar
**profile_keys
, **keys
;
1977 gchar
**values
, **curr_values
;
1978 const char *section_name
= "editor";
1979 gchar
*macros_fname
;
1983 if (macros_list
== NULL
|| macros_list
->len
!= 0)
1986 macros_fname
= mc_config_get_full_path (MC_MACRO_FILE
);
1987 macros_config
= mc_config_init (macros_fname
, TRUE
);
1988 g_free (macros_fname
);
1990 if (macros_config
== NULL
)
1993 keys
= mc_config_get_keys (macros_config
, section_name
, NULL
);
1995 for (profile_keys
= keys
; *profile_keys
!= NULL
; profile_keys
++)
1998 gboolean have_macro
= FALSE
;
2002 macros
= g_array_new (TRUE
, FALSE
, sizeof (macro_action_t
));
2003 values
= mc_config_get_string_list (macros_config
, section_name
, *profile_keys
, NULL
);
2004 hotkey
= lookup_key (*profile_keys
, NULL
);
2006 for (curr_values
= values
; *curr_values
!= NULL
&& *curr_values
[0] != '\0'; curr_values
++)
2008 char **macro_pair
= NULL
;
2010 macro_pair
= g_strsplit (*curr_values
, ":", 2);
2011 if (macro_pair
!= NULL
)
2013 macro_action_t m_act
;
2014 if (macro_pair
[0] == NULL
|| macro_pair
[0][0] == '\0')
2018 m_act
.action
= keybind_lookup_action (macro_pair
[0]);
2019 MC_PTR_FREE (macro_pair
[0]);
2021 if (macro_pair
[1] == NULL
|| macro_pair
[1][0] == '\0')
2025 m_act
.ch
= strtol (macro_pair
[1], NULL
, 0);
2026 MC_PTR_FREE (macro_pair
[1]);
2028 if (m_act
.action
!= 0)
2030 /* a shell command */
2031 if ((m_act
.action
/ CK_PipeBlock (0)) == 1)
2033 m_act
.action
= CK_PipeBlock (0) + (m_act
.ch
> 0 ? m_act
.ch
: 0);
2036 g_array_append_val (macros
, m_act
);
2039 g_strfreev (macro_pair
);
2045 macro
.hotkey
= hotkey
;
2046 macro
.macro
= macros
;
2047 g_array_append_val (macros_list
, macro
);
2049 g_strfreev (values
);
2052 mc_config_deinit (macros_config
);
2053 edit_macro_sort_by_hotkey ();
2057 /* }}} Macro stuff end here */
2059 /* --------------------------------------------------------------------------------------------- */
2060 /** returns TRUE on success */
2063 edit_save_confirm_cmd (WEdit
* edit
)
2065 if (edit
->filename_vpath
== NULL
)
2066 return edit_save_as_cmd (edit
);
2068 if (!edit_check_newline (&edit
->buffer
))
2071 if (edit_confirm_save
)
2076 f
= g_strdup_printf (_("Confirm save file: \"%s\""),
2077 vfs_path_as_str (edit
->filename_vpath
));
2078 ok
= (edit_query_dialog2 (_("Save file"), f
, _("&Save"), _("&Cancel")) == 0);
2083 return edit_save_cmd (edit
);
2086 /* --------------------------------------------------------------------------------------------- */
2088 * Ask file to edit and load it.
2090 * @return TRUE on success or cancel of ask.
2094 edit_load_cmd (WDialog
* h
)
2097 gboolean ret
= TRUE
; /* possible cancel */
2099 exp
= input_expand_dialog (_("Load"), _("Enter file name:"),
2100 MC_HISTORY_EDIT_LOAD
, INPUT_LAST_TEXT
,
2101 INPUT_COMPLETE_FILENAMES
| INPUT_COMPLETE_CD
);
2103 if (exp
!= NULL
&& *exp
!= '\0')
2105 vfs_path_t
*exp_vpath
;
2107 exp_vpath
= vfs_path_from_str (exp
);
2108 ret
= edit_load_file_from_filename (h
, exp_vpath
);
2109 vfs_path_free (exp_vpath
);
2117 /* --------------------------------------------------------------------------------------------- */
2119 * Load syntax file to edit.
2121 * @return TRUE on success
2125 edit_load_syntax_file (WDialog
* h
)
2127 vfs_path_t
*extdir_vpath
;
2129 gboolean ret
= FALSE
;
2131 if (geteuid () == 0)
2132 dir
= query_dialog (_("Syntax file edit"),
2133 _("Which syntax file you want to edit?"), D_NORMAL
, 2,
2134 _("&User"), _("&System wide"));
2137 vfs_path_build_filename (mc_global
.sysconfig_dir
, "syntax", "Syntax", (char *) NULL
);
2138 if (!exist_file (vfs_path_get_last_path_str (extdir_vpath
)))
2140 vfs_path_free (extdir_vpath
);
2142 vfs_path_build_filename (mc_global
.share_data_dir
, "syntax", "Syntax", (char *) NULL
);
2147 vfs_path_t
*user_syntax_file_vpath
;
2149 user_syntax_file_vpath
= mc_config_get_full_vpath (EDIT_SYNTAX_FILE
);
2150 check_for_default (extdir_vpath
, user_syntax_file_vpath
);
2151 ret
= edit_load_file_from_filename (h
, user_syntax_file_vpath
);
2152 vfs_path_free (user_syntax_file_vpath
);
2155 ret
= edit_load_file_from_filename (h
, extdir_vpath
);
2157 vfs_path_free (extdir_vpath
);
2162 /* --------------------------------------------------------------------------------------------- */
2164 * Load menu file to edit.
2166 * @return TRUE on success
2170 edit_load_menu_file (WDialog
* h
)
2172 vfs_path_t
*buffer_vpath
;
2173 vfs_path_t
*menufile_vpath
;
2178 dir
= query_dialog (_("Menu edit"),
2179 _("Which menu file do you want to edit?"), D_NORMAL
,
2180 geteuid () != 0 ? 2 : 3, _("&Local"), _("&User"), _("&System wide"));
2182 menufile_vpath
= vfs_path_build_filename (mc_global
.sysconfig_dir
, EDIT_GLOBAL_MENU
, NULL
);
2183 if (!exist_file (vfs_path_get_last_path_str (menufile_vpath
)))
2185 vfs_path_free (menufile_vpath
);
2186 menufile_vpath
= vfs_path_build_filename (mc_global
.share_data_dir
, EDIT_GLOBAL_MENU
, NULL
);
2192 buffer_vpath
= vfs_path_from_str (EDIT_LOCAL_MENU
);
2193 check_for_default (menufile_vpath
, buffer_vpath
);
2194 chmod (vfs_path_get_last_path_str (buffer_vpath
), 0600);
2198 buffer_vpath
= mc_config_get_full_vpath (EDIT_HOME_MENU
);
2199 check_for_default (menufile_vpath
, buffer_vpath
);
2203 buffer_vpath
= vfs_path_build_filename (mc_global
.sysconfig_dir
, EDIT_GLOBAL_MENU
, NULL
);
2204 if (!exist_file (vfs_path_get_last_path_str (buffer_vpath
)))
2206 vfs_path_free (buffer_vpath
);
2208 vfs_path_build_filename (mc_global
.share_data_dir
, EDIT_GLOBAL_MENU
, NULL
);
2213 vfs_path_free (menufile_vpath
);
2217 ret
= edit_load_file_from_filename (h
, buffer_vpath
);
2219 vfs_path_free (buffer_vpath
);
2220 vfs_path_free (menufile_vpath
);
2225 /* --------------------------------------------------------------------------------------------- */
2227 * Close window with opened file.
2229 * @return TRUE if file was closed.
2233 edit_close_cmd (WEdit
* edit
)
2237 ret
= (edit
!= NULL
) && edit_ok_to_exit (edit
);
2241 WDialog
*h
= WIDGET (edit
)->owner
;
2243 if (edit
->locked
!= 0)
2244 unlock_file (edit
->filename_vpath
);
2248 if (edit_widget_is_editor (CONST_WIDGET (h
->current
->data
)))
2249 edit
= (WEdit
*) h
->current
->data
;
2252 edit
= find_editor (h
);
2254 dlg_set_top_widget (edit
);
2259 edit
->force
|= REDRAW_COMPLETELY
;
2264 /* --------------------------------------------------------------------------------------------- */
2266 if mark2 is -1 then marking is from mark1 to the cursor.
2267 Otherwise its between the markers. This handles this.
2268 Returns FALSE if no text is marked.
2272 eval_marks (WEdit
* edit
, off_t
* start_mark
, off_t
* end_mark
)
2276 if (edit
->mark1
== edit
->mark2
)
2278 *start_mark
= *end_mark
= 0;
2279 edit
->column2
= edit
->column1
= 0;
2283 if (edit
->end_mark_curs
< 0)
2284 end_mark_curs
= edit
->buffer
.curs1
;
2286 end_mark_curs
= edit
->end_mark_curs
;
2288 if (edit
->mark2
>= 0)
2290 *start_mark
= min (edit
->mark1
, edit
->mark2
);
2291 *end_mark
= max (edit
->mark1
, edit
->mark2
);
2295 *start_mark
= min (edit
->mark1
, end_mark_curs
);
2296 *end_mark
= max (edit
->mark1
, end_mark_curs
);
2297 edit
->column2
= edit
->curs_col
+ edit
->over_col
;
2300 if (edit
->column_highlight
2301 && ((edit
->mark1
> end_mark_curs
&& edit
->column1
< edit
->column2
)
2302 || (edit
->mark1
< end_mark_curs
&& edit
->column1
> edit
->column2
)))
2304 off_t start_bol
, start_eol
;
2305 off_t end_bol
, end_eol
;
2309 start_bol
= edit_buffer_get_bol (&edit
->buffer
, *start_mark
);
2310 start_eol
= edit_buffer_get_eol (&edit
->buffer
, start_bol
- 1) + 1;
2311 end_bol
= edit_buffer_get_bol (&edit
->buffer
, *end_mark
);
2312 end_eol
= edit_buffer_get_eol (&edit
->buffer
, *end_mark
);
2313 col1
= min (edit
->column1
, edit
->column2
);
2314 col2
= max (edit
->column1
, edit
->column2
);
2316 diff1
= edit_move_forward3 (edit
, start_bol
, col2
, 0) -
2317 edit_move_forward3 (edit
, start_bol
, col1
, 0);
2318 diff2
= edit_move_forward3 (edit
, end_bol
, col2
, 0) -
2319 edit_move_forward3 (edit
, end_bol
, col1
, 0);
2321 *start_mark
-= diff1
;
2323 *start_mark
= max (*start_mark
, start_eol
);
2324 *end_mark
= min (*end_mark
, end_eol
);
2329 /* --------------------------------------------------------------------------------------------- */
2332 edit_block_copy_cmd (WEdit
* edit
)
2334 off_t start_mark
, end_mark
, current
= edit
->buffer
.curs1
;
2335 off_t mark1
= 0, mark2
= 0;
2336 long c1
= 0, c2
= 0;
2338 unsigned char *copy_buf
;
2340 edit_update_curs_col (edit
);
2341 if (!eval_marks (edit
, &start_mark
, &end_mark
))
2344 copy_buf
= edit_get_block (edit
, start_mark
, end_mark
, &size
);
2346 /* all that gets pushed are deletes hence little space is used on the stack */
2348 edit_push_markers (edit
);
2350 if (edit
->column_highlight
)
2354 col_delta
= labs (edit
->column2
- edit
->column1
);
2355 edit_insert_column_of_text (edit
, copy_buf
, size
, col_delta
, &mark1
, &mark2
, &c1
, &c2
);
2359 int size_orig
= size
;
2362 edit_insert_ahead (edit
, copy_buf
[size
]);
2364 /* Place cursor at the end of text selection */
2365 if (option_cursor_after_inserted_block
)
2366 edit_cursor_move (edit
, size_orig
);
2370 edit_scroll_screen_over_cursor (edit
);
2372 if (edit
->column_highlight
)
2373 edit_set_markers (edit
, edit
->buffer
.curs1
, mark2
, c1
, c2
);
2374 else if (start_mark
< current
&& end_mark
> current
)
2375 edit_set_markers (edit
, start_mark
, end_mark
+ end_mark
- start_mark
, 0, 0);
2377 edit
->force
|= REDRAW_PAGE
;
2381 /* --------------------------------------------------------------------------------------------- */
2384 edit_block_move_cmd (WEdit
* edit
)
2387 unsigned char *copy_buf
= NULL
;
2388 off_t start_mark
, end_mark
;
2390 if (!eval_marks (edit
, &start_mark
, &end_mark
))
2393 if (!edit
->column_highlight
&& edit
->buffer
.curs1
> start_mark
&& edit
->buffer
.curs1
< end_mark
)
2396 if (edit
->mark2
< 0)
2397 edit_mark_cmd (edit
, FALSE
);
2398 edit_push_markers (edit
);
2400 if (edit
->column_highlight
)
2404 long c1
, c2
, b_width
;
2407 c1
= min (edit
->column1
, edit
->column2
);
2408 c2
= max (edit
->column1
, edit
->column2
);
2411 edit_update_curs_col (edit
);
2414 x2
= x
+ edit
->over_col
;
2416 /* do nothing when cursor inside first line of selected area */
2417 if ((edit_buffer_get_eol (&edit
->buffer
, edit
->buffer
.curs1
) ==
2418 edit_buffer_get_eol (&edit
->buffer
, start_mark
)) && x2
> c1
&& x2
<= c2
)
2421 if (edit
->buffer
.curs1
> start_mark
2422 && edit
->buffer
.curs1
< edit_buffer_get_eol (&edit
->buffer
, end_mark
))
2426 else if (x
> c1
&& x
<= c2
)
2429 /* save current selection into buffer */
2430 copy_buf
= edit_get_block (edit
, start_mark
, end_mark
, &size
);
2432 /* remove current selection */
2433 edit_block_delete_cmd (edit
);
2435 edit
->over_col
= max (0, edit
->over_col
- b_width
);
2436 /* calculate the cursor pos after delete block */
2437 current
= edit_move_forward3 (edit
, edit_buffer_get_current_bol (&edit
->buffer
), x
, 0);
2438 edit_cursor_move (edit
, current
- edit
->buffer
.curs1
);
2439 edit_scroll_screen_over_cursor (edit
);
2441 /* add TWS if need before block insertion */
2442 if (option_cursor_beyond_eol
&& edit
->over_col
> 0)
2443 edit_insert_over (edit
);
2445 edit_insert_column_of_text (edit
, copy_buf
, size
, b_width
, &mark1
, &mark2
, &c1
, &c2
);
2446 edit_set_markers (edit
, mark1
, mark2
, c1
, c2
);
2450 off_t count
, count_orig
;
2452 current
= edit
->buffer
.curs1
;
2453 copy_buf
= g_malloc0 (end_mark
- start_mark
);
2454 edit_cursor_move (edit
, start_mark
- edit
->buffer
.curs1
);
2455 edit_scroll_screen_over_cursor (edit
);
2457 for (count
= start_mark
; count
< end_mark
; count
++)
2458 copy_buf
[end_mark
- count
- 1] = edit_delete (edit
, TRUE
);
2460 edit_scroll_screen_over_cursor (edit
);
2461 edit_cursor_move (edit
,
2462 current
- edit
->buffer
.curs1
-
2463 (((current
- edit
->buffer
.curs1
) > 0) ? end_mark
- start_mark
: 0));
2464 edit_scroll_screen_over_cursor (edit
);
2466 while (count
-- > start_mark
)
2467 edit_insert_ahead (edit
, copy_buf
[end_mark
- count
- 1]);
2469 edit_set_markers (edit
, edit
->buffer
.curs1
, edit
->buffer
.curs1
+ end_mark
- start_mark
, 0,
2472 /* Place cursor at the end of text selection */
2473 if (option_cursor_after_inserted_block
)
2474 edit_cursor_move (edit
, count_orig
- start_mark
);
2477 edit_scroll_screen_over_cursor (edit
);
2479 edit
->force
|= REDRAW_PAGE
;
2482 /* --------------------------------------------------------------------------------------------- */
2483 /** returns 1 if canceelled by user */
2486 edit_block_delete_cmd (WEdit
* edit
)
2488 off_t start_mark
, end_mark
;
2490 if (eval_marks (edit
, &start_mark
, &end_mark
))
2491 return edit_block_delete (edit
);
2493 edit_delete_line (edit
);
2497 /* --------------------------------------------------------------------------------------------- */
2498 /** call with edit = 0 before shutdown to close memory leaks */
2501 edit_replace_cmd (WEdit
* edit
, int again
)
2503 /* 1 = search string, 2 = replace with */
2504 static char *saved1
= NULL
; /* saved default[123] */
2505 static char *saved2
= NULL
;
2506 char *input1
= NULL
; /* user input from the dialog */
2507 char *input2
= NULL
;
2508 GString
*input2_str
= NULL
;
2511 long times_replaced
= 0;
2512 gboolean once_found
= FALSE
;
2513 edit_search_status_msg_t esm
;
2517 MC_PTR_FREE (saved1
);
2518 MC_PTR_FREE (saved2
);
2522 edit
->force
|= REDRAW_COMPLETELY
;
2524 if (again
&& !saved1
&& !saved2
)
2529 input1
= g_strdup (saved1
? saved1
: "");
2530 input2
= g_strdup (saved2
? saved2
: "");
2534 char *tmp_inp1
, *tmp_inp2
;
2536 disp1
= edit_replace_cmd__conv_to_display (saved1
? saved1
: "");
2537 disp2
= edit_replace_cmd__conv_to_display (saved2
? saved2
: "");
2539 edit_push_undo_action (edit
, KEY_PRESS
+ edit
->start_display
);
2541 editcmd_dialog_replace_show (edit
, disp1
, disp2
, &input1
, &input2
);
2546 if (input1
== NULL
|| *input1
== '\0')
2548 edit
->force
= REDRAW_COMPLETELY
;
2554 input1
= edit_replace_cmd__conv_to_input (input1
);
2555 input2
= edit_replace_cmd__conv_to_input (input2
);
2559 g_free (saved1
), saved1
= g_strdup (input1
);
2560 g_free (saved2
), saved2
= g_strdup (input2
);
2562 mc_search_free (edit
->search
);
2563 edit
->search
= NULL
;
2566 input2_str
= g_string_new (input2
);
2568 if (edit
->search
== NULL
)
2571 edit
->search
= mc_search_new (input1
, cp_source
);
2573 edit
->search
= mc_search_new (input1
, NULL
);
2575 if (edit
->search
== NULL
)
2577 edit
->search_start
= edit
->buffer
.curs1
;
2580 edit
->search
->search_type
= edit_search_options
.type
;
2582 edit
->search
->is_all_charsets
= edit_search_options
.all_codepages
;
2584 edit
->search
->is_case_sensitive
= edit_search_options
.case_sens
;
2585 edit
->search
->whole_words
= edit_search_options
.whole_words
;
2586 edit
->search
->search_fn
= edit_search_cmd_callback
;
2587 edit
->search
->update_fn
= edit_search_update_callback
;
2588 edit
->search_line_type
= edit_get_search_line_type (edit
->search
);
2589 edit_search_fix_search_start_if_selection (edit
);
2592 if (edit
->found_len
&& edit
->search_start
== edit
->found_start
+ 1
2593 && edit_search_options
.backwards
)
2594 edit
->search_start
--;
2596 if (edit
->found_len
&& edit
->search_start
== edit
->found_start
- 1
2597 && !edit_search_options
.backwards
)
2598 edit
->search_start
++;
2602 esm
.offset
= edit
->search_start
;
2604 status_msg_init (STATUS_MSG (&esm
), _("Search"), 1.0, simple_status_msg_init_cb
,
2605 edit_search_status_update_cb
, NULL
);
2611 if (!editcmd_find (&esm
, &len
))
2613 if (!(edit
->search
->error
== MC_SEARCH_E_OK
||
2614 (once_found
&& edit
->search
->error
== MC_SEARCH_E_NOTFOUND
)))
2616 edit_query_dialog (_("Search"), edit
->search
->error_str
);
2622 edit
->search_start
= edit
->search
->normal_offset
;
2623 /*returns negative on not found or error in pattern */
2625 if ((edit
->search_start
>= 0) && (edit
->search_start
< edit
->buffer
.size
))
2630 edit
->found_start
= edit
->search_start
;
2631 i
= edit
->found_len
= len
;
2633 edit_cursor_move (edit
, edit
->search_start
- edit
->buffer
.curs1
);
2634 edit_scroll_screen_over_cursor (edit
);
2636 if (edit
->replace_mode
== 0)
2641 l
= edit
->curs_row
- WIDGET (edit
)->lines
/ 3;
2643 edit_scroll_downward (edit
, l
);
2645 edit_scroll_upward (edit
, -l
);
2647 edit_scroll_screen_over_cursor (edit
);
2648 edit
->force
|= REDRAW_PAGE
;
2649 edit_render_keypress (edit
);
2651 /*so that undo stops at each query */
2652 edit_push_key_press (edit
);
2653 /* and prompt 2/3 down */
2654 disp1
= edit_replace_cmd__conv_to_display (saved1
);
2655 disp2
= edit_replace_cmd__conv_to_display (saved2
);
2656 prompt
= editcmd_dialog_replace_prompt_show (edit
, disp1
, disp2
, -1, -1);
2660 if (prompt
== B_REPLACE_ALL
)
2661 edit
->replace_mode
= 1;
2662 else if (prompt
== B_SKIP_REPLACE
)
2664 if (edit_search_options
.backwards
)
2665 edit
->search_start
--;
2667 edit
->search_start
++;
2668 continue; /* loop */
2670 else if (prompt
== B_CANCEL
)
2672 edit
->replace_mode
= -1;
2677 repl_str
= mc_search_prepare_replace_str (edit
->search
, input2_str
);
2679 if (edit
->search
->error
!= MC_SEARCH_E_OK
)
2681 edit_error_dialog (_("Replace"), edit
->search
->error_str
);
2682 g_string_free (repl_str
, TRUE
);
2686 /* delete then insert new */
2687 for (i
= 0; i
< len
; i
++)
2688 edit_delete (edit
, TRUE
);
2690 for (i
= 0; i
< repl_str
->len
; i
++)
2691 edit_insert (edit
, repl_str
->str
[i
]);
2693 edit
->found_len
= repl_str
->len
;
2694 g_string_free (repl_str
, TRUE
);
2697 /* so that we don't find the same string again */
2698 if (edit_search_options
.backwards
)
2700 edit
->search_start
--;
2704 edit
->search_start
+= edit
->found_len
+ (len
== 0 ? 1 : 0);
2706 if (edit
->search_start
>= edit
->buffer
.size
)
2710 edit_scroll_screen_over_cursor (edit
);
2714 /* try and find from right here for next search */
2715 edit
->search_start
= edit
->buffer
.curs1
;
2716 edit_update_curs_col (edit
);
2718 edit
->force
|= REDRAW_PAGE
;
2719 edit_render_keypress (edit
);
2721 if (times_replaced
== 0)
2722 query_dialog (_("Replace"), _(STR_E_NOTFOUND
), D_NORMAL
, 1, _("&OK"));
2726 while (edit
->replace_mode
>= 0);
2728 status_msg_deinit (STATUS_MSG (&esm
));
2729 edit_scroll_screen_over_cursor (edit
);
2730 edit
->force
|= REDRAW_COMPLETELY
;
2731 edit_render_keypress (edit
);
2733 if ((edit
->replace_mode
== 1) && (times_replaced
!= 0))
2734 message (D_NORMAL
, _("Replace"), _("%ld replacements made"), times_replaced
);
2739 if (input2_str
!= NULL
)
2740 g_string_free (input2_str
, TRUE
);
2743 /* --------------------------------------------------------------------------------------------- */
2746 edit_search_cmd_callback (const void *user_data
, gsize char_offset
, int *current_char
)
2748 WEdit
*edit
= ((const edit_search_status_msg_t
*) user_data
)->edit
;
2750 *current_char
= edit_buffer_get_byte (&edit
->buffer
, (off_t
) char_offset
);
2751 return MC_SEARCH_CB_OK
;
2754 /* --------------------------------------------------------------------------------------------- */
2757 edit_search_update_callback (const void *user_data
, gsize char_offset
)
2759 status_msg_t
*sm
= STATUS_MSG (user_data
);
2761 ((edit_search_status_msg_t
*) sm
)->offset
= (off_t
) char_offset
;
2763 return (sm
->update (sm
) == B_CANCEL
? MC_SEARCH_CB_ABORT
: MC_SEARCH_CB_OK
);
2766 /* --------------------------------------------------------------------------------------------- */
2769 edit_search_cmd (WEdit
* edit
, gboolean again
)
2777 else if (edit
->last_search_string
!= NULL
)
2778 edit_do_search (edit
);
2781 /* find last search string in history */
2784 history
= history_get (MC_HISTORY_SHARED_SEARCH
);
2785 if (history
!= NULL
&& history
->data
!= NULL
)
2787 edit
->last_search_string
= (char *) history
->data
;
2788 history
->data
= NULL
;
2789 history
= g_list_first (history
);
2790 g_list_free_full (history
, g_free
);
2793 edit
->search
= mc_search_new (edit
->last_search_string
, cp_source
);
2795 edit
->search
= mc_search_new (edit
->last_search_string
, NULL
);
2797 if (edit
->search
== NULL
)
2799 /* if not... then ask for an expression */
2800 MC_PTR_FREE (edit
->last_search_string
);
2805 edit
->search
->search_type
= edit_search_options
.type
;
2807 edit
->search
->is_all_charsets
= edit_search_options
.all_codepages
;
2809 edit
->search
->is_case_sensitive
= edit_search_options
.case_sens
;
2810 edit
->search
->whole_words
= edit_search_options
.whole_words
;
2811 edit
->search
->search_fn
= edit_search_cmd_callback
;
2812 edit
->search
->update_fn
= edit_search_update_callback
;
2813 edit
->search_line_type
= edit_get_search_line_type (edit
->search
);
2814 edit_do_search (edit
);
2819 /* if not... then ask for an expression */
2820 MC_PTR_FREE (edit
->last_search_string
);
2827 /* --------------------------------------------------------------------------------------------- */
2829 * Check if it's OK to close the file. If there are unsaved changes, ask user.
2831 * @return TRUE if it's OK to exit, FALSE to continue editing.
2835 edit_ok_to_exit (WEdit
* edit
)
2837 const char *fname
= N_("[NoName]");
2841 if (!edit
->modified
)
2844 if (edit
->filename_vpath
!= NULL
)
2845 fname
= vfs_path_as_str (edit
->filename_vpath
);
2851 if (!mc_global
.midnight_shutdown
)
2855 msg
= g_strdup_printf (_("File %s was modified.\nSave before close?"), fname
);
2856 act
= edit_query_dialog3 (_("Close file"), msg
, _("&Yes"), _("&No"), _("&Cancel"));
2860 msg
= g_strdup_printf (_("Midnight Commander is being shut down.\nSave modified file %s?"),
2862 act
= edit_query_dialog2 (_("Quit"), msg
, _("&Yes"), _("&No"));
2874 if (!mc_global
.midnight_shutdown
&& !edit_check_newline (&edit
->buffer
))
2876 edit_push_markers (edit
);
2877 edit_set_markers (edit
, 0, 0, 0, 0);
2878 if (!edit_save_cmd (edit
) || mc_global
.midnight_shutdown
)
2879 return mc_global
.midnight_shutdown
;
2884 case 2: /* Cancel quit */
2892 /* --------------------------------------------------------------------------------------------- */
2893 /** save block, returns TRUE on success */
2896 edit_save_block (WEdit
* edit
, const char *filename
, off_t start
, off_t finish
)
2902 vpath
= vfs_path_from_str (filename
);
2903 file
= mc_open (vpath
, O_CREAT
| O_WRONLY
| O_TRUNC
,
2904 S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IROTH
| O_BINARY
);
2905 vfs_path_free (vpath
);
2909 if (edit
->column_highlight
)
2913 r
= mc_write (file
, VERTICAL_MAGIC
, sizeof (VERTICAL_MAGIC
));
2916 unsigned char *block
, *p
;
2918 p
= block
= edit_get_block (edit
, start
, finish
, &len
);
2921 r
= mc_write (file
, p
, len
);
2936 len
= finish
- start
;
2937 buf
= g_malloc0 (TEMP_BUF_LEN
);
2938 while (start
!= finish
)
2940 end
= min (finish
, start
+ TEMP_BUF_LEN
);
2941 for (; i
< end
; i
++)
2942 buf
[i
- start
] = edit_buffer_get_byte (&edit
->buffer
, i
);
2943 len
-= mc_write (file
, (char *) buf
, end
- start
);
2953 /* --------------------------------------------------------------------------------------------- */
2956 edit_paste_from_history (WEdit
* edit
)
2959 edit_error_dialog (_("Error"), _("This function is not implemented"));
2962 /* --------------------------------------------------------------------------------------------- */
2965 edit_copy_to_X_buf_cmd (WEdit
* edit
)
2967 off_t start_mark
, end_mark
;
2969 if (!eval_marks (edit
, &start_mark
, &end_mark
))
2972 if (!edit_save_block_to_clip_file (edit
, start_mark
, end_mark
))
2974 edit_error_dialog (_("Copy to clipboard"), get_sys_error (_("Unable to save to file")));
2977 /* try use external clipboard utility */
2978 mc_event_raise (MCEVENT_GROUP_CORE
, "clipboard_file_to_ext_clip", NULL
);
2980 if (option_drop_selection_on_copy
)
2981 edit_mark_cmd (edit
, TRUE
);
2986 /* --------------------------------------------------------------------------------------------- */
2989 edit_cut_to_X_buf_cmd (WEdit
* edit
)
2991 off_t start_mark
, end_mark
;
2993 if (!eval_marks (edit
, &start_mark
, &end_mark
))
2996 if (!edit_save_block_to_clip_file (edit
, start_mark
, end_mark
))
2998 edit_error_dialog (_("Cut to clipboard"), _("Unable to save to file"));
3001 /* try use external clipboard utility */
3002 mc_event_raise (MCEVENT_GROUP_CORE
, "clipboard_file_to_ext_clip", NULL
);
3004 edit_block_delete_cmd (edit
);
3005 edit_mark_cmd (edit
, TRUE
);
3010 /* --------------------------------------------------------------------------------------------- */
3013 edit_paste_from_X_buf_cmd (WEdit
* edit
)
3018 /* try use external clipboard utility */
3019 mc_event_raise (MCEVENT_GROUP_CORE
, "clipboard_file_from_ext_clip", NULL
);
3020 tmp
= mc_config_get_full_vpath (EDIT_CLIP_FILE
);
3021 ret
= (edit_insert_file (edit
, tmp
) >= 0);
3022 vfs_path_free (tmp
);
3027 /* --------------------------------------------------------------------------------------------- */
3029 * Ask user for the line and go to that line.
3030 * Negative numbers mean line from the end (i.e. -1 is the last line).
3034 edit_goto_cmd (WEdit
* edit
)
3036 static gboolean first_run
= TRUE
;
3042 f
= input_dialog (_("Goto line"), _("Enter line:"), MC_HISTORY_EDIT_GOTO_LINE
,
3043 first_run
? NULL
: INPUT_LAST_TEXT
, INPUT_COMPLETE_NONE
);
3044 if (f
== NULL
|| *f
== '\0')
3050 l
= strtol (f
, &error
, 0);
3058 l
= edit
->buffer
.lines
+ l
+ 2;
3059 edit_move_display (edit
, l
- WIDGET (edit
)->lines
/ 2 - 1);
3060 edit_move_to_line (edit
, l
- 1);
3061 edit
->force
|= REDRAW_COMPLETELY
;
3068 /* --------------------------------------------------------------------------------------------- */
3069 /** Return TRUE on success */
3072 edit_save_block_cmd (WEdit
* edit
)
3074 off_t start_mark
, end_mark
;
3076 gboolean ret
= FALSE
;
3078 if (!eval_marks (edit
, &start_mark
, &end_mark
))
3081 tmp
= mc_config_get_full_path (EDIT_CLIP_FILE
);
3083 input_expand_dialog (_("Save block"), _("Enter file name:"),
3084 MC_HISTORY_EDIT_SAVE_BLOCK
, tmp
, INPUT_COMPLETE_FILENAMES
);
3086 edit_push_undo_action (edit
, KEY_PRESS
+ edit
->start_display
);
3088 if (exp
!= NULL
&& *exp
!= '\0')
3090 if (edit_save_block (edit
, exp
, start_mark
, end_mark
))
3093 edit_error_dialog (_("Save block"), get_sys_error (_("Cannot save file")));
3095 edit
->force
|= REDRAW_COMPLETELY
;
3104 /* --------------------------------------------------------------------------------------------- */
3106 /** returns TRUE on success */
3108 edit_insert_file_cmd (WEdit
* edit
)
3112 gboolean ret
= FALSE
;
3114 tmp
= mc_config_get_full_path (EDIT_CLIP_FILE
);
3115 exp
= input_expand_dialog (_("Insert file"), _("Enter file name:"),
3116 MC_HISTORY_EDIT_INSERT_FILE
, tmp
, INPUT_COMPLETE_FILENAMES
);
3119 edit_push_undo_action (edit
, KEY_PRESS
+ edit
->start_display
);
3121 if (exp
!= NULL
&& *exp
!= '\0')
3123 vfs_path_t
*exp_vpath
;
3125 exp_vpath
= vfs_path_from_str (exp
);
3126 ret
= (edit_insert_file (edit
, exp_vpath
) >= 0);
3127 vfs_path_free (exp_vpath
);
3130 edit_error_dialog (_("Insert file"), get_sys_error (_("Cannot insert file")));
3135 edit
->force
|= REDRAW_COMPLETELY
;
3139 /* --------------------------------------------------------------------------------------------- */
3140 /** sorts a block, returns -1 on system fail, 1 on cancel and 0 on success */
3143 edit_sort_cmd (WEdit
* edit
)
3145 char *exp
, *tmp
, *tmp_edit_block_name
, *tmp_edit_temp_name
;
3146 off_t start_mark
, end_mark
;
3149 if (!eval_marks (edit
, &start_mark
, &end_mark
))
3151 edit_error_dialog (_("Sort block"), _("You must first highlight a block of text"));
3155 tmp
= mc_config_get_full_path (EDIT_BLOCK_FILE
);
3156 edit_save_block (edit
, tmp
, start_mark
, end_mark
);
3159 exp
= input_dialog (_("Run sort"),
3160 _("Enter sort options (see manpage) separated by whitespace:"),
3161 MC_HISTORY_EDIT_SORT
, INPUT_LAST_TEXT
, INPUT_COMPLETE_NONE
);
3166 tmp_edit_block_name
= mc_config_get_full_path (EDIT_BLOCK_FILE
);
3167 tmp_edit_temp_name
= mc_config_get_full_path (EDIT_TEMP_FILE
);
3169 g_strconcat (" sort ", exp
, " ", tmp_edit_block_name
,
3170 " > ", tmp_edit_temp_name
, (char *) NULL
);
3171 g_free (tmp_edit_temp_name
);
3172 g_free (tmp_edit_block_name
);
3179 if (e
== -1 || e
== 127)
3180 edit_error_dialog (_("Sort"), get_sys_error (_("Cannot execute sort command")));
3185 sprintf (q
, "%d ", e
);
3186 tmp
= g_strdup_printf (_("Sort returned non-zero: %s"), q
);
3187 edit_error_dialog (_("Sort"), tmp
);
3193 edit
->force
|= REDRAW_COMPLETELY
;
3195 if (edit_block_delete_cmd (edit
))
3199 vfs_path_t
*tmp_vpath
;
3201 tmp_vpath
= mc_config_get_full_vpath (EDIT_TEMP_FILE
);
3202 edit_insert_file (edit
, tmp_vpath
);
3203 vfs_path_free (tmp_vpath
);
3208 /* --------------------------------------------------------------------------------------------- */
3210 * Ask user for a command, execute it and paste its output back to the
3215 edit_ext_cmd (WEdit
* edit
)
3217 char *exp
, *tmp
, *tmp_edit_temp_file
;
3221 input_dialog (_("Paste output of external command"),
3222 _("Enter shell command(s):"), MC_HISTORY_EDIT_PASTE_EXTCMD
, INPUT_LAST_TEXT
,
3223 INPUT_COMPLETE_FILENAMES
| INPUT_COMPLETE_VARIABLES
| INPUT_COMPLETE_USERNAMES
3224 | INPUT_COMPLETE_HOSTNAMES
| INPUT_COMPLETE_CD
| INPUT_COMPLETE_COMMANDS
|
3225 INPUT_COMPLETE_SHELL_ESC
);
3230 tmp_edit_temp_file
= mc_config_get_full_path (EDIT_TEMP_FILE
);
3231 tmp
= g_strconcat (exp
, " > ", tmp_edit_temp_file
, (char *) NULL
);
3232 g_free (tmp_edit_temp_file
);
3239 edit_error_dialog (_("External command"), get_sys_error (_("Cannot execute command")));
3243 edit
->force
|= REDRAW_COMPLETELY
;
3246 vfs_path_t
*tmp_vpath
;
3248 tmp_vpath
= mc_config_get_full_vpath (EDIT_TEMP_FILE
);
3249 edit_insert_file (edit
, tmp_vpath
);
3250 vfs_path_free (tmp_vpath
);
3255 /* --------------------------------------------------------------------------------------------- */
3256 /** if block is 1, a block must be highlighted and the shell command
3257 processes it. If block is 0 the shell command is a straight system
3258 command, that just produces some output which is to be inserted */
3261 edit_block_process_cmd (WEdit
* edit
, int macro_number
)
3264 char *macros_fname
= NULL
;
3266 fname
= g_strdup_printf ("%s.%i.sh", MC_EXTMACRO_FILE
, macro_number
);
3267 macros_fname
= g_build_filename (mc_config_get_data_path (), fname
, (char *) NULL
);
3268 user_menu (edit
, macros_fname
, 0);
3270 g_free (macros_fname
);
3271 edit
->force
|= REDRAW_COMPLETELY
;
3274 /* --------------------------------------------------------------------------------------------- */
3277 edit_mail_dialog (WEdit
* edit
)
3279 char *mail_to
, *mail_subject
, *mail_cc
;
3281 quick_widget_t quick_widgets
[] = {
3283 QUICK_LABEL (N_("mail -s <subject> -c <cc> <to>"), NULL
),
3284 QUICK_LABELED_INPUT (N_("To"), input_label_above
,
3285 INPUT_LAST_TEXT
, "mail-dlg-input-3",
3286 &mail_to
, NULL
, FALSE
, FALSE
, INPUT_COMPLETE_USERNAMES
),
3287 QUICK_LABELED_INPUT (N_("Subject"), input_label_above
,
3288 INPUT_LAST_TEXT
, "mail-dlg-input-2",
3289 &mail_subject
, NULL
, FALSE
, FALSE
, INPUT_COMPLETE_NONE
),
3290 QUICK_LABELED_INPUT (N_("Copies to"), input_label_above
,
3291 INPUT_LAST_TEXT
, "mail-dlg-input",
3292 &mail_cc
, NULL
, FALSE
, FALSE
, INPUT_COMPLETE_USERNAMES
),
3293 QUICK_BUTTONS_OK_CANCEL
,
3298 quick_dialog_t qdlg
= {
3300 N_("Mail"), "[Input Line Keys]",
3301 quick_widgets
, NULL
, NULL
3304 if (quick_dialog (&qdlg
) != B_CANCEL
)
3306 pipe_mail (&edit
->buffer
, mail_to
, mail_subject
, mail_cc
);
3308 g_free (mail_subject
);
3313 /* --------------------------------------------------------------------------------------------- */
3315 /*******************/
3316 /* Word Completion */
3317 /*******************/
3320 * Complete current word using regular expression search
3321 * backwards beginning at the current cursor position.
3325 edit_complete_word_cmd (WEdit
* edit
)
3327 gsize i
, max_len
, word_len
= 0, num_compl
= 0;
3328 off_t word_start
= 0;
3329 GString
*match_expr
;
3330 GString
*compl[MAX_WORD_COMPLETIONS
]; /* completions */
3332 /* search start of word to be completed */
3333 if (!edit_find_word_start (&edit
->buffer
, &word_start
, &word_len
))
3336 /* prepare match expression */
3337 /* match_expr = g_strdup_printf ("\\b%.*s[a-zA-Z_0-9]+", word_len, bufpos); */
3338 match_expr
= g_string_new ("(^|\\s+|\\b)");
3339 for (i
= 0; i
< word_len
; i
++)
3340 g_string_append_c (match_expr
, edit_buffer_get_byte (&edit
->buffer
, word_start
+ i
));
3341 g_string_append (match_expr
,
3342 "[^\\s\\.=\\+\\[\\]\\(\\)\\,\\;\\:\\\"\\'\\-\\?\\/\\|\\\\\\{\\}\\*\\&\\^\\%%\\$#@\\!]+");
3344 /* collect the possible completions */
3345 /* start search from begin to end of file */
3347 edit_collect_completions (edit
, word_start
, word_len
, match_expr
->str
, (GString
**) & compl,
3352 /* insert completed word if there is only one match */
3354 edit_complete_word_insert_recoded_completion (edit
, compl[0]->str
, word_len
);
3355 /* more than one possible completion => ask the user */
3360 /* !!! usually only a beep is expected and when <ALT-TAB> is !!! */
3361 /* !!! pressed again the selection dialog pops up, but that !!! */
3362 /* !!! seems to require a further internal state !!! */
3365 /* let the user select the preferred completion */
3366 curr_compl
= editcmd_dialog_completion_show (edit
, max_len
,
3367 (GString
**) & compl, num_compl
);
3369 if (curr_compl
!= NULL
)
3371 edit_complete_word_insert_recoded_completion (edit
, curr_compl
, word_len
);
3372 g_free (curr_compl
);
3377 g_string_free (match_expr
, TRUE
);
3378 /* release memory before return */
3379 for (i
= 0; i
< num_compl
; i
++)
3380 g_string_free (compl[i
], TRUE
);
3383 /* --------------------------------------------------------------------------------------------- */
3387 edit_select_codepage_cmd (WEdit
* edit
)
3389 if (do_select_codepage ())
3390 edit_set_codeset (edit
);
3392 edit
->force
= REDRAW_PAGE
;
3393 widget_redraw (WIDGET (edit
));
3397 /* --------------------------------------------------------------------------------------------- */
3400 edit_insert_literal_cmd (WEdit
* edit
)
3402 int char_for_insertion
;
3404 char_for_insertion
= editcmd_dialog_raw_key_query (_("Insert literal"),
3405 _("Press any key:"), FALSE
);
3406 edit_execute_key_command (edit
, -1, ascii_alpha_to_cntrl (char_for_insertion
));
3409 /* --------------------------------------------------------------------------------------------- */
3412 edit_begin_end_macro_cmd (WEdit
* edit
)
3414 /* edit is a pointer to the widget */
3417 long command
= macro_index
< 0 ? CK_MacroStartRecord
: CK_MacroStopRecord
;
3418 edit_execute_key_command (edit
, command
, -1);
3422 /* --------------------------------------------------------------------------------------------- */
3425 edit_begin_end_repeat_cmd (WEdit
* edit
)
3427 /* edit is a pointer to the widget */
3430 long command
= macro_index
< 0 ? CK_RepeatStartRecord
: CK_RepeatStopRecord
;
3431 edit_execute_key_command (edit
, command
, -1);
3435 /* --------------------------------------------------------------------------------------------- */
3438 edit_load_forward_cmd (WEdit
* edit
)
3441 && edit_query_dialog2 (_("Warning"),
3442 _("Current text was modified without a file save.\n"
3443 "Continue discards these changes"), _("C&ontinue"),
3446 edit
->force
|= REDRAW_COMPLETELY
;
3450 if (edit_stack_iterator
+ 1 >= MAX_HISTORY_MOVETO
)
3453 if (edit_history_moveto
[edit_stack_iterator
+ 1].line
< 1)
3456 edit_stack_iterator
++;
3457 if (edit_history_moveto
[edit_stack_iterator
].filename_vpath
!= NULL
)
3458 return edit_reload_line (edit
, edit_history_moveto
[edit_stack_iterator
].filename_vpath
,
3459 edit_history_moveto
[edit_stack_iterator
].line
);
3464 /* --------------------------------------------------------------------------------------------- */
3467 edit_load_back_cmd (WEdit
* edit
)
3470 && edit_query_dialog2 (_("Warning"),
3471 _("Current text was modified without a file save.\n"
3472 "Continue discards these changes"), _("C&ontinue"),
3475 edit
->force
|= REDRAW_COMPLETELY
;
3479 /* we are in the bottom of the stack, NO WAY! */
3480 if (edit_stack_iterator
== 0)
3483 edit_stack_iterator
--;
3484 if (edit_history_moveto
[edit_stack_iterator
].filename_vpath
!= NULL
)
3485 return edit_reload_line (edit
, edit_history_moveto
[edit_stack_iterator
].filename_vpath
,
3486 edit_history_moveto
[edit_stack_iterator
].line
);
3491 /* --------------------------------------------------------------------------------------------- */
3494 edit_get_match_keyword_cmd (WEdit
* edit
)
3496 gsize word_len
= 0, max_len
= 0;
3499 off_t word_start
= 0;
3500 GString
*match_expr
;
3503 char *tagfile
= NULL
;
3505 etags_hash_t def_hash
[MAX_DEFINITIONS
];
3507 for (i
= 0; i
< MAX_DEFINITIONS
; i
++)
3509 def_hash
[i
].filename
= NULL
;
3512 /* search start of word to be completed */
3513 if (!edit_find_word_start (&edit
->buffer
, &word_start
, &word_len
))
3516 /* prepare match expression */
3517 match_expr
= g_string_sized_new (word_len
);
3518 for (i
= 0; i
< word_len
; i
++)
3519 g_string_append_c (match_expr
, edit_buffer_get_byte (&edit
->buffer
, word_start
+ i
));
3521 ptr
= g_get_current_dir ();
3522 path
= g_strconcat (ptr
, PATH_SEP_STR
, (char *) NULL
);
3525 /* Recursive search file 'TAGS' in parent dirs */
3528 ptr
= g_path_get_dirname (path
);
3532 tagfile
= mc_build_filename (path
, TAGS_NAME
, (char *) NULL
);
3533 if (exist_file (tagfile
))
3536 while (strcmp (path
, PATH_SEP_STR
) != 0);
3541 etags_set_definition_hash (tagfile
, path
, match_expr
->str
, (etags_hash_t
*) & def_hash
);
3546 max_len
= MAX_WIDTH_DEF_DIALOG
;
3550 editcmd_dialog_select_definition_show (edit
, match_expr
->str
, max_len
, word_len
,
3551 (etags_hash_t
*) & def_hash
, num_def
);
3553 g_string_free (match_expr
, TRUE
);
3556 /* --------------------------------------------------------------------------------------------- */
3560 edit_suggest_current_word (WEdit
* edit
)
3564 off_t word_start
= 0;
3565 int retval
= B_SKIP_WORD
;
3566 GString
*match_word
;
3568 /* search start of word to spell check */
3569 match_word
= edit_buffer_get_word_from_pos (&edit
->buffer
, edit
->buffer
.curs1
, &word_start
,
3571 word_len
= match_word
->len
;
3574 if (mc_global
.source_codepage
>= 0 && (mc_global
.source_codepage
!= mc_global
.display_codepage
))
3578 tmp_word
= str_convert_to_display (match_word
->str
);
3579 g_string_free (match_word
, TRUE
);
3580 match_word
= tmp_word
;
3583 if (!aspell_check (match_word
->str
, (int) word_len
))
3589 suggest
= g_array_new (TRUE
, FALSE
, sizeof (char *));
3591 res
= aspell_suggest (suggest
, match_word
->str
, (int) word_len
);
3594 char *new_word
= NULL
;
3596 edit
->found_start
= word_start
;
3597 edit
->found_len
= word_len
;
3598 edit
->force
|= REDRAW_PAGE
;
3599 edit_scroll_screen_over_cursor (edit
);
3600 edit_render_keypress (edit
);
3602 retval
= spell_dialog_spell_suggest_show (edit
, match_word
->str
, &new_word
, suggest
);
3603 edit_cursor_move (edit
, word_len
- cut_len
);
3605 if (retval
== B_ENTER
&& new_word
!= NULL
)
3610 if (mc_global
.source_codepage
>= 0 &&
3611 (mc_global
.source_codepage
!= mc_global
.display_codepage
))
3615 tmp_word
= str_convert_to_input (new_word
);
3617 new_word
= g_string_free (tmp_word
, FALSE
);
3621 for (i
= 0; i
< word_len
; i
++)
3622 edit_backspace (edit
, TRUE
);
3623 for (; *new_word
; new_word
++)
3624 edit_insert (edit
, *new_word
);
3627 else if (retval
== B_ADD_WORD
&& match_word
!= NULL
)
3628 aspell_add_to_dict (match_word
->str
, (int) word_len
);
3632 for (i
= 0; i
< suggest
->len
; i
++)
3634 char *cur_sugg_word
;
3636 cur_sugg_word
= g_array_index (suggest
, char *, i
);
3637 g_free (cur_sugg_word
);
3639 g_array_free (suggest
, TRUE
);
3640 edit
->found_start
= 0;
3641 edit
->found_len
= 0;
3643 g_string_free (match_word
, TRUE
);
3647 /* --------------------------------------------------------------------------------------------- */
3650 edit_spellcheck_file (WEdit
* edit
)
3652 if (edit
->buffer
.curs_line
> 0)
3654 edit_cursor_move (edit
, -edit
->buffer
.curs1
);
3655 edit_move_to_prev_col (edit
, 0);
3656 edit_update_curs_row (edit
);
3663 c2
= edit_buffer_get_current_byte (&edit
->buffer
);
3667 if (edit
->buffer
.curs1
>= edit
->buffer
.size
)
3671 edit_cursor_move (edit
, 1);
3672 c2
= edit_buffer_get_current_byte (&edit
->buffer
);
3674 while (is_break_char (c1
) || is_break_char (c2
));
3676 while (edit_suggest_current_word (edit
) != B_CANCEL
);
3679 /* --------------------------------------------------------------------------------------------- */
3682 edit_set_spell_lang (void)
3686 lang_list
= g_array_new (TRUE
, FALSE
, sizeof (char *));
3687 if (aspell_get_lang_list (lang_list
) != 0)
3691 lang
= spell_dialog_lang_list_show (lang_list
);
3694 (void) aspell_set_lang (lang
);
3698 aspell_array_clean (lang_list
);
3700 #endif /* HAVE_ASPELL */
3702 /* --------------------------------------------------------------------------------------------- */