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", (char *) 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 mc_search_set_error (edit
->search
, MC_SEARCH_E_NOTFOUND
, "%s", _(STR_E_NOTFOUND
));
836 /* fix the start and the end of search block positions */
837 if ((edit
->search_line_type
& AT_START_LINE
) != 0
839 || edit_buffer_get_byte (&edit
->buffer
, start_mark
- 1) != end_string_symbol
))
841 edit_calculate_start_of_next_line (&edit
->buffer
, start_mark
, edit
->buffer
.size
,
844 if ((edit
->search_line_type
& AT_END_LINE
) != 0
845 && (end_mark
- 1 != edit
->buffer
.size
846 || edit_buffer_get_byte (&edit
->buffer
, end_mark
) != end_string_symbol
))
848 edit_calculate_end_of_previous_line (&edit
->buffer
, end_mark
, end_string_symbol
);
850 if (start_mark
>= end_mark
)
852 mc_search_set_error (edit
->search
, MC_SEARCH_E_NOTFOUND
, "%s", _(STR_E_NOTFOUND
));
856 else if (edit_search_options
.backwards
)
857 end_mark
= MAX (1, edit
->buffer
.curs1
) - 1;
860 if (edit_search_options
.backwards
)
862 /* backward search */
863 search_end
= end_mark
;
865 if ((edit
->search_line_type
& AT_START_LINE
) != 0)
867 edit_calculate_start_of_current_line (&edit
->buffer
, search_start
,
870 while (search_start
>= start_mark
)
874 if (search_end
> (off_t
) (search_start
+ edit
->search
->original_len
)
875 && mc_search_is_fixed_search_str (edit
->search
))
876 search_end
= search_start
+ edit
->search
->original_len
;
878 ok
= mc_search_run (edit
->search
, (void *) esm
, search_start
, search_end
, len
);
880 if (ok
&& edit
->search
->normal_offset
== search_start
)
884 if (!ok
&& edit
->search
->error
== MC_SEARCH_E_ABORT
)
887 if ((edit
->search_line_type
& AT_START_LINE
) != 0)
889 edit_calculate_start_of_previous_line (&edit
->buffer
, search_start
,
895 mc_search_set_error (edit
->search
, MC_SEARCH_E_NOTFOUND
, "%s", _(STR_E_NOTFOUND
));
900 if ((edit
->search_line_type
& AT_START_LINE
) != 0 && search_start
!= start_mark
)
902 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
);
908 /* --------------------------------------------------------------------------------------------- */
911 edit_replace_cmd__conv_to_display (const char *str
)
916 tmp
= str_convert_to_display (str
);
920 return g_string_free (tmp
, FALSE
);
921 g_string_free (tmp
, TRUE
);
924 return g_strdup (str
);
927 /* --------------------------------------------------------------------------------------------- */
930 edit_replace_cmd__conv_to_input (char *str
)
935 tmp
= str_convert_to_input (str
);
939 return g_string_free (tmp
, FALSE
);
940 g_string_free (tmp
, TRUE
);
943 return g_strdup (str
);
946 /* --------------------------------------------------------------------------------------------- */
949 edit_do_search (WEdit
* edit
)
951 edit_search_status_msg_t esm
;
954 if (edit
->search
== NULL
)
955 edit
->search_start
= edit
->buffer
.curs1
;
957 edit_push_undo_action (edit
, KEY_PRESS
+ edit
->start_display
);
961 esm
.offset
= edit
->search_start
;
963 status_msg_init (STATUS_MSG (&esm
), _("Search"), 1.0, simple_status_msg_init_cb
,
964 edit_search_status_update_cb
, NULL
);
966 if (search_create_bookmark
)
968 int found
= 0, books
= 0;
969 long l
= 0, l_last
= -1;
972 search_create_bookmark
= FALSE
;
973 book_mark_flush (edit
, -1);
975 while (mc_search_run (edit
->search
, (void *) &esm
, q
, edit
->buffer
.size
, &len
))
978 edit
->search_start
= edit
->search
->normal_offset
;
980 l
+= edit_buffer_count_lines (&edit
->buffer
, q
, edit
->search
->normal_offset
);
983 book_mark_insert (edit
, l
, BOOK_MARK_FOUND_COLOR
);
987 q
= edit
->search
->normal_offset
+ 1;
991 edit_error_dialog (_("Search"), _(STR_E_NOTFOUND
));
993 edit_cursor_move (edit
, edit
->search_start
- edit
->buffer
.curs1
);
997 if (edit
->found_len
!= 0 && edit
->search_start
== edit
->found_start
+ 1
998 && edit_search_options
.backwards
)
999 edit
->search_start
--;
1001 if (edit
->found_len
!= 0 && edit
->search_start
== edit
->found_start
- 1
1002 && !edit_search_options
.backwards
)
1003 edit
->search_start
++;
1005 if (editcmd_find (&esm
, &len
))
1007 edit
->found_start
= edit
->search_start
= edit
->search
->normal_offset
;
1008 edit
->found_len
= len
;
1010 edit_cursor_move (edit
, edit
->search_start
- edit
->buffer
.curs1
);
1011 edit_scroll_screen_over_cursor (edit
);
1012 if (edit_search_options
.backwards
)
1013 edit
->search_start
--;
1015 edit
->search_start
++;
1019 edit
->search_start
= edit
->buffer
.curs1
;
1020 if (edit
->search
->error_str
!= NULL
)
1021 edit_query_dialog (_("Search"), edit
->search
->error_str
);
1025 status_msg_deinit (STATUS_MSG (&esm
));
1027 edit
->force
|= REDRAW_COMPLETELY
;
1028 edit_scroll_screen_over_cursor (edit
);
1031 /* --------------------------------------------------------------------------------------------- */
1034 edit_search (WEdit
* edit
)
1036 if (editcmd_dialog_search_show (edit
))
1038 edit
->search_line_type
= edit_get_search_line_type (edit
->search
);
1039 edit_search_fix_search_start_if_selection (edit
);
1040 edit_do_search (edit
);
1044 /* --------------------------------------------------------------------------------------------- */
1045 /** Return a null terminated length of text. Result must be g_free'd */
1047 static unsigned char *
1048 edit_get_block (WEdit
* edit
, off_t start
, off_t finish
, off_t
* l
)
1050 unsigned char *s
, *r
;
1052 r
= s
= g_malloc0 (finish
- start
+ 1);
1053 if (edit
->column_highlight
)
1056 /* copy from buffer, excluding chars that are out of the column 'margins' */
1057 while (start
< finish
)
1062 x
= edit_move_forward3 (edit
, edit_buffer_get_bol (&edit
->buffer
, start
), 0, start
);
1063 c
= edit_buffer_get_byte (&edit
->buffer
, start
);
1064 if ((x
>= edit
->column1
&& x
< edit
->column2
)
1065 || (x
>= edit
->column2
&& x
< edit
->column1
) || c
== '\n')
1075 *l
= finish
- start
;
1076 while (start
< finish
)
1077 *s
++ = edit_buffer_get_byte (&edit
->buffer
, start
++);
1083 /* --------------------------------------------------------------------------------------------- */
1084 /** copies a block to clipboard file */
1087 edit_save_block_to_clip_file (WEdit
* edit
, off_t start
, off_t finish
)
1092 tmp
= mc_config_get_full_path (EDIT_CLIP_FILE
);
1093 ret
= edit_save_block (edit
, tmp
, start
, finish
);
1098 /* --------------------------------------------------------------------------------------------- */
1101 pipe_mail (const edit_buffer_t
* buf
, char *to
, char *subject
, char *cc
)
1106 to
= name_quote (to
, FALSE
);
1107 subject
= name_quote (subject
, FALSE
);
1108 cc
= name_quote (cc
, FALSE
);
1109 s
= g_strconcat ("mail -s ", subject
, *cc
? " -c " : "", cc
, " ", to
, (char *) NULL
);
1124 for (i
= 0; i
< buf
->size
; i
++)
1125 fputc (edit_buffer_get_byte (buf
, i
), p
);
1130 /* --------------------------------------------------------------------------------------------- */
1131 /** find first character of current word */
1134 edit_find_word_start (const edit_buffer_t
* buf
, off_t
* word_start
, gsize
* word_len
)
1139 /* return if at begin of file */
1140 if (buf
->curs1
<= 0)
1143 c
= edit_buffer_get_previous_byte (buf
);
1144 /* return if not at end or in word */
1145 if (is_break_char (c
))
1148 /* search start of word to be completed */
1154 c
= edit_buffer_get_byte (buf
, buf
->curs1
- i
- 1);
1156 if (is_break_char (c
))
1158 /* return if word starts with digit */
1167 *word_start
= buf
->curs1
- i
; /* start found */
1168 *word_len
= (gsize
) i
;
1172 /* --------------------------------------------------------------------------------------------- */
1174 * Get current word under cursor
1176 * @param esm status message window
1177 * @param srch mc_search object
1178 * @param word_start start word position
1180 * @return newly allocated string or NULL if no any words under cursor
1184 edit_collect_completions_get_current_word (edit_search_status_msg_t
* esm
, mc_search_t
* srch
,
1187 WEdit
*edit
= esm
->edit
;
1192 if (!mc_search_run (srch
, (void *) esm
, word_start
, edit
->buffer
.size
, &len
))
1195 temp
= g_string_sized_new (len
);
1197 for (i
= 0; i
< (off_t
) len
; i
++)
1201 chr
= edit_buffer_get_byte (&edit
->buffer
, word_start
+ i
);
1203 g_string_append_c (temp
, chr
);
1206 return g_string_free (temp
, temp
->len
== 0);
1209 /* --------------------------------------------------------------------------------------------- */
1210 /** collect the possible completions */
1213 edit_collect_completions (WEdit
* edit
, off_t word_start
, gsize word_len
,
1214 char *match_expr
, GString
** compl, gsize
* num
)
1222 off_t last_byte
, start
= -1;
1224 gboolean entire_file
;
1225 edit_search_status_msg_t esm
;
1228 srch
= mc_search_new (match_expr
, cp_source
);
1230 srch
= mc_search_new (match_expr
, NULL
);
1236 mc_config_get_bool (mc_global
.main_config
, CONFIG_APP_SECTION
,
1237 "editor_wordcompletion_collect_entire_file", 0);
1239 last_byte
= entire_file
? edit
->buffer
.size
: word_start
;
1241 srch
->search_type
= MC_SEARCH_T_REGEX
;
1242 srch
->is_case_sensitive
= TRUE
;
1243 srch
->search_fn
= edit_search_cmd_callback
;
1244 srch
->update_fn
= edit_search_update_callback
;
1248 esm
.offset
= entire_file
? 0 : word_start
;
1250 status_msg_init (STATUS_MSG (&esm
), _("Collect completions"), 1.0, simple_status_msg_init_cb
,
1251 edit_search_status_update_cb
, NULL
);
1253 current_word
= edit_collect_completions_get_current_word (&esm
, srch
, word_start
);
1255 temp
= g_string_new ("");
1257 /* collect max MAX_WORD_COMPLETIONS completions */
1258 while (mc_search_run (srch
, (void *) &esm
, start
+ 1, last_byte
, &len
))
1260 g_string_set_size (temp
, 0);
1261 start
= srch
->normal_offset
;
1263 /* add matched completion if not yet added */
1264 for (i
= 0; i
< len
; i
++)
1266 skip
= edit_buffer_get_byte (&edit
->buffer
, start
+ i
);
1270 /* skip current word */
1271 if (start
+ (off_t
) i
== word_start
)
1274 g_string_append_c (temp
, skip
);
1280 if (current_word
!= NULL
&& strcmp (current_word
, temp
->str
) == 0)
1285 for (i
= 0; i
< *num
; i
++)
1288 ((char *) &compl[i
]->str
[word_len
],
1289 (char *) &temp
->str
[word_len
], MAX (len
, compl[i
]->len
) - word_len
) == 0)
1291 GString
*this = compl[i
];
1292 for (++i
; i
< *num
; i
++)
1293 compl[i
- 1] = compl[i
];
1294 compl[*num
- 1] = this;
1296 break; /* skip it, already added */
1302 if (*num
== MAX_WORD_COMPLETIONS
)
1304 g_string_free (compl[0], TRUE
);
1305 for (i
= 1; i
< *num
; i
++)
1306 compl[i
- 1] = compl[i
];
1312 recoded
= str_convert_to_display (temp
->str
);
1314 if (recoded
&& recoded
->len
)
1315 g_string_assign (temp
, recoded
->str
);
1317 g_string_free (recoded
, TRUE
);
1320 compl[(*num
)++] = g_string_new_len (temp
->str
, temp
->len
);
1323 /* note the maximal length needed for the completion dialog */
1328 status_msg_deinit (STATUS_MSG (&esm
));
1329 mc_search_free (srch
);
1330 g_string_free (temp
, TRUE
);
1331 g_free (current_word
);
1336 /* --------------------------------------------------------------------------------------------- */
1339 edit_insert_column_of_text (WEdit
* edit
, unsigned char *data
, off_t size
, long width
,
1340 off_t
* start_pos
, off_t
* end_pos
, long *col1
, long *col2
)
1345 cursor
= edit
->buffer
.curs1
;
1346 col
= edit_get_col (edit
);
1348 for (i
= 0; i
< size
; i
++)
1350 if (data
[i
] != '\n')
1351 edit_insert (edit
, data
[i
]);
1353 { /* fill in and move to next line */
1357 if (edit_buffer_get_current_byte (&edit
->buffer
) != '\n')
1359 for (l
= width
- (edit_get_col (edit
) - col
); l
> 0; l
-= space_width
)
1360 edit_insert (edit
, ' ');
1362 for (p
= edit
->buffer
.curs1
;; p
++)
1364 if (p
== edit
->buffer
.size
)
1366 edit_cursor_move (edit
, edit
->buffer
.size
- edit
->buffer
.curs1
);
1367 edit_insert_ahead (edit
, '\n');
1371 if (edit_buffer_get_byte (&edit
->buffer
, p
) == '\n')
1377 edit_cursor_move (edit
, edit_move_forward3 (edit
, p
, col
, 0) - edit
->buffer
.curs1
);
1379 for (l
= col
- edit_get_col (edit
); l
>= space_width
; l
-= space_width
)
1380 edit_insert (edit
, ' ');
1385 *col2
= col
+ width
;
1386 *start_pos
= cursor
;
1387 *end_pos
= edit
->buffer
.curs1
;
1388 edit_cursor_move (edit
, cursor
- edit
->buffer
.curs1
);
1391 /* --------------------------------------------------------------------------------------------- */
1394 edit_macro_comparator (gconstpointer
* macro1
, gconstpointer
* macro2
)
1396 const macros_t
*m1
= (const macros_t
*) macro1
;
1397 const macros_t
*m2
= (const macros_t
*) macro2
;
1399 return m1
->hotkey
- m2
->hotkey
;
1402 /* --------------------------------------------------------------------------------------------- */
1405 edit_macro_sort_by_hotkey (void)
1407 if (macros_list
!= NULL
&& macros_list
->len
!= 0)
1408 g_array_sort (macros_list
, (GCompareFunc
) edit_macro_comparator
);
1411 /* --------------------------------------------------------------------------------------------- */
1414 edit_get_macro (WEdit
* edit
, int hotkey
, const macros_t
** macros
, guint
* indx
)
1416 const macros_t
*array_start
= &g_array_index (macros_list
, struct macros_t
, 0);
1418 macros_t search_macro
;
1422 search_macro
.hotkey
= hotkey
;
1423 result
= bsearch (&search_macro
, macros_list
->data
, macros_list
->len
,
1424 sizeof (macros_t
), (GCompareFunc
) edit_macro_comparator
);
1426 if (result
!= NULL
&& result
->macro
!= NULL
)
1428 *indx
= (result
- array_start
);
1436 /* --------------------------------------------------------------------------------------------- */
1437 /** returns FALSE on error */
1440 edit_delete_macro (WEdit
* edit
, int hotkey
)
1442 mc_config_t
*macros_config
= NULL
;
1443 const char *section_name
= "editor";
1444 gchar
*macros_fname
;
1447 const macros_t
*macros
= NULL
;
1449 /* clear array of actions for current hotkey */
1450 while (edit_get_macro (edit
, hotkey
, ¯os
, &indx
))
1452 if (macros
->macro
!= NULL
)
1453 g_array_free (macros
->macro
, TRUE
);
1455 g_array_remove_index (macros_list
, indx
);
1456 edit_macro_sort_by_hotkey ();
1459 macros_fname
= mc_config_get_full_path (MC_MACRO_FILE
);
1460 macros_config
= mc_config_init (macros_fname
, FALSE
);
1461 g_free (macros_fname
);
1463 if (macros_config
== NULL
)
1466 skeyname
= lookup_key_by_code (hotkey
);
1467 while (mc_config_del_key (macros_config
, section_name
, skeyname
))
1470 mc_config_save_file (macros_config
, NULL
);
1471 mc_config_deinit (macros_config
);
1475 /* --------------------------------------------------------------------------------------------- */
1477 * Callback for the iteration of objects in the 'editors' array.
1478 * Toggle syntax highlighting in editor object.
1480 * @param data probably WEdit object
1481 * @param user_data unused
1485 edit_syntax_onoff_cb (void *data
, void *user_data
)
1489 if (edit_widget_is_editor (CONST_WIDGET (data
)))
1491 WEdit
*edit
= (WEdit
*) data
;
1493 if (option_syntax_highlighting
)
1494 edit_load_syntax (edit
, NULL
, edit
->syntax_type
);
1495 edit
->force
|= REDRAW_PAGE
;
1499 /* --------------------------------------------------------------------------------------------- */
1501 * Callback for the iteration of objects in the 'editors' array.
1502 * Redraw editor object.
1504 * @param data probably WEdit object
1505 * @param user_data unused
1509 edit_redraw_page_cb (void *data
, void *user_data
)
1513 if (edit_widget_is_editor (CONST_WIDGET (data
)))
1514 ((WEdit
*) data
)->force
|= REDRAW_PAGE
;
1517 /* --------------------------------------------------------------------------------------------- */
1519 * Insert autocompleted word into editor.
1521 * @param edit editor object
1522 * @param completion word for completion
1523 * @param word_len offset from beginning for insert
1527 edit_complete_word_insert_recoded_completion (WEdit
* edit
, char *completion
, gsize word_len
)
1532 temp
= str_convert_to_input (completion
);
1534 for (completion
= temp
->str
+ word_len
; *completion
!= '\0'; completion
++)
1535 edit_insert (edit
, *completion
);
1536 g_string_free (temp
, TRUE
);
1538 for (completion
+= word_len
; *completion
!= '\0'; completion
++)
1539 edit_insert (edit
, *completion
);
1543 /* --------------------------------------------------------------------------------------------- */
1544 /*** public functions ****************************************************************************/
1545 /* --------------------------------------------------------------------------------------------- */
1548 edit_refresh_cmd (void)
1555 /* --------------------------------------------------------------------------------------------- */
1557 * Toggle syntax highlighting in all editor windows.
1559 * @param h root widget for all windows
1563 edit_syntax_onoff_cmd (WDialog
* h
)
1565 option_syntax_highlighting
= !option_syntax_highlighting
;
1566 g_list_foreach (h
->widgets
, edit_syntax_onoff_cb
, NULL
);
1570 /* --------------------------------------------------------------------------------------------- */
1572 * Toggle tabs showing in all editor windows.
1574 * @param h root widget for all windows
1578 edit_show_tabs_tws_cmd (WDialog
* h
)
1580 enable_show_tabs_tws
= !enable_show_tabs_tws
;
1581 g_list_foreach (h
->widgets
, edit_redraw_page_cb
, NULL
);
1585 /* --------------------------------------------------------------------------------------------- */
1587 * Toggle right margin showing in all editor windows.
1589 * @param h root widget for all windows
1593 edit_show_margin_cmd (WDialog
* h
)
1595 show_right_margin
= !show_right_margin
;
1596 g_list_foreach (h
->widgets
, edit_redraw_page_cb
, NULL
);
1600 /* --------------------------------------------------------------------------------------------- */
1602 * Toggle line numbers showing in all editor windows.
1604 * @param h root widget for all windows
1608 edit_show_numbers_cmd (WDialog
* h
)
1610 option_line_state
= !option_line_state
;
1611 option_line_state_width
= option_line_state
? LINE_STATE_WIDTH
: 0;
1612 g_list_foreach (h
->widgets
, edit_redraw_page_cb
, NULL
);
1616 /* --------------------------------------------------------------------------------------------- */
1619 edit_save_mode_cmd (void)
1621 char *str_result
= NULL
;
1623 const char *str
[] = {
1626 N_("&Do backups with following extension:")
1629 #ifdef HAVE_ASSERT_H
1630 assert (option_backup_ext
!= NULL
);
1636 for (i
= 0; i
< 3; i
++)
1641 quick_widget_t quick_widgets
[] = {
1643 QUICK_RADIO (3, str
, &option_save_mode
, &edit_save_mode_radio_id
),
1644 QUICK_INPUT (option_backup_ext
, "edit-backup-ext", &str_result
,
1645 &edit_save_mode_input_id
, FALSE
, FALSE
, INPUT_COMPLETE_NONE
),
1646 QUICK_SEPARATOR (TRUE
),
1647 QUICK_CHECKBOX (N_("Check &POSIX new line"), &option_check_nl_at_eof
, NULL
),
1648 QUICK_BUTTONS_OK_CANCEL
,
1653 quick_dialog_t qdlg
= {
1655 N_("Edit Save Mode"), "[Edit Save Mode]",
1656 quick_widgets
, edit_save_mode_callback
, NULL
1659 if (quick_dialog (&qdlg
) != B_CANCEL
)
1661 g_free (option_backup_ext
);
1662 option_backup_ext
= str_result
;
1667 /* --------------------------------------------------------------------------------------------- */
1670 edit_set_filename (WEdit
* edit
, const vfs_path_t
* name_vpath
)
1672 vfs_path_free (edit
->filename_vpath
);
1673 edit
->filename_vpath
= vfs_path_clone (name_vpath
);
1675 if (edit
->dir_vpath
== NULL
)
1676 edit
->dir_vpath
= vfs_path_clone (vfs_get_raw_current_dir ());
1679 /* --------------------------------------------------------------------------------------------- */
1680 /* Here we want to warn the users of overwriting an existing file,
1681 but only if they have made a change to the filename */
1682 /* returns TRUE on success */
1684 edit_save_as_cmd (WEdit
* edit
)
1686 /* This heads the 'Save As' dialog box */
1687 vfs_path_t
*exp_vpath
;
1689 int different_filename
= 0;
1691 if (!edit_check_newline (&edit
->buffer
))
1694 exp_vpath
= edit_get_save_file_as (edit
);
1695 edit_push_undo_action (edit
, KEY_PRESS
+ edit
->start_display
);
1697 if (exp_vpath
!= NULL
)
1699 if (vfs_path_len (exp_vpath
) == 0)
1705 if (!vfs_path_equal (edit
->filename_vpath
, exp_vpath
))
1710 if (mc_stat (exp_vpath
, &sb
) == 0 && !S_ISREG (sb
.st_mode
))
1712 edit_error_dialog (_("Save as"),
1714 ("Cannot save: destination is not a regular file")));
1718 different_filename
= 1;
1719 file
= mc_open (exp_vpath
, O_RDONLY
| O_BINARY
);
1723 /* the file exists */
1725 /* Overwrite the current file or cancel the operation */
1726 if (edit_query_dialog2
1728 _("A file already exists with this name"), _("&Overwrite"), _("&Cancel")))
1733 edit
->stat1
.st_mode
|= S_IWUSR
;
1735 save_lock
= lock_file (exp_vpath
);
1739 /* filenames equal, check if already locked */
1740 if (!edit
->locked
&& !edit
->delete_file
)
1741 save_lock
= lock_file (exp_vpath
);
1744 if (different_filename
)
1747 * Allow user to write into saved (under another name) file
1748 * even if original file had r/o user permissions.
1750 edit
->stat1
.st_mode
|= S_IWRITE
;
1753 rv
= edit_save_file (edit
, exp_vpath
);
1757 /* Successful, so unlock both files */
1758 if (different_filename
)
1761 unlock_file (exp_vpath
);
1763 edit
->locked
= unlock_file (edit
->filename_vpath
);
1767 if (edit
->locked
|| save_lock
)
1768 edit
->locked
= unlock_file (edit
->filename_vpath
);
1771 edit_set_filename (edit
, exp_vpath
);
1772 if (edit
->lb
!= LB_ASIS
)
1773 edit_reload (edit
, exp_vpath
);
1775 edit
->delete_file
= 0;
1776 if (different_filename
)
1777 edit_load_syntax (edit
, NULL
, edit
->syntax_type
);
1778 vfs_path_free (exp_vpath
);
1779 edit
->force
|= REDRAW_COMPLETELY
;
1782 edit_error_dialog (_("Save as"), get_sys_error (_("Cannot save file")));
1785 /* Failed, so maintain modify (not save) lock */
1787 unlock_file (exp_vpath
);
1794 vfs_path_free (exp_vpath
);
1795 edit
->force
|= REDRAW_COMPLETELY
;
1799 /* {{{ Macro stuff starts here */
1800 /* --------------------------------------------------------------------------------------------- */
1803 edit_delete_macro_cmd (WEdit
* edit
)
1807 hotkey
= editcmd_dialog_raw_key_query (_("Delete macro"), _("Press macro hotkey:"), TRUE
);
1809 if (hotkey
!= 0 && !edit_delete_macro (edit
, hotkey
))
1810 message (D_ERROR
, _("Delete macro"), _("Macro not deleted"));
1813 /* --------------------------------------------------------------------------------------------- */
1815 /** returns FALSE on error */
1817 edit_execute_macro (WEdit
* edit
, int hotkey
)
1819 gboolean res
= FALSE
;
1823 const macros_t
*macros
;
1826 if (edit_get_macro (edit
, hotkey
, ¯os
, &indx
) &&
1827 macros
->macro
!= NULL
&& macros
->macro
->len
!= 0)
1831 edit
->force
|= REDRAW_PAGE
;
1833 for (i
= 0; i
< macros
->macro
->len
; i
++)
1835 const macro_action_t
*m_act
;
1837 m_act
= &g_array_index (macros
->macro
, struct macro_action_t
, i
);
1838 edit_execute_cmd (edit
, m_act
->action
, m_act
->ch
);
1847 /* --------------------------------------------------------------------------------------------- */
1849 /** returns FALSE on error */
1851 edit_store_macro_cmd (WEdit
* edit
)
1855 GString
*marcros_string
;
1856 mc_config_t
*macros_config
= NULL
;
1857 const char *section_name
= "editor";
1858 gchar
*macros_fname
;
1859 GArray
*macros
; /* current macro */
1861 gboolean have_macro
= FALSE
;
1862 char *skeyname
= NULL
;
1865 editcmd_dialog_raw_key_query (_("Save macro"), _("Press the macro's new hotkey:"), TRUE
);
1866 if (hotkey
== ESC_CHAR
)
1869 tmp_act
= keybind_lookup_keymap_command (editor_map
, hotkey
);
1871 /* return FALSE if try assign macro into restricted hotkeys */
1872 if (tmp_act
== CK_MacroStartRecord
1873 || tmp_act
== CK_MacroStopRecord
|| tmp_act
== CK_MacroStartStopRecord
)
1876 edit_delete_macro (edit
, hotkey
);
1878 macros_fname
= mc_config_get_full_path (MC_MACRO_FILE
);
1879 macros_config
= mc_config_init (macros_fname
, FALSE
);
1880 g_free (macros_fname
);
1882 if (macros_config
== NULL
)
1885 edit_push_undo_action (edit
, KEY_PRESS
+ edit
->start_display
);
1887 marcros_string
= g_string_sized_new (250);
1888 macros
= g_array_new (TRUE
, FALSE
, sizeof (macro_action_t
));
1890 skeyname
= lookup_key_by_code (hotkey
);
1892 for (i
= 0; i
< macro_index
; i
++)
1894 macro_action_t m_act
;
1895 const char *action_name
;
1897 action_name
= keybind_lookup_actionname (record_macro_buf
[i
].action
);
1899 if (action_name
== NULL
)
1902 m_act
.action
= record_macro_buf
[i
].action
;
1903 m_act
.ch
= record_macro_buf
[i
].ch
;
1904 g_array_append_val (macros
, m_act
);
1906 g_string_append_printf (marcros_string
, "%s:%i;", action_name
,
1907 (int) record_macro_buf
[i
].ch
);
1912 macro
.hotkey
= hotkey
;
1913 macro
.macro
= macros
;
1914 g_array_append_val (macros_list
, macro
);
1915 mc_config_set_string (macros_config
, section_name
, skeyname
, marcros_string
->str
);
1918 mc_config_del_key (macros_config
, section_name
, skeyname
);
1921 edit_macro_sort_by_hotkey ();
1923 g_string_free (marcros_string
, TRUE
);
1924 mc_config_save_file (macros_config
, NULL
);
1925 mc_config_deinit (macros_config
);
1929 /* --------------------------------------------------------------------------------------------- */
1932 edit_repeat_macro_cmd (WEdit
* edit
)
1939 f
= input_dialog (_("Repeat last commands"), _("Repeat times:"), MC_HISTORY_EDIT_REPEAT
, NULL
,
1940 INPUT_COMPLETE_NONE
);
1941 if (f
== NULL
|| *f
== '\0')
1947 count_repeat
= strtol (f
, &error
, 0);
1957 edit_push_undo_action (edit
, KEY_PRESS
+ edit
->start_display
);
1958 edit
->force
|= REDRAW_PAGE
;
1960 for (j
= 0; j
< count_repeat
; j
++)
1961 for (i
= 0; i
< macro_index
; i
++)
1962 edit_execute_cmd (edit
, record_macro_buf
[i
].action
, record_macro_buf
[i
].ch
);
1963 edit_update_screen (edit
);
1967 /* --------------------------------------------------------------------------------------------- */
1968 /** return FALSE on error */
1971 edit_load_macro_cmd (WEdit
* edit
)
1973 mc_config_t
*macros_config
= NULL
;
1974 gchar
**profile_keys
, **keys
;
1975 gchar
**values
, **curr_values
;
1976 const char *section_name
= "editor";
1977 gchar
*macros_fname
;
1981 if (macros_list
== NULL
|| macros_list
->len
!= 0)
1984 macros_fname
= mc_config_get_full_path (MC_MACRO_FILE
);
1985 macros_config
= mc_config_init (macros_fname
, TRUE
);
1986 g_free (macros_fname
);
1988 if (macros_config
== NULL
)
1991 keys
= mc_config_get_keys (macros_config
, section_name
, NULL
);
1993 for (profile_keys
= keys
; *profile_keys
!= NULL
; profile_keys
++)
1996 gboolean have_macro
= FALSE
;
2000 macros
= g_array_new (TRUE
, FALSE
, sizeof (macro_action_t
));
2001 values
= mc_config_get_string_list (macros_config
, section_name
, *profile_keys
, NULL
);
2002 hotkey
= lookup_key (*profile_keys
, NULL
);
2004 for (curr_values
= values
; *curr_values
!= NULL
&& *curr_values
[0] != '\0'; curr_values
++)
2006 char **macro_pair
= NULL
;
2008 macro_pair
= g_strsplit (*curr_values
, ":", 2);
2009 if (macro_pair
!= NULL
)
2011 macro_action_t m_act
;
2012 if (macro_pair
[0] == NULL
|| macro_pair
[0][0] == '\0')
2016 m_act
.action
= keybind_lookup_action (macro_pair
[0]);
2017 MC_PTR_FREE (macro_pair
[0]);
2019 if (macro_pair
[1] == NULL
|| macro_pair
[1][0] == '\0')
2023 m_act
.ch
= strtol (macro_pair
[1], NULL
, 0);
2024 MC_PTR_FREE (macro_pair
[1]);
2026 if (m_act
.action
!= 0)
2028 /* a shell command */
2029 if ((m_act
.action
/ CK_PipeBlock (0)) == 1)
2031 m_act
.action
= CK_PipeBlock (0) + (m_act
.ch
> 0 ? m_act
.ch
: 0);
2034 g_array_append_val (macros
, m_act
);
2037 g_strfreev (macro_pair
);
2043 macro
.hotkey
= hotkey
;
2044 macro
.macro
= macros
;
2045 g_array_append_val (macros_list
, macro
);
2047 g_strfreev (values
);
2050 mc_config_deinit (macros_config
);
2051 edit_macro_sort_by_hotkey ();
2055 /* }}} Macro stuff end here */
2057 /* --------------------------------------------------------------------------------------------- */
2058 /** returns TRUE on success */
2061 edit_save_confirm_cmd (WEdit
* edit
)
2063 if (edit
->filename_vpath
== NULL
)
2064 return edit_save_as_cmd (edit
);
2066 if (!edit_check_newline (&edit
->buffer
))
2069 if (edit_confirm_save
)
2074 f
= g_strdup_printf (_("Confirm save file: \"%s\""),
2075 vfs_path_as_str (edit
->filename_vpath
));
2076 ok
= (edit_query_dialog2 (_("Save file"), f
, _("&Save"), _("&Cancel")) == 0);
2081 return edit_save_cmd (edit
);
2084 /* --------------------------------------------------------------------------------------------- */
2086 * Ask file to edit and load it.
2088 * @return TRUE on success or cancel of ask.
2092 edit_load_cmd (WDialog
* h
)
2095 gboolean ret
= TRUE
; /* possible cancel */
2097 exp
= input_expand_dialog (_("Load"), _("Enter file name:"),
2098 MC_HISTORY_EDIT_LOAD
, INPUT_LAST_TEXT
,
2099 INPUT_COMPLETE_FILENAMES
| INPUT_COMPLETE_CD
);
2101 if (exp
!= NULL
&& *exp
!= '\0')
2103 vfs_path_t
*exp_vpath
;
2105 exp_vpath
= vfs_path_from_str (exp
);
2106 ret
= edit_load_file_from_filename (h
, exp_vpath
);
2107 vfs_path_free (exp_vpath
);
2115 /* --------------------------------------------------------------------------------------------- */
2117 * Load syntax file to edit.
2119 * @return TRUE on success
2123 edit_load_syntax_file (WDialog
* h
)
2125 vfs_path_t
*extdir_vpath
;
2127 gboolean ret
= FALSE
;
2129 if (geteuid () == 0)
2130 dir
= query_dialog (_("Syntax file edit"),
2131 _("Which syntax file you want to edit?"), D_NORMAL
, 2,
2132 _("&User"), _("&System wide"));
2135 vfs_path_build_filename (mc_global
.sysconfig_dir
, "syntax", "Syntax", (char *) NULL
);
2136 if (!exist_file (vfs_path_get_last_path_str (extdir_vpath
)))
2138 vfs_path_free (extdir_vpath
);
2140 vfs_path_build_filename (mc_global
.share_data_dir
, "syntax", "Syntax", (char *) NULL
);
2145 vfs_path_t
*user_syntax_file_vpath
;
2147 user_syntax_file_vpath
= mc_config_get_full_vpath (EDIT_SYNTAX_FILE
);
2148 check_for_default (extdir_vpath
, user_syntax_file_vpath
);
2149 ret
= edit_load_file_from_filename (h
, user_syntax_file_vpath
);
2150 vfs_path_free (user_syntax_file_vpath
);
2153 ret
= edit_load_file_from_filename (h
, extdir_vpath
);
2155 vfs_path_free (extdir_vpath
);
2160 /* --------------------------------------------------------------------------------------------- */
2162 * Load menu file to edit.
2164 * @return TRUE on success
2168 edit_load_menu_file (WDialog
* h
)
2170 vfs_path_t
*buffer_vpath
;
2171 vfs_path_t
*menufile_vpath
;
2176 dir
= query_dialog (_("Menu edit"),
2177 _("Which menu file do you want to edit?"), D_NORMAL
,
2178 geteuid () != 0 ? 2 : 3, _("&Local"), _("&User"), _("&System wide"));
2181 vfs_path_build_filename (mc_global
.sysconfig_dir
, EDIT_GLOBAL_MENU
, (char *) NULL
);
2182 if (!exist_file (vfs_path_get_last_path_str (menufile_vpath
)))
2184 vfs_path_free (menufile_vpath
);
2186 vfs_path_build_filename (mc_global
.share_data_dir
, EDIT_GLOBAL_MENU
, (char *) 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
);
2204 vfs_path_build_filename (mc_global
.sysconfig_dir
, EDIT_GLOBAL_MENU
, (char *) NULL
);
2205 if (!exist_file (vfs_path_get_last_path_str (buffer_vpath
)))
2207 vfs_path_free (buffer_vpath
);
2209 vfs_path_build_filename (mc_global
.share_data_dir
, EDIT_GLOBAL_MENU
, (char *) NULL
);
2214 vfs_path_free (menufile_vpath
);
2218 ret
= edit_load_file_from_filename (h
, buffer_vpath
);
2220 vfs_path_free (buffer_vpath
);
2221 vfs_path_free (menufile_vpath
);
2226 /* --------------------------------------------------------------------------------------------- */
2228 * Close window with opened file.
2230 * @return TRUE if file was closed.
2234 edit_close_cmd (WEdit
* edit
)
2238 ret
= (edit
!= NULL
) && edit_ok_to_exit (edit
);
2242 WDialog
*h
= WIDGET (edit
)->owner
;
2244 if (edit
->locked
!= 0)
2245 unlock_file (edit
->filename_vpath
);
2249 if (edit_widget_is_editor (CONST_WIDGET (h
->current
->data
)))
2250 edit
= (WEdit
*) h
->current
->data
;
2253 edit
= find_editor (h
);
2255 dlg_set_top_widget (edit
);
2260 edit
->force
|= REDRAW_COMPLETELY
;
2265 /* --------------------------------------------------------------------------------------------- */
2267 if mark2 is -1 then marking is from mark1 to the cursor.
2268 Otherwise its between the markers. This handles this.
2269 Returns FALSE if no text is marked.
2273 eval_marks (WEdit
* edit
, off_t
* start_mark
, off_t
* end_mark
)
2277 if (edit
->mark1
== edit
->mark2
)
2279 *start_mark
= *end_mark
= 0;
2280 edit
->column2
= edit
->column1
= 0;
2284 if (edit
->end_mark_curs
< 0)
2285 end_mark_curs
= edit
->buffer
.curs1
;
2287 end_mark_curs
= edit
->end_mark_curs
;
2289 if (edit
->mark2
>= 0)
2291 *start_mark
= MIN (edit
->mark1
, edit
->mark2
);
2292 *end_mark
= MAX (edit
->mark1
, edit
->mark2
);
2296 *start_mark
= MIN (edit
->mark1
, end_mark_curs
);
2297 *end_mark
= MAX (edit
->mark1
, end_mark_curs
);
2298 edit
->column2
= edit
->curs_col
+ edit
->over_col
;
2301 if (edit
->column_highlight
2302 && ((edit
->mark1
> end_mark_curs
&& edit
->column1
< edit
->column2
)
2303 || (edit
->mark1
< end_mark_curs
&& edit
->column1
> edit
->column2
)))
2305 off_t start_bol
, start_eol
;
2306 off_t end_bol
, end_eol
;
2310 start_bol
= edit_buffer_get_bol (&edit
->buffer
, *start_mark
);
2311 start_eol
= edit_buffer_get_eol (&edit
->buffer
, start_bol
- 1) + 1;
2312 end_bol
= edit_buffer_get_bol (&edit
->buffer
, *end_mark
);
2313 end_eol
= edit_buffer_get_eol (&edit
->buffer
, *end_mark
);
2314 col1
= MIN (edit
->column1
, edit
->column2
);
2315 col2
= MAX (edit
->column1
, edit
->column2
);
2317 diff1
= edit_move_forward3 (edit
, start_bol
, col2
, 0) -
2318 edit_move_forward3 (edit
, start_bol
, col1
, 0);
2319 diff2
= edit_move_forward3 (edit
, end_bol
, col2
, 0) -
2320 edit_move_forward3 (edit
, end_bol
, col1
, 0);
2322 *start_mark
-= diff1
;
2324 *start_mark
= MAX (*start_mark
, start_eol
);
2325 *end_mark
= MIN (*end_mark
, end_eol
);
2330 /* --------------------------------------------------------------------------------------------- */
2333 edit_block_copy_cmd (WEdit
* edit
)
2335 off_t start_mark
, end_mark
, current
= edit
->buffer
.curs1
;
2336 off_t mark1
= 0, mark2
= 0;
2337 long c1
= 0, c2
= 0;
2339 unsigned char *copy_buf
;
2341 edit_update_curs_col (edit
);
2342 if (!eval_marks (edit
, &start_mark
, &end_mark
))
2345 copy_buf
= edit_get_block (edit
, start_mark
, end_mark
, &size
);
2347 /* all that gets pushed are deletes hence little space is used on the stack */
2349 edit_push_markers (edit
);
2351 if (edit
->column_highlight
)
2355 col_delta
= labs (edit
->column2
- edit
->column1
);
2356 edit_insert_column_of_text (edit
, copy_buf
, size
, col_delta
, &mark1
, &mark2
, &c1
, &c2
);
2360 int size_orig
= size
;
2363 edit_insert_ahead (edit
, copy_buf
[size
]);
2365 /* Place cursor at the end of text selection */
2366 if (option_cursor_after_inserted_block
)
2367 edit_cursor_move (edit
, size_orig
);
2371 edit_scroll_screen_over_cursor (edit
);
2373 if (edit
->column_highlight
)
2374 edit_set_markers (edit
, edit
->buffer
.curs1
, mark2
, c1
, c2
);
2375 else if (start_mark
< current
&& end_mark
> current
)
2376 edit_set_markers (edit
, start_mark
, end_mark
+ end_mark
- start_mark
, 0, 0);
2378 edit
->force
|= REDRAW_PAGE
;
2382 /* --------------------------------------------------------------------------------------------- */
2385 edit_block_move_cmd (WEdit
* edit
)
2388 unsigned char *copy_buf
= NULL
;
2389 off_t start_mark
, end_mark
;
2391 if (!eval_marks (edit
, &start_mark
, &end_mark
))
2394 if (!edit
->column_highlight
&& edit
->buffer
.curs1
> start_mark
&& edit
->buffer
.curs1
< end_mark
)
2397 if (edit
->mark2
< 0)
2398 edit_mark_cmd (edit
, FALSE
);
2399 edit_push_markers (edit
);
2401 if (edit
->column_highlight
)
2405 long c1
, c2
, b_width
;
2408 c1
= MIN (edit
->column1
, edit
->column2
);
2409 c2
= MAX (edit
->column1
, edit
->column2
);
2412 edit_update_curs_col (edit
);
2415 x2
= x
+ edit
->over_col
;
2417 /* do nothing when cursor inside first line of selected area */
2418 if ((edit_buffer_get_eol (&edit
->buffer
, edit
->buffer
.curs1
) ==
2419 edit_buffer_get_eol (&edit
->buffer
, start_mark
)) && x2
> c1
&& x2
<= c2
)
2422 if (edit
->buffer
.curs1
> start_mark
2423 && edit
->buffer
.curs1
< edit_buffer_get_eol (&edit
->buffer
, end_mark
))
2427 else if (x
> c1
&& x
<= c2
)
2430 /* save current selection into buffer */
2431 copy_buf
= edit_get_block (edit
, start_mark
, end_mark
, &size
);
2433 /* remove current selection */
2434 edit_block_delete_cmd (edit
);
2436 edit
->over_col
= MAX (0, edit
->over_col
- b_width
);
2437 /* calculate the cursor pos after delete block */
2438 current
= edit_move_forward3 (edit
, edit_buffer_get_current_bol (&edit
->buffer
), x
, 0);
2439 edit_cursor_move (edit
, current
- edit
->buffer
.curs1
);
2440 edit_scroll_screen_over_cursor (edit
);
2442 /* add TWS if need before block insertion */
2443 if (option_cursor_beyond_eol
&& edit
->over_col
> 0)
2444 edit_insert_over (edit
);
2446 edit_insert_column_of_text (edit
, copy_buf
, size
, b_width
, &mark1
, &mark2
, &c1
, &c2
);
2447 edit_set_markers (edit
, mark1
, mark2
, c1
, c2
);
2451 off_t count
, count_orig
;
2453 current
= edit
->buffer
.curs1
;
2454 copy_buf
= g_malloc0 (end_mark
- start_mark
);
2455 edit_cursor_move (edit
, start_mark
- edit
->buffer
.curs1
);
2456 edit_scroll_screen_over_cursor (edit
);
2458 for (count
= start_mark
; count
< end_mark
; count
++)
2459 copy_buf
[end_mark
- count
- 1] = edit_delete (edit
, TRUE
);
2461 edit_scroll_screen_over_cursor (edit
);
2462 edit_cursor_move (edit
,
2463 current
- edit
->buffer
.curs1
-
2464 (((current
- edit
->buffer
.curs1
) > 0) ? end_mark
- start_mark
: 0));
2465 edit_scroll_screen_over_cursor (edit
);
2467 while (count
-- > start_mark
)
2468 edit_insert_ahead (edit
, copy_buf
[end_mark
- count
- 1]);
2470 edit_set_markers (edit
, edit
->buffer
.curs1
, edit
->buffer
.curs1
+ end_mark
- start_mark
, 0,
2473 /* Place cursor at the end of text selection */
2474 if (option_cursor_after_inserted_block
)
2475 edit_cursor_move (edit
, count_orig
- start_mark
);
2478 edit_scroll_screen_over_cursor (edit
);
2480 edit
->force
|= REDRAW_PAGE
;
2483 /* --------------------------------------------------------------------------------------------- */
2484 /** returns 1 if canceelled by user */
2487 edit_block_delete_cmd (WEdit
* edit
)
2489 off_t start_mark
, end_mark
;
2491 if (eval_marks (edit
, &start_mark
, &end_mark
))
2492 return edit_block_delete (edit
);
2494 edit_delete_line (edit
);
2498 /* --------------------------------------------------------------------------------------------- */
2499 /** call with edit = 0 before shutdown to close memory leaks */
2502 edit_replace_cmd (WEdit
* edit
, gboolean again
)
2504 /* 1 = search string, 2 = replace with */
2505 static char *saved1
= NULL
; /* saved default[123] */
2506 static char *saved2
= NULL
;
2507 char *input1
= NULL
; /* user input from the dialog */
2508 char *input2
= NULL
;
2509 GString
*input2_str
= NULL
;
2512 long times_replaced
= 0;
2513 gboolean once_found
= FALSE
;
2514 edit_search_status_msg_t esm
;
2518 MC_PTR_FREE (saved1
);
2519 MC_PTR_FREE (saved2
);
2523 edit
->force
|= REDRAW_COMPLETELY
;
2525 if (again
&& !saved1
&& !saved2
)
2530 input1
= g_strdup (saved1
? saved1
: "");
2531 input2
= g_strdup (saved2
? saved2
: "");
2535 char *tmp_inp1
, *tmp_inp2
;
2537 disp1
= edit_replace_cmd__conv_to_display (saved1
? saved1
: "");
2538 disp2
= edit_replace_cmd__conv_to_display (saved2
? saved2
: "");
2540 edit_push_undo_action (edit
, KEY_PRESS
+ edit
->start_display
);
2542 editcmd_dialog_replace_show (edit
, disp1
, disp2
, &input1
, &input2
);
2547 if (input1
== NULL
|| *input1
== '\0')
2549 edit
->force
= REDRAW_COMPLETELY
;
2555 input1
= edit_replace_cmd__conv_to_input (input1
);
2556 input2
= edit_replace_cmd__conv_to_input (input2
);
2560 g_free (saved1
), saved1
= g_strdup (input1
);
2561 g_free (saved2
), saved2
= g_strdup (input2
);
2563 mc_search_free (edit
->search
);
2564 edit
->search
= NULL
;
2567 input2_str
= g_string_new (input2
);
2569 if (edit
->search
== NULL
)
2572 edit
->search
= mc_search_new (input1
, cp_source
);
2574 edit
->search
= mc_search_new (input1
, NULL
);
2576 if (edit
->search
== NULL
)
2578 edit
->search_start
= edit
->buffer
.curs1
;
2581 edit
->search
->search_type
= edit_search_options
.type
;
2583 edit
->search
->is_all_charsets
= edit_search_options
.all_codepages
;
2585 edit
->search
->is_case_sensitive
= edit_search_options
.case_sens
;
2586 edit
->search
->whole_words
= edit_search_options
.whole_words
;
2587 edit
->search
->search_fn
= edit_search_cmd_callback
;
2588 edit
->search
->update_fn
= edit_search_update_callback
;
2589 edit
->search_line_type
= edit_get_search_line_type (edit
->search
);
2590 edit_search_fix_search_start_if_selection (edit
);
2593 if (edit
->found_len
&& edit
->search_start
== edit
->found_start
+ 1
2594 && edit_search_options
.backwards
)
2595 edit
->search_start
--;
2597 if (edit
->found_len
&& edit
->search_start
== edit
->found_start
- 1
2598 && !edit_search_options
.backwards
)
2599 edit
->search_start
++;
2603 esm
.offset
= edit
->search_start
;
2605 status_msg_init (STATUS_MSG (&esm
), _("Search"), 1.0, simple_status_msg_init_cb
,
2606 edit_search_status_update_cb
, NULL
);
2612 if (!editcmd_find (&esm
, &len
))
2614 if (!(edit
->search
->error
== MC_SEARCH_E_OK
||
2615 (once_found
&& edit
->search
->error
== MC_SEARCH_E_NOTFOUND
))
2616 && edit
->search
->error_str
!= NULL
)
2617 edit_query_dialog (_("Search"), edit
->search
->error_str
);
2623 edit
->search_start
= edit
->search
->normal_offset
;
2624 /*returns negative on not found or error in pattern */
2626 if ((edit
->search_start
>= 0) && (edit
->search_start
< edit
->buffer
.size
))
2631 edit
->found_start
= edit
->search_start
;
2632 i
= edit
->found_len
= len
;
2634 edit_cursor_move (edit
, edit
->search_start
- edit
->buffer
.curs1
);
2635 edit_scroll_screen_over_cursor (edit
);
2637 if (edit
->replace_mode
== 0)
2642 l
= edit
->curs_row
- WIDGET (edit
)->lines
/ 3;
2644 edit_scroll_downward (edit
, l
);
2646 edit_scroll_upward (edit
, -l
);
2648 edit_scroll_screen_over_cursor (edit
);
2649 edit
->force
|= REDRAW_PAGE
;
2650 edit_render_keypress (edit
);
2652 /*so that undo stops at each query */
2653 edit_push_key_press (edit
);
2654 /* and prompt 2/3 down */
2655 disp1
= edit_replace_cmd__conv_to_display (saved1
);
2656 disp2
= edit_replace_cmd__conv_to_display (saved2
);
2657 prompt
= editcmd_dialog_replace_prompt_show (edit
, disp1
, disp2
, -1, -1);
2661 if (prompt
== B_REPLACE_ALL
)
2662 edit
->replace_mode
= 1;
2663 else if (prompt
== B_SKIP_REPLACE
)
2665 if (edit_search_options
.backwards
)
2666 edit
->search_start
--;
2668 edit
->search_start
++;
2669 continue; /* loop */
2671 else if (prompt
== B_CANCEL
)
2673 edit
->replace_mode
= -1;
2678 repl_str
= mc_search_prepare_replace_str (edit
->search
, input2_str
);
2680 if (edit
->search
->error
!= MC_SEARCH_E_OK
)
2682 if (edit
->search
->error_str
!= NULL
)
2683 edit_error_dialog (_("Replace"), edit
->search
->error_str
);
2685 g_string_free (repl_str
, TRUE
);
2689 /* delete then insert new */
2690 for (i
= 0; i
< len
; i
++)
2691 edit_delete (edit
, TRUE
);
2693 for (i
= 0; i
< repl_str
->len
; i
++)
2694 edit_insert (edit
, repl_str
->str
[i
]);
2696 edit
->found_len
= repl_str
->len
;
2697 g_string_free (repl_str
, TRUE
);
2700 /* so that we don't find the same string again */
2701 if (edit_search_options
.backwards
)
2703 edit
->search_start
--;
2707 edit
->search_start
+= edit
->found_len
+ (len
== 0 ? 1 : 0);
2709 if (edit
->search_start
>= edit
->buffer
.size
)
2713 edit_scroll_screen_over_cursor (edit
);
2717 /* try and find from right here for next search */
2718 edit
->search_start
= edit
->buffer
.curs1
;
2719 edit_update_curs_col (edit
);
2721 edit
->force
|= REDRAW_PAGE
;
2722 edit_render_keypress (edit
);
2724 if (times_replaced
== 0)
2725 query_dialog (_("Replace"), _(STR_E_NOTFOUND
), D_NORMAL
, 1, _("&OK"));
2729 while (edit
->replace_mode
>= 0);
2731 status_msg_deinit (STATUS_MSG (&esm
));
2732 edit_scroll_screen_over_cursor (edit
);
2733 edit
->force
|= REDRAW_COMPLETELY
;
2734 edit_render_keypress (edit
);
2736 if ((edit
->replace_mode
== 1) && (times_replaced
!= 0))
2737 message (D_NORMAL
, _("Replace"), _("%ld replacements made"), times_replaced
);
2742 if (input2_str
!= NULL
)
2743 g_string_free (input2_str
, TRUE
);
2746 /* --------------------------------------------------------------------------------------------- */
2749 edit_search_cmd_callback (const void *user_data
, gsize char_offset
, int *current_char
)
2751 WEdit
*edit
= ((const edit_search_status_msg_t
*) user_data
)->edit
;
2753 *current_char
= edit_buffer_get_byte (&edit
->buffer
, (off_t
) char_offset
);
2754 return MC_SEARCH_CB_OK
;
2757 /* --------------------------------------------------------------------------------------------- */
2760 edit_search_update_callback (const void *user_data
, gsize char_offset
)
2762 status_msg_t
*sm
= STATUS_MSG (user_data
);
2764 ((edit_search_status_msg_t
*) sm
)->offset
= (off_t
) char_offset
;
2766 return (sm
->update (sm
) == B_CANCEL
? MC_SEARCH_CB_ABORT
: MC_SEARCH_CB_OK
);
2769 /* --------------------------------------------------------------------------------------------- */
2772 edit_search_cmd (WEdit
* edit
, gboolean again
)
2780 else if (edit
->last_search_string
!= NULL
)
2781 edit_do_search (edit
);
2784 /* find last search string in history */
2787 history
= history_get (MC_HISTORY_SHARED_SEARCH
);
2788 if (history
!= NULL
&& history
->data
!= NULL
)
2790 edit
->last_search_string
= (char *) history
->data
;
2791 history
->data
= NULL
;
2792 history
= g_list_first (history
);
2793 g_list_free_full (history
, g_free
);
2796 edit
->search
= mc_search_new (edit
->last_search_string
, cp_source
);
2798 edit
->search
= mc_search_new (edit
->last_search_string
, NULL
);
2800 if (edit
->search
== NULL
)
2802 /* if not... then ask for an expression */
2803 MC_PTR_FREE (edit
->last_search_string
);
2808 edit
->search
->search_type
= edit_search_options
.type
;
2810 edit
->search
->is_all_charsets
= edit_search_options
.all_codepages
;
2812 edit
->search
->is_case_sensitive
= edit_search_options
.case_sens
;
2813 edit
->search
->whole_words
= edit_search_options
.whole_words
;
2814 edit
->search
->search_fn
= edit_search_cmd_callback
;
2815 edit
->search
->update_fn
= edit_search_update_callback
;
2816 edit
->search_line_type
= edit_get_search_line_type (edit
->search
);
2817 edit_do_search (edit
);
2822 /* if not... then ask for an expression */
2823 MC_PTR_FREE (edit
->last_search_string
);
2830 /* --------------------------------------------------------------------------------------------- */
2832 * Check if it's OK to close the file. If there are unsaved changes, ask user.
2834 * @return TRUE if it's OK to exit, FALSE to continue editing.
2838 edit_ok_to_exit (WEdit
* edit
)
2840 const char *fname
= N_("[NoName]");
2844 if (!edit
->modified
)
2847 if (edit
->filename_vpath
!= NULL
)
2848 fname
= vfs_path_as_str (edit
->filename_vpath
);
2854 if (!mc_global
.midnight_shutdown
)
2858 msg
= g_strdup_printf (_("File %s was modified.\nSave before close?"), fname
);
2859 act
= edit_query_dialog3 (_("Close file"), msg
, _("&Yes"), _("&No"), _("&Cancel"));
2863 msg
= g_strdup_printf (_("Midnight Commander is being shut down.\nSave modified file %s?"),
2865 act
= edit_query_dialog2 (_("Quit"), msg
, _("&Yes"), _("&No"));
2877 if (!mc_global
.midnight_shutdown
&& !edit_check_newline (&edit
->buffer
))
2879 edit_push_markers (edit
);
2880 edit_set_markers (edit
, 0, 0, 0, 0);
2881 if (!edit_save_cmd (edit
) || mc_global
.midnight_shutdown
)
2882 return mc_global
.midnight_shutdown
;
2887 case 2: /* Cancel quit */
2895 /* --------------------------------------------------------------------------------------------- */
2896 /** save block, returns TRUE on success */
2899 edit_save_block (WEdit
* edit
, const char *filename
, off_t start
, off_t finish
)
2905 vpath
= vfs_path_from_str (filename
);
2906 file
= mc_open (vpath
, O_CREAT
| O_WRONLY
| O_TRUNC
,
2907 S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IROTH
| O_BINARY
);
2908 vfs_path_free (vpath
);
2912 if (edit
->column_highlight
)
2916 r
= mc_write (file
, VERTICAL_MAGIC
, sizeof (VERTICAL_MAGIC
));
2919 unsigned char *block
, *p
;
2921 p
= block
= edit_get_block (edit
, start
, finish
, &len
);
2924 r
= mc_write (file
, p
, len
);
2939 len
= finish
- start
;
2940 buf
= g_malloc0 (TEMP_BUF_LEN
);
2941 while (start
!= finish
)
2943 end
= MIN (finish
, start
+ TEMP_BUF_LEN
);
2944 for (; i
< end
; i
++)
2945 buf
[i
- start
] = edit_buffer_get_byte (&edit
->buffer
, i
);
2946 len
-= mc_write (file
, (char *) buf
, end
- start
);
2956 /* --------------------------------------------------------------------------------------------- */
2959 edit_paste_from_history (WEdit
* edit
)
2962 edit_error_dialog (_("Error"), _("This function is not implemented"));
2965 /* --------------------------------------------------------------------------------------------- */
2968 edit_copy_to_X_buf_cmd (WEdit
* edit
)
2970 off_t start_mark
, end_mark
;
2972 if (!eval_marks (edit
, &start_mark
, &end_mark
))
2975 if (!edit_save_block_to_clip_file (edit
, start_mark
, end_mark
))
2977 edit_error_dialog (_("Copy to clipboard"), get_sys_error (_("Unable to save to file")));
2980 /* try use external clipboard utility */
2981 mc_event_raise (MCEVENT_GROUP_CORE
, "clipboard_file_to_ext_clip", NULL
);
2983 if (option_drop_selection_on_copy
)
2984 edit_mark_cmd (edit
, TRUE
);
2989 /* --------------------------------------------------------------------------------------------- */
2992 edit_cut_to_X_buf_cmd (WEdit
* edit
)
2994 off_t start_mark
, end_mark
;
2996 if (!eval_marks (edit
, &start_mark
, &end_mark
))
2999 if (!edit_save_block_to_clip_file (edit
, start_mark
, end_mark
))
3001 edit_error_dialog (_("Cut to clipboard"), _("Unable to save to file"));
3004 /* try use external clipboard utility */
3005 mc_event_raise (MCEVENT_GROUP_CORE
, "clipboard_file_to_ext_clip", NULL
);
3007 edit_block_delete_cmd (edit
);
3008 edit_mark_cmd (edit
, TRUE
);
3013 /* --------------------------------------------------------------------------------------------- */
3016 edit_paste_from_X_buf_cmd (WEdit
* edit
)
3021 /* try use external clipboard utility */
3022 mc_event_raise (MCEVENT_GROUP_CORE
, "clipboard_file_from_ext_clip", NULL
);
3023 tmp
= mc_config_get_full_vpath (EDIT_CLIP_FILE
);
3024 ret
= (edit_insert_file (edit
, tmp
) >= 0);
3025 vfs_path_free (tmp
);
3030 /* --------------------------------------------------------------------------------------------- */
3032 * Ask user for the line and go to that line.
3033 * Negative numbers mean line from the end (i.e. -1 is the last line).
3037 edit_goto_cmd (WEdit
* edit
)
3039 static gboolean first_run
= TRUE
;
3045 f
= input_dialog (_("Goto line"), _("Enter line:"), MC_HISTORY_EDIT_GOTO_LINE
,
3046 first_run
? NULL
: INPUT_LAST_TEXT
, INPUT_COMPLETE_NONE
);
3047 if (f
== NULL
|| *f
== '\0')
3053 l
= strtol (f
, &error
, 0);
3061 l
= edit
->buffer
.lines
+ l
+ 2;
3062 edit_move_display (edit
, l
- WIDGET (edit
)->lines
/ 2 - 1);
3063 edit_move_to_line (edit
, l
- 1);
3064 edit
->force
|= REDRAW_COMPLETELY
;
3071 /* --------------------------------------------------------------------------------------------- */
3072 /** Return TRUE on success */
3075 edit_save_block_cmd (WEdit
* edit
)
3077 off_t start_mark
, end_mark
;
3079 gboolean ret
= FALSE
;
3081 if (!eval_marks (edit
, &start_mark
, &end_mark
))
3084 tmp
= mc_config_get_full_path (EDIT_CLIP_FILE
);
3086 input_expand_dialog (_("Save block"), _("Enter file name:"),
3087 MC_HISTORY_EDIT_SAVE_BLOCK
, tmp
, INPUT_COMPLETE_FILENAMES
);
3089 edit_push_undo_action (edit
, KEY_PRESS
+ edit
->start_display
);
3091 if (exp
!= NULL
&& *exp
!= '\0')
3093 if (edit_save_block (edit
, exp
, start_mark
, end_mark
))
3096 edit_error_dialog (_("Save block"), get_sys_error (_("Cannot save file")));
3098 edit
->force
|= REDRAW_COMPLETELY
;
3107 /* --------------------------------------------------------------------------------------------- */
3109 /** returns TRUE on success */
3111 edit_insert_file_cmd (WEdit
* edit
)
3115 gboolean ret
= FALSE
;
3117 tmp
= mc_config_get_full_path (EDIT_CLIP_FILE
);
3118 exp
= input_expand_dialog (_("Insert file"), _("Enter file name:"),
3119 MC_HISTORY_EDIT_INSERT_FILE
, tmp
, INPUT_COMPLETE_FILENAMES
);
3122 edit_push_undo_action (edit
, KEY_PRESS
+ edit
->start_display
);
3124 if (exp
!= NULL
&& *exp
!= '\0')
3126 vfs_path_t
*exp_vpath
;
3128 exp_vpath
= vfs_path_from_str (exp
);
3129 ret
= (edit_insert_file (edit
, exp_vpath
) >= 0);
3130 vfs_path_free (exp_vpath
);
3133 edit_error_dialog (_("Insert file"), get_sys_error (_("Cannot insert file")));
3138 edit
->force
|= REDRAW_COMPLETELY
;
3142 /* --------------------------------------------------------------------------------------------- */
3143 /** sorts a block, returns -1 on system fail, 1 on cancel and 0 on success */
3146 edit_sort_cmd (WEdit
* edit
)
3148 char *exp
, *tmp
, *tmp_edit_block_name
, *tmp_edit_temp_name
;
3149 off_t start_mark
, end_mark
;
3152 if (!eval_marks (edit
, &start_mark
, &end_mark
))
3154 edit_error_dialog (_("Sort block"), _("You must first highlight a block of text"));
3158 tmp
= mc_config_get_full_path (EDIT_BLOCK_FILE
);
3159 edit_save_block (edit
, tmp
, start_mark
, end_mark
);
3162 exp
= input_dialog (_("Run sort"),
3163 _("Enter sort options (see manpage) separated by whitespace:"),
3164 MC_HISTORY_EDIT_SORT
, INPUT_LAST_TEXT
, INPUT_COMPLETE_NONE
);
3169 tmp_edit_block_name
= mc_config_get_full_path (EDIT_BLOCK_FILE
);
3170 tmp_edit_temp_name
= mc_config_get_full_path (EDIT_TEMP_FILE
);
3172 g_strconcat (" sort ", exp
, " ", tmp_edit_block_name
,
3173 " > ", tmp_edit_temp_name
, (char *) NULL
);
3174 g_free (tmp_edit_temp_name
);
3175 g_free (tmp_edit_block_name
);
3182 if (e
== -1 || e
== 127)
3183 edit_error_dialog (_("Sort"), get_sys_error (_("Cannot execute sort command")));
3188 sprintf (q
, "%d ", e
);
3189 tmp
= g_strdup_printf (_("Sort returned non-zero: %s"), q
);
3190 edit_error_dialog (_("Sort"), tmp
);
3196 edit
->force
|= REDRAW_COMPLETELY
;
3198 if (edit_block_delete_cmd (edit
))
3202 vfs_path_t
*tmp_vpath
;
3204 tmp_vpath
= mc_config_get_full_vpath (EDIT_TEMP_FILE
);
3205 edit_insert_file (edit
, tmp_vpath
);
3206 vfs_path_free (tmp_vpath
);
3211 /* --------------------------------------------------------------------------------------------- */
3213 * Ask user for a command, execute it and paste its output back to the
3218 edit_ext_cmd (WEdit
* edit
)
3220 char *exp
, *tmp
, *tmp_edit_temp_file
;
3224 input_dialog (_("Paste output of external command"),
3225 _("Enter shell command(s):"), MC_HISTORY_EDIT_PASTE_EXTCMD
, INPUT_LAST_TEXT
,
3226 INPUT_COMPLETE_FILENAMES
| INPUT_COMPLETE_VARIABLES
| INPUT_COMPLETE_USERNAMES
3227 | INPUT_COMPLETE_HOSTNAMES
| INPUT_COMPLETE_CD
| INPUT_COMPLETE_COMMANDS
|
3228 INPUT_COMPLETE_SHELL_ESC
);
3233 tmp_edit_temp_file
= mc_config_get_full_path (EDIT_TEMP_FILE
);
3234 tmp
= g_strconcat (exp
, " > ", tmp_edit_temp_file
, (char *) NULL
);
3235 g_free (tmp_edit_temp_file
);
3242 edit_error_dialog (_("External command"), get_sys_error (_("Cannot execute command")));
3246 edit
->force
|= REDRAW_COMPLETELY
;
3249 vfs_path_t
*tmp_vpath
;
3251 tmp_vpath
= mc_config_get_full_vpath (EDIT_TEMP_FILE
);
3252 edit_insert_file (edit
, tmp_vpath
);
3253 vfs_path_free (tmp_vpath
);
3258 /* --------------------------------------------------------------------------------------------- */
3259 /** if block is 1, a block must be highlighted and the shell command
3260 processes it. If block is 0 the shell command is a straight system
3261 command, that just produces some output which is to be inserted */
3264 edit_block_process_cmd (WEdit
* edit
, int macro_number
)
3267 char *macros_fname
= NULL
;
3269 fname
= g_strdup_printf ("%s.%i.sh", MC_EXTMACRO_FILE
, macro_number
);
3270 macros_fname
= g_build_filename (mc_config_get_data_path (), fname
, (char *) NULL
);
3271 user_menu (edit
, macros_fname
, 0);
3273 g_free (macros_fname
);
3274 edit
->force
|= REDRAW_COMPLETELY
;
3277 /* --------------------------------------------------------------------------------------------- */
3280 edit_mail_dialog (WEdit
* edit
)
3282 char *mail_to
, *mail_subject
, *mail_cc
;
3284 quick_widget_t quick_widgets
[] = {
3286 QUICK_LABEL (N_("mail -s <subject> -c <cc> <to>"), NULL
),
3287 QUICK_LABELED_INPUT (N_("To"), input_label_above
,
3288 INPUT_LAST_TEXT
, "mail-dlg-input-3",
3289 &mail_to
, NULL
, FALSE
, FALSE
, INPUT_COMPLETE_USERNAMES
),
3290 QUICK_LABELED_INPUT (N_("Subject"), input_label_above
,
3291 INPUT_LAST_TEXT
, "mail-dlg-input-2",
3292 &mail_subject
, NULL
, FALSE
, FALSE
, INPUT_COMPLETE_NONE
),
3293 QUICK_LABELED_INPUT (N_("Copies to"), input_label_above
,
3294 INPUT_LAST_TEXT
, "mail-dlg-input",
3295 &mail_cc
, NULL
, FALSE
, FALSE
, INPUT_COMPLETE_USERNAMES
),
3296 QUICK_BUTTONS_OK_CANCEL
,
3301 quick_dialog_t qdlg
= {
3303 N_("Mail"), "[Input Line Keys]",
3304 quick_widgets
, NULL
, NULL
3307 if (quick_dialog (&qdlg
) != B_CANCEL
)
3309 pipe_mail (&edit
->buffer
, mail_to
, mail_subject
, mail_cc
);
3311 g_free (mail_subject
);
3316 /* --------------------------------------------------------------------------------------------- */
3318 /*******************/
3319 /* Word Completion */
3320 /*******************/
3323 * Complete current word using regular expression search
3324 * backwards beginning at the current cursor position.
3328 edit_complete_word_cmd (WEdit
* edit
)
3330 gsize i
, max_len
, word_len
= 0, num_compl
= 0;
3331 off_t word_start
= 0;
3332 GString
*match_expr
;
3333 GString
*compl[MAX_WORD_COMPLETIONS
]; /* completions */
3335 /* search start of word to be completed */
3336 if (!edit_find_word_start (&edit
->buffer
, &word_start
, &word_len
))
3339 /* prepare match expression */
3340 /* match_expr = g_strdup_printf ("\\b%.*s[a-zA-Z_0-9]+", word_len, bufpos); */
3341 match_expr
= g_string_new ("(^|\\s+|\\b)");
3342 for (i
= 0; i
< word_len
; i
++)
3343 g_string_append_c (match_expr
, edit_buffer_get_byte (&edit
->buffer
, word_start
+ i
));
3344 g_string_append (match_expr
,
3345 "[^\\s\\.=\\+\\[\\]\\(\\)\\,\\;\\:\\\"\\'\\-\\?\\/\\|\\\\\\{\\}\\*\\&\\^\\%%\\$#@\\!]+");
3347 /* collect the possible completions */
3348 /* start search from begin to end of file */
3350 edit_collect_completions (edit
, word_start
, word_len
, match_expr
->str
, (GString
**) & compl,
3355 /* insert completed word if there is only one match */
3357 edit_complete_word_insert_recoded_completion (edit
, compl[0]->str
, word_len
);
3358 /* more than one possible completion => ask the user */
3363 /* !!! usually only a beep is expected and when <ALT-TAB> is !!! */
3364 /* !!! pressed again the selection dialog pops up, but that !!! */
3365 /* !!! seems to require a further internal state !!! */
3368 /* let the user select the preferred completion */
3369 curr_compl
= editcmd_dialog_completion_show (edit
, max_len
,
3370 (GString
**) & compl, num_compl
);
3372 if (curr_compl
!= NULL
)
3374 edit_complete_word_insert_recoded_completion (edit
, curr_compl
, word_len
);
3375 g_free (curr_compl
);
3380 g_string_free (match_expr
, TRUE
);
3381 /* release memory before return */
3382 for (i
= 0; i
< num_compl
; i
++)
3383 g_string_free (compl[i
], TRUE
);
3386 /* --------------------------------------------------------------------------------------------- */
3390 edit_select_codepage_cmd (WEdit
* edit
)
3392 if (do_select_codepage ())
3393 edit_set_codeset (edit
);
3395 edit
->force
= REDRAW_PAGE
;
3396 widget_redraw (WIDGET (edit
));
3400 /* --------------------------------------------------------------------------------------------- */
3403 edit_insert_literal_cmd (WEdit
* edit
)
3405 int char_for_insertion
;
3407 char_for_insertion
= editcmd_dialog_raw_key_query (_("Insert literal"),
3408 _("Press any key:"), FALSE
);
3409 edit_execute_key_command (edit
, -1, ascii_alpha_to_cntrl (char_for_insertion
));
3412 /* --------------------------------------------------------------------------------------------- */
3415 edit_begin_end_macro_cmd (WEdit
* edit
)
3417 /* edit is a pointer to the widget */
3420 long command
= macro_index
< 0 ? CK_MacroStartRecord
: CK_MacroStopRecord
;
3421 edit_execute_key_command (edit
, command
, -1);
3425 /* --------------------------------------------------------------------------------------------- */
3428 edit_begin_end_repeat_cmd (WEdit
* edit
)
3430 /* edit is a pointer to the widget */
3433 long command
= macro_index
< 0 ? CK_RepeatStartRecord
: CK_RepeatStopRecord
;
3434 edit_execute_key_command (edit
, command
, -1);
3438 /* --------------------------------------------------------------------------------------------- */
3441 edit_load_forward_cmd (WEdit
* edit
)
3444 && edit_query_dialog2 (_("Warning"),
3445 _("Current text was modified without a file save.\n"
3446 "Continue discards these changes"), _("C&ontinue"),
3449 edit
->force
|= REDRAW_COMPLETELY
;
3453 if (edit_stack_iterator
+ 1 >= MAX_HISTORY_MOVETO
)
3456 if (edit_history_moveto
[edit_stack_iterator
+ 1].line
< 1)
3459 edit_stack_iterator
++;
3460 if (edit_history_moveto
[edit_stack_iterator
].filename_vpath
!= NULL
)
3461 return edit_reload_line (edit
, edit_history_moveto
[edit_stack_iterator
].filename_vpath
,
3462 edit_history_moveto
[edit_stack_iterator
].line
);
3467 /* --------------------------------------------------------------------------------------------- */
3470 edit_load_back_cmd (WEdit
* edit
)
3473 && edit_query_dialog2 (_("Warning"),
3474 _("Current text was modified without a file save.\n"
3475 "Continue discards these changes"), _("C&ontinue"),
3478 edit
->force
|= REDRAW_COMPLETELY
;
3482 /* we are in the bottom of the stack, NO WAY! */
3483 if (edit_stack_iterator
== 0)
3486 edit_stack_iterator
--;
3487 if (edit_history_moveto
[edit_stack_iterator
].filename_vpath
!= NULL
)
3488 return edit_reload_line (edit
, edit_history_moveto
[edit_stack_iterator
].filename_vpath
,
3489 edit_history_moveto
[edit_stack_iterator
].line
);
3494 /* --------------------------------------------------------------------------------------------- */
3497 edit_get_match_keyword_cmd (WEdit
* edit
)
3499 gsize word_len
= 0, max_len
= 0;
3502 off_t word_start
= 0;
3503 GString
*match_expr
;
3506 char *tagfile
= NULL
;
3508 etags_hash_t def_hash
[MAX_DEFINITIONS
];
3510 for (i
= 0; i
< MAX_DEFINITIONS
; i
++)
3512 def_hash
[i
].filename
= NULL
;
3515 /* search start of word to be completed */
3516 if (!edit_find_word_start (&edit
->buffer
, &word_start
, &word_len
))
3519 /* prepare match expression */
3520 match_expr
= g_string_sized_new (word_len
);
3521 for (i
= 0; i
< word_len
; i
++)
3522 g_string_append_c (match_expr
, edit_buffer_get_byte (&edit
->buffer
, word_start
+ i
));
3524 ptr
= g_get_current_dir ();
3525 path
= g_strconcat (ptr
, PATH_SEP_STR
, (char *) NULL
);
3528 /* Recursive search file 'TAGS' in parent dirs */
3531 ptr
= g_path_get_dirname (path
);
3535 tagfile
= mc_build_filename (path
, TAGS_NAME
, (char *) NULL
);
3536 if (exist_file (tagfile
))
3539 while (strcmp (path
, PATH_SEP_STR
) != 0);
3544 etags_set_definition_hash (tagfile
, path
, match_expr
->str
, (etags_hash_t
*) & def_hash
);
3549 max_len
= MAX_WIDTH_DEF_DIALOG
;
3553 editcmd_dialog_select_definition_show (edit
, match_expr
->str
, max_len
, word_len
,
3554 (etags_hash_t
*) & def_hash
, num_def
);
3556 g_string_free (match_expr
, TRUE
);
3559 /* --------------------------------------------------------------------------------------------- */
3563 edit_suggest_current_word (WEdit
* edit
)
3567 off_t word_start
= 0;
3568 int retval
= B_SKIP_WORD
;
3569 GString
*match_word
;
3571 /* search start of word to spell check */
3572 match_word
= edit_buffer_get_word_from_pos (&edit
->buffer
, edit
->buffer
.curs1
, &word_start
,
3574 word_len
= match_word
->len
;
3577 if (mc_global
.source_codepage
>= 0 && (mc_global
.source_codepage
!= mc_global
.display_codepage
))
3581 tmp_word
= str_convert_to_display (match_word
->str
);
3582 g_string_free (match_word
, TRUE
);
3583 match_word
= tmp_word
;
3586 if (!aspell_check (match_word
->str
, (int) word_len
))
3592 suggest
= g_array_new (TRUE
, FALSE
, sizeof (char *));
3594 res
= aspell_suggest (suggest
, match_word
->str
, (int) word_len
);
3597 char *new_word
= NULL
;
3599 edit
->found_start
= word_start
;
3600 edit
->found_len
= word_len
;
3601 edit
->force
|= REDRAW_PAGE
;
3602 edit_scroll_screen_over_cursor (edit
);
3603 edit_render_keypress (edit
);
3605 retval
= spell_dialog_spell_suggest_show (edit
, match_word
->str
, &new_word
, suggest
);
3606 edit_cursor_move (edit
, word_len
- cut_len
);
3608 if (retval
== B_ENTER
&& new_word
!= NULL
)
3613 if (mc_global
.source_codepage
>= 0 &&
3614 (mc_global
.source_codepage
!= mc_global
.display_codepage
))
3618 tmp_word
= str_convert_to_input (new_word
);
3620 new_word
= g_string_free (tmp_word
, FALSE
);
3624 for (i
= 0; i
< word_len
; i
++)
3625 edit_backspace (edit
, TRUE
);
3626 for (; *new_word
; new_word
++)
3627 edit_insert (edit
, *new_word
);
3630 else if (retval
== B_ADD_WORD
&& match_word
!= NULL
)
3631 aspell_add_to_dict (match_word
->str
, (int) word_len
);
3635 for (i
= 0; i
< suggest
->len
; i
++)
3637 char *cur_sugg_word
;
3639 cur_sugg_word
= g_array_index (suggest
, char *, i
);
3640 g_free (cur_sugg_word
);
3642 g_array_free (suggest
, TRUE
);
3643 edit
->found_start
= 0;
3644 edit
->found_len
= 0;
3646 g_string_free (match_word
, TRUE
);
3650 /* --------------------------------------------------------------------------------------------- */
3653 edit_spellcheck_file (WEdit
* edit
)
3655 if (edit
->buffer
.curs_line
> 0)
3657 edit_cursor_move (edit
, -edit
->buffer
.curs1
);
3658 edit_move_to_prev_col (edit
, 0);
3659 edit_update_curs_row (edit
);
3666 c2
= edit_buffer_get_current_byte (&edit
->buffer
);
3670 if (edit
->buffer
.curs1
>= edit
->buffer
.size
)
3674 edit_cursor_move (edit
, 1);
3675 c2
= edit_buffer_get_current_byte (&edit
->buffer
);
3677 while (is_break_char (c1
) || is_break_char (c2
));
3679 while (edit_suggest_current_word (edit
) != B_CANCEL
);
3682 /* --------------------------------------------------------------------------------------------- */
3685 edit_set_spell_lang (void)
3689 lang_list
= g_array_new (TRUE
, FALSE
, sizeof (char *));
3690 if (aspell_get_lang_list (lang_list
) != 0)
3694 lang
= spell_dialog_lang_list_show (lang_list
);
3697 (void) aspell_set_lang (lang
);
3701 aspell_array_clean (lang_list
);
3703 #endif /* HAVE_ASPELL */
3705 /* --------------------------------------------------------------------------------------------- */