2 Editor high level editing commands
4 Copyright (C) 1996-2017
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 */
41 #include <sys/types.h>
48 #include "lib/global.h"
49 #include "lib/tty/tty.h"
50 #include "lib/tty/key.h" /* XCTRL */
51 #include "lib/mcconfig.h"
53 #include "lib/strutil.h" /* utf string functions */
54 #include "lib/fileloc.h"
56 #include "lib/util.h" /* tilde_expand() */
57 #include "lib/vfs/vfs.h"
58 #include "lib/widget.h"
59 #include "lib/event.h" /* mc_event_raise() */
61 #include "lib/charsets.h"
64 #include "src/history.h"
65 #include "src/setup.h" /* option_tab_spacing */
67 #include "src/selcodepage.h"
69 #include "src/keybind-defaults.h"
70 #include "src/util.h" /* check_for_default() */
72 #include "edit-impl.h"
73 #include "editwidget.h"
74 #include "editcmd_dialogs.h"
77 #include "spell_dialogs.h"
81 /*** global variables ****************************************************************************/
83 /* search and replace: */
84 int search_create_bookmark
= FALSE
;
86 /* queries on a save */
87 gboolean edit_confirm_save
= TRUE
;
89 /* whether we need to drop selection on copy to buffer */
90 gboolean option_drop_selection_on_copy
= TRUE
;
92 /*** file scope macro definitions ****************************************************************/
96 #define TEMP_BUF_LEN 1024
98 #define MAX_WORD_COMPLETIONS 100 /* in listbox */
100 /*** file scope type declarations ****************************************************************/
104 simple_status_msg_t status_msg
; /* base class */
109 } edit_search_status_msg_t
;
111 /*** file scope variables ************************************************************************/
113 static unsigned long edit_save_mode_radio_id
, edit_save_mode_input_id
;
115 /* --------------------------------------------------------------------------------------------- */
116 /*** file scope functions ************************************************************************/
117 /* --------------------------------------------------------------------------------------------- */
120 edit_search_status_update_cb (status_msg_t
* sm
)
122 simple_status_msg_t
*ssm
= SIMPLE_STATUS_MSG (sm
);
123 edit_search_status_msg_t
*esm
= (edit_search_status_msg_t
*) sm
;
124 Widget
*wd
= WIDGET (sm
->dlg
);
127 label_set_textv (ssm
->label
, _("Searching %s: %3d%%"), esm
->edit
->last_search_string
,
128 edit_buffer_calc_percent (&esm
->edit
->buffer
, esm
->offset
));
130 label_set_textv (ssm
->label
, _("Searching %s"), esm
->edit
->last_search_string
);
135 Widget
*lw
= WIDGET (ssm
->label
);
137 wd_width
= MAX (wd
->cols
, lw
->cols
+ 6);
138 widget_set_size (wd
, wd
->y
, wd
->x
, wd
->lines
, wd_width
);
139 widget_set_size (lw
, lw
->y
, wd
->x
+ (wd
->cols
- lw
->cols
) / 2, lw
->lines
, lw
->cols
);
143 return status_msg_common_update (sm
);
146 /* --------------------------------------------------------------------------------------------- */
149 edit_save_mode_callback (Widget
* w
, Widget
* sender
, widget_msg_t msg
, int parm
, void *data
)
153 case MSG_CHANGED_FOCUS
:
154 if (sender
!= NULL
&& sender
->id
== edit_save_mode_radio_id
)
158 ww
= dlg_find_by_id (DIALOG (w
), edit_save_mode_input_id
);
159 widget_disable (ww
, RADIO (sender
)->sel
!= 2);
163 return MSG_NOT_HANDLED
;
166 return dlg_default_callback (w
, sender
, msg
, parm
, data
);
170 /* --------------------------------------------------------------------------------------------- */
172 /* If 0 (quick save) then a) create/truncate <filename> file,
173 b) save to <filename>;
174 if 1 (safe save) then a) save to <tempnam>,
175 b) rename <tempnam> to <filename>;
176 if 2 (do backups) then a) save to <tempnam>,
177 b) rename <filename> to <filename.backup_ext>,
178 c) rename <tempnam> to <filename>. */
180 /* returns 0 on error, -1 on abort */
183 edit_save_file (WEdit
* edit
, const vfs_path_t
* filename_vpath
)
188 int this_save_mode
, rv
, fd
= -1;
189 vfs_path_t
*real_filename_vpath
;
190 vfs_path_t
*savename_vpath
= NULL
;
191 const char *start_filename
;
192 const vfs_path_element_t
*vpath_element
;
195 vpath_element
= vfs_path_get_by_index (filename_vpath
, 0);
196 if (vpath_element
== NULL
)
199 start_filename
= vpath_element
->path
;
200 if (*start_filename
== '\0')
203 if (!IS_PATH_SEP (*start_filename
) && edit
->dir_vpath
!= NULL
)
204 real_filename_vpath
= vfs_path_append_vpath_new (edit
->dir_vpath
, filename_vpath
, NULL
);
206 real_filename_vpath
= vfs_path_clone (filename_vpath
);
208 this_save_mode
= option_save_mode
;
209 if (this_save_mode
!= EDIT_QUICK_SAVE
)
211 if (!vfs_file_is_local (real_filename_vpath
)
212 || (fd
= mc_open (real_filename_vpath
, O_RDONLY
| O_BINARY
)) == -1)
215 * The file does not exists yet, so no safe save or
216 * backup are necessary.
218 this_save_mode
= EDIT_QUICK_SAVE
;
224 rv
= mc_stat (real_filename_vpath
, &sb
);
227 if (this_save_mode
== EDIT_QUICK_SAVE
&& !edit
->skip_detach_prompt
&& sb
.st_nlink
> 1)
229 rv
= edit_query_dialog3 (_("Warning"),
230 _("File has hard-links. Detach before saving?"),
231 _("&Yes"), _("&No"), _("&Cancel"));
235 this_save_mode
= EDIT_SAFE_SAVE
;
238 edit
->skip_detach_prompt
= 1;
241 vfs_path_free (real_filename_vpath
);
246 /* Prevent overwriting changes from other editor sessions. */
247 if (edit
->stat1
.st_mtime
!= 0 && edit
->stat1
.st_mtime
!= sb
.st_mtime
)
249 /* The default action is "Cancel". */
252 rv
= edit_query_dialog2 (_("Warning"),
253 _("The file has been modified in the meantime. Save anyway?"),
254 _("&Yes"), _("&Cancel"));
257 vfs_path_free (real_filename_vpath
);
263 if (this_save_mode
!= EDIT_QUICK_SAVE
)
265 char *savedir
, *saveprefix
;
267 savedir
= vfs_path_tokens_get (real_filename_vpath
, 0, -1);
269 savedir
= g_strdup (".");
271 /* Token-related function never return leading slash, so we need add it manually */
272 saveprefix
= mc_build_filename (PATH_SEP_STR
, savedir
, "cooledit", (char *) NULL
);
274 fd
= mc_mkstemps (&savename_vpath
, saveprefix
, NULL
);
276 if (savename_vpath
== NULL
)
278 vfs_path_free (real_filename_vpath
);
282 * Close for now because mc_mkstemps use pure open system call
283 * to create temporary file and it needs to be reopened by
284 * VFS-aware mc_open().
289 savename_vpath
= vfs_path_clone (real_filename_vpath
);
291 (void) mc_chown (savename_vpath
, edit
->stat1
.st_uid
, edit
->stat1
.st_gid
);
292 (void) mc_chmod (savename_vpath
, edit
->stat1
.st_mode
);
294 fd
= mc_open (savename_vpath
, O_CREAT
| O_WRONLY
| O_TRUNC
| O_BINARY
, edit
->stat1
.st_mode
);
299 p
= edit_get_write_filter (savename_vpath
, real_filename_vpath
);
305 file
= (FILE *) popen (p
, "w");
309 filelen
= edit_write_stream (edit
, file
);
313 if (pclose (file
) != 0)
315 tmp
= g_strdup_printf (_("Error writing to pipe: %s"), p
);
316 edit_error_dialog (_("Error"), tmp
);
325 tmp
= g_strdup_printf (_("Cannot open pipe for writing: %s"), p
);
326 edit_error_dialog (_("Error"), get_sys_error (tmp
));
333 else if (edit
->lb
== LB_ASIS
)
334 { /* do not change line breaks */
335 filelen
= edit_buffer_write_file (&edit
->buffer
, fd
);
337 if (filelen
!= edit
->buffer
.size
)
342 if (mc_close (fd
) != 0)
344 /* Update the file information, especially the mtime. */
345 if (mc_stat (savename_vpath
, &edit
->stat1
) == -1)
349 { /* change line breaks */
351 const vfs_path_element_t
*path_element
;
355 path_element
= vfs_path_get_by_index (savename_vpath
, -1);
356 file
= (FILE *) fopen (path_element
->path
, "w");
359 filelen
= edit_write_stream (edit
, file
);
366 msg
= g_strdup_printf (_("Cannot open file for writing: %s"), path_element
->path
);
367 edit_error_dialog (_("Error"), msg
);
373 if (filelen
!= edit
->buffer
.size
)
376 if (this_save_mode
== EDIT_DO_BACKUP
)
378 char *tmp_store_filename
;
379 vfs_path_element_t
*last_vpath_element
;
380 vfs_path_t
*tmp_vpath
;
383 g_assert (option_backup_ext
!= NULL
);
385 /* add backup extension to the path */
386 tmp_vpath
= vfs_path_clone (real_filename_vpath
);
387 last_vpath_element
= (vfs_path_element_t
*) vfs_path_get_by_index (tmp_vpath
, -1);
388 tmp_store_filename
= last_vpath_element
->path
;
389 last_vpath_element
->path
= g_strdup_printf ("%s%s", tmp_store_filename
, option_backup_ext
);
390 g_free (tmp_store_filename
);
392 ok
= (mc_rename (real_filename_vpath
, tmp_vpath
) != -1);
393 vfs_path_free (tmp_vpath
);
398 if (this_save_mode
!= EDIT_QUICK_SAVE
)
399 if (mc_rename (savename_vpath
, real_filename_vpath
) == -1)
402 vfs_path_free (real_filename_vpath
);
403 vfs_path_free (savename_vpath
);
406 /* FIXME: Is this safe ?
407 * if (this_save_mode != EDIT_QUICK_SAVE)
408 * mc_unlink (savename);
410 vfs_path_free (real_filename_vpath
);
411 vfs_path_free (savename_vpath
);
415 /* --------------------------------------------------------------------------------------------- */
418 edit_check_newline (const edit_buffer_t
* buf
)
420 return !(option_check_nl_at_eof
&& buf
->size
> 0
421 && edit_buffer_get_byte (buf
, buf
->size
- 1) != '\n'
422 && edit_query_dialog2 (_("Warning"),
423 _("The file you are saving does not end with a newline."),
424 _("C&ontinue"), _("&Cancel")) != 0);
427 /* --------------------------------------------------------------------------------------------- */
430 edit_get_save_file_as (WEdit
* edit
)
432 static LineBreaks cur_lb
= LB_ASIS
;
434 vfs_path_t
*ret_vpath
= NULL
;
436 const char *lb_names
[LB_NAMES
] = {
437 N_("&Do not change"),
438 N_("&Unix format (LF)"),
439 N_("&Windows/DOS format (CR LF)"),
440 N_("&Macintosh format (CR)")
443 quick_widget_t quick_widgets
[] = {
445 QUICK_LABELED_INPUT (N_("Enter file name:"), input_label_above
,
446 vfs_path_as_str (edit
->filename_vpath
), "save-as",
447 &filename_res
, NULL
, FALSE
, FALSE
, INPUT_COMPLETE_FILENAMES
),
448 QUICK_SEPARATOR (TRUE
),
449 QUICK_LABEL (N_("Change line breaks to:"), NULL
),
450 QUICK_RADIO (LB_NAMES
, lb_names
, (int *) &cur_lb
, NULL
),
451 QUICK_BUTTONS_OK_CANCEL
,
456 quick_dialog_t qdlg
= {
458 N_("Save As"), "[Save File As]",
459 quick_widgets
, NULL
, NULL
462 if (quick_dialog (&qdlg
) != B_CANCEL
)
467 fname
= tilde_expand (filename_res
);
468 g_free (filename_res
);
469 ret_vpath
= vfs_path_from_str (fname
);
476 /* --------------------------------------------------------------------------------------------- */
478 /** returns TRUE on success */
481 edit_save_cmd (WEdit
* edit
)
483 int res
, save_lock
= 0;
485 if (!edit
->locked
&& !edit
->delete_file
)
486 save_lock
= lock_file (edit
->filename_vpath
);
487 res
= edit_save_file (edit
, edit
->filename_vpath
);
489 /* Maintain modify (not save) lock on failure */
490 if ((res
> 0 && edit
->locked
) || save_lock
)
491 edit
->locked
= unlock_file (edit
->filename_vpath
);
493 /* On failure try 'save as', it does locking on its own */
495 return edit_save_as_cmd (edit
);
496 edit
->force
|= REDRAW_COMPLETELY
;
499 edit
->delete_file
= 0;
506 /* --------------------------------------------------------------------------------------------- */
510 * @param h screen the owner of editor window
511 * @param vpath vfs file path
512 * @return TRUE if file content was successfully loaded, FALSE otherwise
515 static inline gboolean
516 edit_load_file_from_filename (WDialog
* h
, const vfs_path_t
* vpath
)
518 Widget
*w
= WIDGET (h
);
520 return edit_add_window (h
, w
->y
+ 1, w
->x
, w
->lines
- 2, w
->cols
, vpath
, 0);
523 /* --------------------------------------------------------------------------------------------- */
526 edit_delete_column_of_text (WEdit
* edit
)
533 eval_marks (edit
, &m1
, &m2
);
534 n
= edit_buffer_get_forward_offset (&edit
->buffer
, m1
, 0, m2
) + 1;
535 c
= (long) edit_move_forward3 (edit
, edit_buffer_get_bol (&edit
->buffer
, m1
), 0, m1
);
536 d
= (long) edit_move_forward3 (edit
, edit_buffer_get_bol (&edit
->buffer
, m2
), 0, m2
);
537 b
= MAX (MIN (c
, d
), MIN (edit
->column1
, edit
->column2
));
538 c
= MAX (c
, MAX (edit
->column1
, edit
->column2
));
542 r
= edit_buffer_get_current_bol (&edit
->buffer
);
543 p
= edit_move_forward3 (edit
, r
, b
, 0);
544 q
= edit_move_forward3 (edit
, r
, c
, 0);
549 edit_cursor_move (edit
, p
- edit
->buffer
.curs1
);
552 /* delete line between margins */
553 if (edit_buffer_get_current_byte (&edit
->buffer
) != '\n')
554 edit_delete (edit
, TRUE
);
558 /* move to next line except on the last delete */
559 edit_cursor_move (edit
,
560 edit_buffer_get_forward_offset (&edit
->buffer
, edit
->buffer
.curs1
, 1,
561 0) - edit
->buffer
.curs1
);
565 /* --------------------------------------------------------------------------------------------- */
566 /** if success return 0 */
569 edit_block_delete (WEdit
* edit
)
571 off_t start_mark
, end_mark
;
573 long curs_line
, c1
, c2
;
575 if (!eval_marks (edit
, &start_mark
, &end_mark
))
578 if (edit
->column_highlight
&& edit
->mark2
< 0)
579 edit_mark_cmd (edit
, FALSE
);
580 if ((end_mark
- start_mark
) > option_max_undo
/ 2)
582 /* Warning message with a query to continue or cancel the operation */
583 if (edit_query_dialog2
586 ("Block is large, you may not be able to undo this action"),
587 _("C&ontinue"), _("&Cancel")))
592 c1
= MIN (edit
->column1
, edit
->column2
);
593 c2
= MAX (edit
->column1
, edit
->column2
);
597 edit_push_markers (edit
);
599 curs_line
= edit
->buffer
.curs_line
;
601 curs_pos
= edit
->curs_col
+ edit
->over_col
;
603 /* move cursor to start of selection */
604 edit_cursor_move (edit
, start_mark
- edit
->buffer
.curs1
);
605 edit_scroll_screen_over_cursor (edit
);
607 if (start_mark
< end_mark
)
609 if (edit
->column_highlight
)
614 edit_mark_cmd (edit
, FALSE
);
615 edit_delete_column_of_text (edit
);
616 /* move cursor to the saved position */
617 edit_move_to_line (edit
, curs_line
);
618 /* calculate line width and cursor position before cut */
619 line_width
= edit_move_forward3 (edit
, edit_buffer_get_current_bol (&edit
->buffer
), 0,
620 edit_buffer_get_current_eol (&edit
->buffer
));
621 if (option_cursor_beyond_eol
&& curs_pos
> line_width
)
622 edit
->over_col
= curs_pos
- line_width
;
628 for (count
= start_mark
; count
< end_mark
; count
++)
629 edit_delete (edit
, TRUE
);
632 edit_set_markers (edit
, 0, 0, 0, 0);
633 edit
->force
|= REDRAW_PAGE
;
637 /* --------------------------------------------------------------------------------------------- */
639 * Get EOL symbol for searching.
641 * @param edit editor object
646 edit_search_get_current_end_line_char (const WEdit
* edit
)
657 /* --------------------------------------------------------------------------------------------- */
659 * Checking if search condition have BOL(^) or EOL ($) regexp special characters.
661 * @param search search object
662 * @return result of checks.
665 static edit_search_line_t
666 edit_get_search_line_type (mc_search_t
* search
)
668 edit_search_line_t search_line_type
= 0;
670 if (search
->search_type
!= MC_SEARCH_T_REGEX
)
671 return search_line_type
;
673 if (*search
->original
== '^')
674 search_line_type
|= AT_START_LINE
;
676 if (search
->original
[search
->original_len
- 1] == '$')
677 search_line_type
|= AT_END_LINE
;
678 return search_line_type
;
681 /* --------------------------------------------------------------------------------------------- */
683 * Calculating the start position of next line.
685 * @param buf editor buffer object
686 * @param current_pos current position
687 * @param max_pos max position
688 * @param end_string_symbol end of line symbol
689 * @return start position of next line
693 edit_calculate_start_of_next_line (const edit_buffer_t
* buf
, off_t current_pos
, off_t max_pos
,
694 char end_string_symbol
)
698 for (i
= current_pos
; i
< max_pos
; i
++)
701 if (edit_buffer_get_byte (buf
, i
) == end_string_symbol
)
708 /* --------------------------------------------------------------------------------------------- */
710 * Calculating the end position of previous line.
712 * @param buf editor buffer object
713 * @param current_pos current position
714 * @param end_string_symbol end of line symbol
715 * @return end position of previous line
719 edit_calculate_end_of_previous_line (const edit_buffer_t
* buf
, off_t current_pos
,
720 char end_string_symbol
)
724 for (i
= current_pos
- 1; i
>= 0; i
--)
725 if (edit_buffer_get_byte (buf
, i
) == end_string_symbol
)
731 /* --------------------------------------------------------------------------------------------- */
733 * Calculating the start position of previous line.
735 * @param buf editor buffer object
736 * @param current_pos current position
737 * @param end_string_symbol end of line symbol
738 * @return start position of previous line
742 edit_calculate_start_of_previous_line (const edit_buffer_t
* buf
, off_t current_pos
,
743 char end_string_symbol
)
745 current_pos
= edit_calculate_end_of_previous_line (buf
, current_pos
, end_string_symbol
);
746 current_pos
= edit_calculate_end_of_previous_line (buf
, current_pos
, end_string_symbol
);
748 return (current_pos
+ 1);
751 /* --------------------------------------------------------------------------------------------- */
753 * Calculating the start position of current line.
755 * @param buf editor buffer object
756 * @param current_pos current position
757 * @param end_string_symbol end of line symbol
758 * @return start position of current line
762 edit_calculate_start_of_current_line (const edit_buffer_t
* buf
, off_t current_pos
,
763 char end_string_symbol
)
765 current_pos
= edit_calculate_end_of_previous_line (buf
, current_pos
, end_string_symbol
);
767 return (current_pos
+ 1);
770 /* --------------------------------------------------------------------------------------------- */
772 * Fixing (if needed) search start position if 'only in selection' option present.
774 * @param edit editor object
778 edit_search_fix_search_start_if_selection (WEdit
* edit
)
780 off_t start_mark
= 0;
783 if (!edit_search_options
.only_in_selection
)
786 if (!eval_marks (edit
, &start_mark
, &end_mark
))
789 if (edit_search_options
.backwards
)
791 if (edit
->search_start
> end_mark
|| edit
->search_start
<= start_mark
)
792 edit
->search_start
= end_mark
;
796 if (edit
->search_start
< start_mark
|| edit
->search_start
>= end_mark
)
797 edit
->search_start
= start_mark
;
801 /* --------------------------------------------------------------------------------------------- */
804 editcmd_find (edit_search_status_msg_t
* esm
, gsize
* len
)
806 WEdit
*edit
= esm
->edit
;
807 off_t search_start
= edit
->search_start
;
809 off_t start_mark
= 0;
810 off_t end_mark
= edit
->buffer
.size
;
811 char end_string_symbol
;
813 end_string_symbol
= edit_search_get_current_end_line_char (edit
);
815 /* prepare for search */
816 if (edit_search_options
.only_in_selection
)
818 if (!eval_marks (edit
, &start_mark
, &end_mark
))
820 mc_search_set_error (edit
->search
, MC_SEARCH_E_NOTFOUND
, "%s", _(STR_E_NOTFOUND
));
824 /* fix the start and the end of search block positions */
825 if ((edit
->search_line_type
& AT_START_LINE
) != 0
827 || edit_buffer_get_byte (&edit
->buffer
, start_mark
- 1) != end_string_symbol
))
829 edit_calculate_start_of_next_line (&edit
->buffer
, start_mark
, edit
->buffer
.size
,
832 if ((edit
->search_line_type
& AT_END_LINE
) != 0
833 && (end_mark
- 1 != edit
->buffer
.size
834 || edit_buffer_get_byte (&edit
->buffer
, end_mark
) != end_string_symbol
))
836 edit_calculate_end_of_previous_line (&edit
->buffer
, end_mark
, end_string_symbol
);
838 if (start_mark
>= end_mark
)
840 mc_search_set_error (edit
->search
, MC_SEARCH_E_NOTFOUND
, "%s", _(STR_E_NOTFOUND
));
844 else if (edit_search_options
.backwards
)
845 end_mark
= MAX (1, edit
->buffer
.curs1
) - 1;
848 if (edit_search_options
.backwards
)
850 /* backward search */
851 search_end
= end_mark
;
853 if ((edit
->search_line_type
& AT_START_LINE
) != 0)
855 edit_calculate_start_of_current_line (&edit
->buffer
, search_start
,
858 while (search_start
>= start_mark
)
862 if (search_end
> (off_t
) (search_start
+ edit
->search
->original_len
)
863 && mc_search_is_fixed_search_str (edit
->search
))
864 search_end
= search_start
+ edit
->search
->original_len
;
866 ok
= mc_search_run (edit
->search
, (void *) esm
, search_start
, search_end
, len
);
868 if (ok
&& edit
->search
->normal_offset
== search_start
)
871 /* We abort the search in case of a pattern error, or if the user aborts
872 the search. In other words: in all cases except "string not found". */
873 if (!ok
&& edit
->search
->error
!= MC_SEARCH_E_NOTFOUND
)
876 if ((edit
->search_line_type
& AT_START_LINE
) != 0)
878 edit_calculate_start_of_previous_line (&edit
->buffer
, search_start
,
884 mc_search_set_error (edit
->search
, MC_SEARCH_E_NOTFOUND
, "%s", _(STR_E_NOTFOUND
));
889 if ((edit
->search_line_type
& AT_START_LINE
) != 0 && search_start
!= start_mark
)
891 edit_calculate_start_of_next_line (&edit
->buffer
, search_start
, end_mark
,
894 return mc_search_run (edit
->search
, (void *) esm
, search_start
, end_mark
, len
);
897 /* --------------------------------------------------------------------------------------------- */
900 edit_replace_cmd__conv_to_display (const char *str
)
905 tmp
= str_convert_to_display (str
);
909 return g_string_free (tmp
, FALSE
);
910 g_string_free (tmp
, TRUE
);
913 return g_strdup (str
);
916 /* --------------------------------------------------------------------------------------------- */
919 edit_replace_cmd__conv_to_input (char *str
)
924 tmp
= str_convert_to_input (str
);
928 return g_string_free (tmp
, FALSE
);
929 g_string_free (tmp
, TRUE
);
932 return g_strdup (str
);
935 /* --------------------------------------------------------------------------------------------- */
938 edit_show_search_error (const WEdit
* edit
, const char *title
)
940 if (edit
->search
->error
== MC_SEARCH_E_NOTFOUND
)
941 edit_query_dialog (title
, _(STR_E_NOTFOUND
));
942 else if (edit
->search
->error_str
!= NULL
)
943 edit_query_dialog (title
, edit
->search
->error_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 edit_show_search_error (edit
, _("Search"));
1024 status_msg_deinit (STATUS_MSG (&esm
));
1026 edit
->force
|= REDRAW_COMPLETELY
;
1027 edit_scroll_screen_over_cursor (edit
);
1030 /* --------------------------------------------------------------------------------------------- */
1033 edit_search (WEdit
* edit
)
1035 if (editcmd_dialog_search_show (edit
))
1037 edit
->search_line_type
= edit_get_search_line_type (edit
->search
);
1038 edit_search_fix_search_start_if_selection (edit
);
1039 edit_do_search (edit
);
1043 /* --------------------------------------------------------------------------------------------- */
1044 /** Return a null terminated length of text. Result must be g_free'd */
1046 static unsigned char *
1047 edit_get_block (WEdit
* edit
, off_t start
, off_t finish
, off_t
* l
)
1049 unsigned char *s
, *r
;
1051 r
= s
= g_malloc0 (finish
- start
+ 1);
1052 if (edit
->column_highlight
)
1055 /* copy from buffer, excluding chars that are out of the column 'margins' */
1056 while (start
< finish
)
1061 x
= edit_move_forward3 (edit
, edit_buffer_get_bol (&edit
->buffer
, start
), 0, start
);
1062 c
= edit_buffer_get_byte (&edit
->buffer
, start
);
1063 if ((x
>= edit
->column1
&& x
< edit
->column2
)
1064 || (x
>= edit
->column2
&& x
< edit
->column1
) || c
== '\n')
1074 *l
= finish
- start
;
1075 while (start
< finish
)
1076 *s
++ = edit_buffer_get_byte (&edit
->buffer
, start
++);
1082 /* --------------------------------------------------------------------------------------------- */
1083 /** copies a block to clipboard file */
1086 edit_save_block_to_clip_file (WEdit
* edit
, off_t start
, off_t finish
)
1091 tmp
= mc_config_get_full_path (EDIT_CLIP_FILE
);
1092 ret
= edit_save_block (edit
, tmp
, start
, finish
);
1097 /* --------------------------------------------------------------------------------------------- */
1100 pipe_mail (const edit_buffer_t
* buf
, char *to
, char *subject
, char *cc
)
1105 to
= name_quote (to
, FALSE
);
1106 subject
= name_quote (subject
, FALSE
);
1107 cc
= name_quote (cc
, FALSE
);
1108 s
= g_strconcat ("mail -s ", subject
, *cc
? " -c " : "", cc
, " ", to
, (char *) NULL
);
1123 for (i
= 0; i
< buf
->size
; i
++)
1124 fputc (edit_buffer_get_byte (buf
, i
), p
);
1129 /* --------------------------------------------------------------------------------------------- */
1130 /** find first character of current word */
1133 edit_find_word_start (const edit_buffer_t
* buf
, off_t
* word_start
, gsize
* word_len
)
1138 /* return if at begin of file */
1139 if (buf
->curs1
<= 0)
1142 c
= edit_buffer_get_previous_byte (buf
);
1143 /* return if not at end or in word */
1144 if (is_break_char (c
))
1147 /* search start of word to be completed */
1153 c
= edit_buffer_get_byte (buf
, buf
->curs1
- i
- 1);
1155 if (is_break_char (c
))
1157 /* return if word starts with digit */
1166 *word_start
= buf
->curs1
- i
; /* start found */
1167 *word_len
= (gsize
) i
;
1171 /* --------------------------------------------------------------------------------------------- */
1173 * Get current word under cursor
1175 * @param esm status message window
1176 * @param srch mc_search object
1177 * @param word_start start word position
1179 * @return newly allocated string or NULL if no any words under cursor
1183 edit_collect_completions_get_current_word (edit_search_status_msg_t
* esm
, mc_search_t
* srch
,
1186 WEdit
*edit
= esm
->edit
;
1191 if (!mc_search_run (srch
, (void *) esm
, word_start
, edit
->buffer
.size
, &len
))
1194 temp
= g_string_sized_new (len
);
1196 for (i
= 0; i
< (off_t
) len
; i
++)
1200 chr
= edit_buffer_get_byte (&edit
->buffer
, word_start
+ i
);
1202 g_string_append_c (temp
, chr
);
1205 return g_string_free (temp
, temp
->len
== 0);
1208 /* --------------------------------------------------------------------------------------------- */
1209 /** collect the possible completions */
1212 edit_collect_completions (WEdit
* edit
, off_t word_start
, gsize word_len
,
1213 char *match_expr
, GString
** compl, gsize
* num
)
1221 off_t last_byte
, start
= -1;
1223 gboolean entire_file
;
1224 edit_search_status_msg_t esm
;
1227 srch
= mc_search_new (match_expr
, cp_source
);
1229 srch
= mc_search_new (match_expr
, NULL
);
1235 mc_config_get_bool (mc_global
.main_config
, CONFIG_APP_SECTION
,
1236 "editor_wordcompletion_collect_entire_file", 0);
1238 last_byte
= entire_file
? edit
->buffer
.size
: word_start
;
1240 srch
->search_type
= MC_SEARCH_T_REGEX
;
1241 srch
->is_case_sensitive
= TRUE
;
1242 srch
->search_fn
= edit_search_cmd_callback
;
1243 srch
->update_fn
= edit_search_update_callback
;
1247 esm
.offset
= entire_file
? 0 : word_start
;
1249 status_msg_init (STATUS_MSG (&esm
), _("Collect completions"), 1.0, simple_status_msg_init_cb
,
1250 edit_search_status_update_cb
, NULL
);
1252 current_word
= edit_collect_completions_get_current_word (&esm
, srch
, word_start
);
1254 temp
= g_string_new ("");
1256 /* collect max MAX_WORD_COMPLETIONS completions */
1257 while (mc_search_run (srch
, (void *) &esm
, start
+ 1, last_byte
, &len
))
1259 g_string_set_size (temp
, 0);
1260 start
= srch
->normal_offset
;
1262 /* add matched completion if not yet added */
1263 for (i
= 0; i
< len
; i
++)
1265 skip
= edit_buffer_get_byte (&edit
->buffer
, start
+ i
);
1269 /* skip current word */
1270 if (start
+ (off_t
) i
== word_start
)
1273 g_string_append_c (temp
, skip
);
1279 if (current_word
!= NULL
&& strcmp (current_word
, temp
->str
) == 0)
1284 for (i
= 0; i
< *num
; i
++)
1287 ((char *) &compl[i
]->str
[word_len
],
1288 (char *) &temp
->str
[word_len
], MAX (len
, compl[i
]->len
) - word_len
) == 0)
1290 GString
*this = compl[i
];
1291 for (++i
; i
< *num
; i
++)
1292 compl[i
- 1] = compl[i
];
1293 compl[*num
- 1] = this;
1295 break; /* skip it, already added */
1301 if (*num
== MAX_WORD_COMPLETIONS
)
1303 g_string_free (compl[0], TRUE
);
1304 for (i
= 1; i
< *num
; i
++)
1305 compl[i
- 1] = compl[i
];
1311 recoded
= str_convert_to_display (temp
->str
);
1313 if (recoded
&& recoded
->len
)
1314 g_string_assign (temp
, recoded
->str
);
1316 g_string_free (recoded
, TRUE
);
1319 compl[(*num
)++] = g_string_new_len (temp
->str
, temp
->len
);
1322 /* note the maximal length needed for the completion dialog */
1327 status_msg_deinit (STATUS_MSG (&esm
));
1328 mc_search_free (srch
);
1329 g_string_free (temp
, TRUE
);
1330 g_free (current_word
);
1335 /* --------------------------------------------------------------------------------------------- */
1338 edit_insert_column_of_text (WEdit
* edit
, unsigned char *data
, off_t size
, long width
,
1339 off_t
* start_pos
, off_t
* end_pos
, long *col1
, long *col2
)
1344 cursor
= edit
->buffer
.curs1
;
1345 col
= edit_get_col (edit
);
1347 for (i
= 0; i
< size
; i
++)
1349 if (data
[i
] != '\n')
1350 edit_insert (edit
, data
[i
]);
1352 { /* fill in and move to next line */
1356 if (edit_buffer_get_current_byte (&edit
->buffer
) != '\n')
1358 for (l
= width
- (edit_get_col (edit
) - col
); l
> 0; l
-= space_width
)
1359 edit_insert (edit
, ' ');
1361 for (p
= edit
->buffer
.curs1
;; p
++)
1363 if (p
== edit
->buffer
.size
)
1365 edit_cursor_move (edit
, edit
->buffer
.size
- edit
->buffer
.curs1
);
1366 edit_insert_ahead (edit
, '\n');
1370 if (edit_buffer_get_byte (&edit
->buffer
, p
) == '\n')
1376 edit_cursor_move (edit
, edit_move_forward3 (edit
, p
, col
, 0) - edit
->buffer
.curs1
);
1378 for (l
= col
- edit_get_col (edit
); l
>= space_width
; l
-= space_width
)
1379 edit_insert (edit
, ' ');
1384 *col2
= col
+ width
;
1385 *start_pos
= cursor
;
1386 *end_pos
= edit
->buffer
.curs1
;
1387 edit_cursor_move (edit
, cursor
- edit
->buffer
.curs1
);
1390 /* --------------------------------------------------------------------------------------------- */
1393 edit_macro_comparator (gconstpointer
* macro1
, gconstpointer
* macro2
)
1395 const macros_t
*m1
= (const macros_t
*) macro1
;
1396 const macros_t
*m2
= (const macros_t
*) macro2
;
1398 return m1
->hotkey
- m2
->hotkey
;
1401 /* --------------------------------------------------------------------------------------------- */
1404 edit_macro_sort_by_hotkey (void)
1406 if (macros_list
!= NULL
&& macros_list
->len
!= 0)
1407 g_array_sort (macros_list
, (GCompareFunc
) edit_macro_comparator
);
1410 /* --------------------------------------------------------------------------------------------- */
1413 edit_get_macro (WEdit
* edit
, int hotkey
, const macros_t
** macros
, guint
* indx
)
1415 const macros_t
*array_start
= &g_array_index (macros_list
, struct macros_t
, 0);
1417 macros_t search_macro
;
1421 search_macro
.hotkey
= hotkey
;
1422 result
= bsearch (&search_macro
, macros_list
->data
, macros_list
->len
,
1423 sizeof (macros_t
), (GCompareFunc
) edit_macro_comparator
);
1425 if (result
!= NULL
&& result
->macro
!= NULL
)
1427 *indx
= (result
- array_start
);
1435 /* --------------------------------------------------------------------------------------------- */
1436 /** returns FALSE on error */
1439 edit_delete_macro (WEdit
* edit
, int hotkey
)
1441 mc_config_t
*macros_config
= NULL
;
1442 const char *section_name
= "editor";
1443 gchar
*macros_fname
;
1446 const macros_t
*macros
= NULL
;
1448 /* clear array of actions for current hotkey */
1449 while (edit_get_macro (edit
, hotkey
, ¯os
, &indx
))
1451 if (macros
->macro
!= NULL
)
1452 g_array_free (macros
->macro
, TRUE
);
1454 g_array_remove_index (macros_list
, indx
);
1455 edit_macro_sort_by_hotkey ();
1458 macros_fname
= mc_config_get_full_path (MC_MACRO_FILE
);
1459 macros_config
= mc_config_init (macros_fname
, FALSE
);
1460 g_free (macros_fname
);
1462 if (macros_config
== NULL
)
1465 skeyname
= lookup_key_by_code (hotkey
);
1466 while (mc_config_del_key (macros_config
, section_name
, skeyname
))
1469 mc_config_save_file (macros_config
, NULL
);
1470 mc_config_deinit (macros_config
);
1474 /* --------------------------------------------------------------------------------------------- */
1476 * Callback for the iteration of objects in the 'editors' array.
1477 * Toggle syntax highlighting in editor object.
1479 * @param data probably WEdit object
1480 * @param user_data unused
1484 edit_syntax_onoff_cb (void *data
, void *user_data
)
1488 if (edit_widget_is_editor (CONST_WIDGET (data
)))
1490 WEdit
*edit
= (WEdit
*) data
;
1492 if (option_syntax_highlighting
)
1493 edit_load_syntax (edit
, NULL
, edit
->syntax_type
);
1494 edit
->force
|= REDRAW_PAGE
;
1498 /* --------------------------------------------------------------------------------------------- */
1500 * Callback for the iteration of objects in the 'editors' array.
1501 * Redraw editor object.
1503 * @param data probably WEdit object
1504 * @param user_data unused
1508 edit_redraw_page_cb (void *data
, void *user_data
)
1512 if (edit_widget_is_editor (CONST_WIDGET (data
)))
1513 ((WEdit
*) data
)->force
|= REDRAW_PAGE
;
1516 /* --------------------------------------------------------------------------------------------- */
1518 * Insert autocompleted word into editor.
1520 * @param edit editor object
1521 * @param completion word for completion
1522 * @param word_len offset from beginning for insert
1526 edit_complete_word_insert_recoded_completion (WEdit
* edit
, char *completion
, gsize word_len
)
1531 temp
= str_convert_to_input (completion
);
1533 for (completion
= temp
->str
+ word_len
; *completion
!= '\0'; completion
++)
1534 edit_insert (edit
, *completion
);
1535 g_string_free (temp
, TRUE
);
1537 for (completion
+= word_len
; *completion
!= '\0'; completion
++)
1538 edit_insert (edit
, *completion
);
1542 /* --------------------------------------------------------------------------------------------- */
1543 /*** public functions ****************************************************************************/
1544 /* --------------------------------------------------------------------------------------------- */
1547 edit_refresh_cmd (void)
1554 /* --------------------------------------------------------------------------------------------- */
1556 * Toggle syntax highlighting in all editor windows.
1558 * @param h root widget for all windows
1562 edit_syntax_onoff_cmd (WDialog
* h
)
1564 option_syntax_highlighting
= !option_syntax_highlighting
;
1565 g_list_foreach (h
->widgets
, edit_syntax_onoff_cb
, NULL
);
1569 /* --------------------------------------------------------------------------------------------- */
1571 * Toggle tabs showing in all editor windows.
1573 * @param h root widget for all windows
1577 edit_show_tabs_tws_cmd (WDialog
* h
)
1579 enable_show_tabs_tws
= !enable_show_tabs_tws
;
1580 g_list_foreach (h
->widgets
, edit_redraw_page_cb
, NULL
);
1584 /* --------------------------------------------------------------------------------------------- */
1586 * Toggle right margin showing in all editor windows.
1588 * @param h root widget for all windows
1592 edit_show_margin_cmd (WDialog
* h
)
1594 show_right_margin
= !show_right_margin
;
1595 g_list_foreach (h
->widgets
, edit_redraw_page_cb
, NULL
);
1599 /* --------------------------------------------------------------------------------------------- */
1601 * Toggle line numbers showing in all editor windows.
1603 * @param h root widget for all windows
1607 edit_show_numbers_cmd (WDialog
* h
)
1609 option_line_state
= !option_line_state
;
1610 option_line_state_width
= option_line_state
? LINE_STATE_WIDTH
: 0;
1611 g_list_foreach (h
->widgets
, edit_redraw_page_cb
, NULL
);
1615 /* --------------------------------------------------------------------------------------------- */
1618 edit_save_mode_cmd (void)
1620 char *str_result
= NULL
;
1622 const char *str
[] = {
1625 N_("&Do backups with following extension:")
1631 for (i
= 0; i
< 3; i
++)
1635 g_assert (option_backup_ext
!= NULL
);
1638 quick_widget_t quick_widgets
[] = {
1640 QUICK_RADIO (3, str
, &option_save_mode
, &edit_save_mode_radio_id
),
1641 QUICK_INPUT (option_backup_ext
, "edit-backup-ext", &str_result
,
1642 &edit_save_mode_input_id
, FALSE
, FALSE
, INPUT_COMPLETE_NONE
),
1643 QUICK_SEPARATOR (TRUE
),
1644 QUICK_CHECKBOX (N_("Check &POSIX new line"), &option_check_nl_at_eof
, NULL
),
1645 QUICK_BUTTONS_OK_CANCEL
,
1650 quick_dialog_t qdlg
= {
1652 N_("Edit Save Mode"), "[Edit Save Mode]",
1653 quick_widgets
, edit_save_mode_callback
, NULL
1656 if (quick_dialog (&qdlg
) != B_CANCEL
)
1658 g_free (option_backup_ext
);
1659 option_backup_ext
= str_result
;
1664 /* --------------------------------------------------------------------------------------------- */
1667 edit_set_filename (WEdit
* edit
, const vfs_path_t
* name_vpath
)
1669 vfs_path_free (edit
->filename_vpath
);
1670 edit
->filename_vpath
= vfs_path_clone (name_vpath
);
1672 if (edit
->dir_vpath
== NULL
)
1673 edit
->dir_vpath
= vfs_path_clone (vfs_get_raw_current_dir ());
1676 /* --------------------------------------------------------------------------------------------- */
1677 /* Here we want to warn the users of overwriting an existing file,
1678 but only if they have made a change to the filename */
1679 /* returns TRUE on success */
1681 edit_save_as_cmd (WEdit
* edit
)
1683 /* This heads the 'Save As' dialog box */
1684 vfs_path_t
*exp_vpath
;
1686 int different_filename
= 0;
1688 if (!edit_check_newline (&edit
->buffer
))
1691 exp_vpath
= edit_get_save_file_as (edit
);
1692 edit_push_undo_action (edit
, KEY_PRESS
+ edit
->start_display
);
1694 if (exp_vpath
!= NULL
)
1696 if (vfs_path_len (exp_vpath
) == 0)
1702 if (!vfs_path_equal (edit
->filename_vpath
, exp_vpath
))
1707 if (mc_stat (exp_vpath
, &sb
) == 0 && !S_ISREG (sb
.st_mode
))
1709 edit_error_dialog (_("Save as"),
1711 ("Cannot save: destination is not a regular file")));
1715 different_filename
= 1;
1716 file
= mc_open (exp_vpath
, O_RDONLY
| O_BINARY
);
1720 /* the file exists */
1722 /* Overwrite the current file or cancel the operation */
1723 if (edit_query_dialog2
1725 _("A file already exists with this name"), _("&Overwrite"), _("&Cancel")))
1730 edit
->stat1
.st_mode
|= S_IWUSR
;
1732 save_lock
= lock_file (exp_vpath
);
1736 /* filenames equal, check if already locked */
1737 if (!edit
->locked
&& !edit
->delete_file
)
1738 save_lock
= lock_file (exp_vpath
);
1741 if (different_filename
)
1744 * Allow user to write into saved (under another name) file
1745 * even if original file had r/o user permissions.
1747 edit
->stat1
.st_mode
|= S_IWRITE
;
1750 rv
= edit_save_file (edit
, exp_vpath
);
1754 /* Successful, so unlock both files */
1755 if (different_filename
)
1758 unlock_file (exp_vpath
);
1760 edit
->locked
= unlock_file (edit
->filename_vpath
);
1764 if (edit
->locked
|| save_lock
)
1765 edit
->locked
= unlock_file (edit
->filename_vpath
);
1768 edit_set_filename (edit
, exp_vpath
);
1769 if (edit
->lb
!= LB_ASIS
)
1770 edit_reload (edit
, exp_vpath
);
1772 edit
->delete_file
= 0;
1773 if (different_filename
)
1774 edit_load_syntax (edit
, NULL
, edit
->syntax_type
);
1775 vfs_path_free (exp_vpath
);
1776 edit
->force
|= REDRAW_COMPLETELY
;
1779 edit_error_dialog (_("Save as"), get_sys_error (_("Cannot save file")));
1782 /* Failed, so maintain modify (not save) lock */
1784 unlock_file (exp_vpath
);
1791 vfs_path_free (exp_vpath
);
1792 edit
->force
|= REDRAW_COMPLETELY
;
1796 /* {{{ Macro stuff starts here */
1797 /* --------------------------------------------------------------------------------------------- */
1800 edit_delete_macro_cmd (WEdit
* edit
)
1804 hotkey
= editcmd_dialog_raw_key_query (_("Delete macro"), _("Press macro hotkey:"), TRUE
);
1806 if (hotkey
!= 0 && !edit_delete_macro (edit
, hotkey
))
1807 message (D_ERROR
, _("Delete macro"), _("Macro not deleted"));
1810 /* --------------------------------------------------------------------------------------------- */
1812 /** returns FALSE on error */
1814 edit_execute_macro (WEdit
* edit
, int hotkey
)
1816 gboolean res
= FALSE
;
1820 const macros_t
*macros
;
1823 if (edit_get_macro (edit
, hotkey
, ¯os
, &indx
) &&
1824 macros
->macro
!= NULL
&& macros
->macro
->len
!= 0)
1828 edit
->force
|= REDRAW_PAGE
;
1830 for (i
= 0; i
< macros
->macro
->len
; i
++)
1832 const macro_action_t
*m_act
;
1834 m_act
= &g_array_index (macros
->macro
, struct macro_action_t
, i
);
1835 edit_execute_cmd (edit
, m_act
->action
, m_act
->ch
);
1844 /* --------------------------------------------------------------------------------------------- */
1846 /** returns FALSE on error */
1848 edit_store_macro_cmd (WEdit
* edit
)
1852 GString
*marcros_string
;
1853 mc_config_t
*macros_config
= NULL
;
1854 const char *section_name
= "editor";
1855 gchar
*macros_fname
;
1856 GArray
*macros
; /* current macro */
1858 gboolean have_macro
= FALSE
;
1859 char *skeyname
= NULL
;
1862 editcmd_dialog_raw_key_query (_("Save macro"), _("Press the macro's new hotkey:"), TRUE
);
1863 if (hotkey
== ESC_CHAR
)
1866 tmp_act
= keybind_lookup_keymap_command (editor_map
, hotkey
);
1868 /* return FALSE if try assign macro into restricted hotkeys */
1869 if (tmp_act
== CK_MacroStartRecord
1870 || tmp_act
== CK_MacroStopRecord
|| tmp_act
== CK_MacroStartStopRecord
)
1873 edit_delete_macro (edit
, hotkey
);
1875 macros_fname
= mc_config_get_full_path (MC_MACRO_FILE
);
1876 macros_config
= mc_config_init (macros_fname
, FALSE
);
1877 g_free (macros_fname
);
1879 if (macros_config
== NULL
)
1882 edit_push_undo_action (edit
, KEY_PRESS
+ edit
->start_display
);
1884 marcros_string
= g_string_sized_new (250);
1885 macros
= g_array_new (TRUE
, FALSE
, sizeof (macro_action_t
));
1887 skeyname
= lookup_key_by_code (hotkey
);
1889 for (i
= 0; i
< macro_index
; i
++)
1891 macro_action_t m_act
;
1892 const char *action_name
;
1894 action_name
= keybind_lookup_actionname (record_macro_buf
[i
].action
);
1896 if (action_name
== NULL
)
1899 m_act
.action
= record_macro_buf
[i
].action
;
1900 m_act
.ch
= record_macro_buf
[i
].ch
;
1901 g_array_append_val (macros
, m_act
);
1903 g_string_append_printf (marcros_string
, "%s:%i;", action_name
,
1904 (int) record_macro_buf
[i
].ch
);
1909 macro
.hotkey
= hotkey
;
1910 macro
.macro
= macros
;
1911 g_array_append_val (macros_list
, macro
);
1912 mc_config_set_string (macros_config
, section_name
, skeyname
, marcros_string
->str
);
1915 mc_config_del_key (macros_config
, section_name
, skeyname
);
1918 edit_macro_sort_by_hotkey ();
1920 g_string_free (marcros_string
, TRUE
);
1921 mc_config_save_file (macros_config
, NULL
);
1922 mc_config_deinit (macros_config
);
1926 /* --------------------------------------------------------------------------------------------- */
1929 edit_repeat_macro_cmd (WEdit
* edit
)
1936 f
= input_dialog (_("Repeat last commands"), _("Repeat times:"), MC_HISTORY_EDIT_REPEAT
, NULL
,
1937 INPUT_COMPLETE_NONE
);
1938 if (f
== NULL
|| *f
== '\0')
1944 count_repeat
= strtol (f
, &error
, 0);
1954 edit_push_undo_action (edit
, KEY_PRESS
+ edit
->start_display
);
1955 edit
->force
|= REDRAW_PAGE
;
1957 for (j
= 0; j
< count_repeat
; j
++)
1958 for (i
= 0; i
< macro_index
; i
++)
1959 edit_execute_cmd (edit
, record_macro_buf
[i
].action
, record_macro_buf
[i
].ch
);
1960 edit_update_screen (edit
);
1964 /* --------------------------------------------------------------------------------------------- */
1965 /** return FALSE on error */
1968 edit_load_macro_cmd (WEdit
* edit
)
1970 mc_config_t
*macros_config
= NULL
;
1971 gchar
**profile_keys
, **keys
;
1972 gchar
**values
, **curr_values
;
1973 const char *section_name
= "editor";
1974 gchar
*macros_fname
;
1978 if (macros_list
== NULL
|| macros_list
->len
!= 0)
1981 macros_fname
= mc_config_get_full_path (MC_MACRO_FILE
);
1982 macros_config
= mc_config_init (macros_fname
, TRUE
);
1983 g_free (macros_fname
);
1985 if (macros_config
== NULL
)
1988 keys
= mc_config_get_keys (macros_config
, section_name
, NULL
);
1990 for (profile_keys
= keys
; *profile_keys
!= NULL
; profile_keys
++)
1993 gboolean have_macro
= FALSE
;
1997 macros
= g_array_new (TRUE
, FALSE
, sizeof (macro_action_t
));
1998 values
= mc_config_get_string_list (macros_config
, section_name
, *profile_keys
, NULL
);
1999 hotkey
= lookup_key (*profile_keys
, NULL
);
2001 for (curr_values
= values
; *curr_values
!= NULL
&& *curr_values
[0] != '\0'; curr_values
++)
2003 char **macro_pair
= NULL
;
2005 macro_pair
= g_strsplit (*curr_values
, ":", 2);
2006 if (macro_pair
!= NULL
)
2008 macro_action_t m_act
;
2009 if (macro_pair
[0] == NULL
|| macro_pair
[0][0] == '\0')
2013 m_act
.action
= keybind_lookup_action (macro_pair
[0]);
2014 MC_PTR_FREE (macro_pair
[0]);
2016 if (macro_pair
[1] == NULL
|| macro_pair
[1][0] == '\0')
2020 m_act
.ch
= strtol (macro_pair
[1], NULL
, 0);
2021 MC_PTR_FREE (macro_pair
[1]);
2023 if (m_act
.action
!= 0)
2025 /* a shell command */
2026 if ((m_act
.action
/ CK_PipeBlock (0)) == 1)
2028 m_act
.action
= CK_PipeBlock (0) + (m_act
.ch
> 0 ? m_act
.ch
: 0);
2031 g_array_append_val (macros
, m_act
);
2034 g_strfreev (macro_pair
);
2040 macro
.hotkey
= hotkey
;
2041 macro
.macro
= macros
;
2042 g_array_append_val (macros_list
, macro
);
2044 g_strfreev (values
);
2047 mc_config_deinit (macros_config
);
2048 edit_macro_sort_by_hotkey ();
2052 /* }}} Macro stuff end here */
2054 /* --------------------------------------------------------------------------------------------- */
2055 /** returns TRUE on success */
2058 edit_save_confirm_cmd (WEdit
* edit
)
2060 if (edit
->filename_vpath
== NULL
)
2061 return edit_save_as_cmd (edit
);
2063 if (!edit_check_newline (&edit
->buffer
))
2066 if (edit_confirm_save
)
2071 f
= g_strdup_printf (_("Confirm save file: \"%s\""),
2072 vfs_path_as_str (edit
->filename_vpath
));
2073 ok
= (edit_query_dialog2 (_("Save file"), f
, _("&Save"), _("&Cancel")) == 0);
2078 return edit_save_cmd (edit
);
2081 /* --------------------------------------------------------------------------------------------- */
2083 * Ask file to edit and load it.
2085 * @return TRUE on success or cancel of ask.
2089 edit_load_cmd (WDialog
* h
)
2092 gboolean ret
= TRUE
; /* possible cancel */
2094 exp
= input_expand_dialog (_("Load"), _("Enter file name:"),
2095 MC_HISTORY_EDIT_LOAD
, INPUT_LAST_TEXT
,
2096 INPUT_COMPLETE_FILENAMES
| INPUT_COMPLETE_CD
);
2098 if (exp
!= NULL
&& *exp
!= '\0')
2100 vfs_path_t
*exp_vpath
;
2102 exp_vpath
= vfs_path_from_str (exp
);
2103 ret
= edit_load_file_from_filename (h
, exp_vpath
);
2104 vfs_path_free (exp_vpath
);
2112 /* --------------------------------------------------------------------------------------------- */
2114 * Load syntax file to edit.
2116 * @return TRUE on success
2120 edit_load_syntax_file (WDialog
* h
)
2122 vfs_path_t
*extdir_vpath
;
2124 gboolean ret
= FALSE
;
2126 if (geteuid () == 0)
2127 dir
= query_dialog (_("Syntax file edit"),
2128 _("Which syntax file you want to edit?"), D_NORMAL
, 2,
2129 _("&User"), _("&System wide"));
2132 vfs_path_build_filename (mc_global
.sysconfig_dir
, "syntax", "Syntax", (char *) NULL
);
2133 if (!exist_file (vfs_path_get_last_path_str (extdir_vpath
)))
2135 vfs_path_free (extdir_vpath
);
2137 vfs_path_build_filename (mc_global
.share_data_dir
, "syntax", "Syntax", (char *) NULL
);
2142 vfs_path_t
*user_syntax_file_vpath
;
2144 user_syntax_file_vpath
= mc_config_get_full_vpath (EDIT_SYNTAX_FILE
);
2145 check_for_default (extdir_vpath
, user_syntax_file_vpath
);
2146 ret
= edit_load_file_from_filename (h
, user_syntax_file_vpath
);
2147 vfs_path_free (user_syntax_file_vpath
);
2150 ret
= edit_load_file_from_filename (h
, extdir_vpath
);
2152 vfs_path_free (extdir_vpath
);
2157 /* --------------------------------------------------------------------------------------------- */
2159 * Load menu file to edit.
2161 * @return TRUE on success
2165 edit_load_menu_file (WDialog
* h
)
2167 vfs_path_t
*buffer_vpath
;
2168 vfs_path_t
*menufile_vpath
;
2173 dir
= query_dialog (_("Menu edit"),
2174 _("Which menu file do you want to edit?"), D_NORMAL
,
2175 geteuid () != 0 ? 2 : 3, _("&Local"), _("&User"), _("&System wide"));
2178 vfs_path_build_filename (mc_global
.sysconfig_dir
, EDIT_GLOBAL_MENU
, (char *) NULL
);
2179 if (!exist_file (vfs_path_get_last_path_str (menufile_vpath
)))
2181 vfs_path_free (menufile_vpath
);
2183 vfs_path_build_filename (mc_global
.share_data_dir
, EDIT_GLOBAL_MENU
, (char *) NULL
);
2189 buffer_vpath
= vfs_path_from_str (EDIT_LOCAL_MENU
);
2190 check_for_default (menufile_vpath
, buffer_vpath
);
2191 chmod (vfs_path_get_last_path_str (buffer_vpath
), 0600);
2195 buffer_vpath
= mc_config_get_full_vpath (EDIT_HOME_MENU
);
2196 check_for_default (menufile_vpath
, buffer_vpath
);
2201 vfs_path_build_filename (mc_global
.sysconfig_dir
, EDIT_GLOBAL_MENU
, (char *) NULL
);
2202 if (!exist_file (vfs_path_get_last_path_str (buffer_vpath
)))
2204 vfs_path_free (buffer_vpath
);
2206 vfs_path_build_filename (mc_global
.share_data_dir
, EDIT_GLOBAL_MENU
, (char *) NULL
);
2211 vfs_path_free (menufile_vpath
);
2215 ret
= edit_load_file_from_filename (h
, buffer_vpath
);
2217 vfs_path_free (buffer_vpath
);
2218 vfs_path_free (menufile_vpath
);
2223 /* --------------------------------------------------------------------------------------------- */
2225 * Close window with opened file.
2227 * @return TRUE if file was closed.
2231 edit_close_cmd (WEdit
* edit
)
2235 ret
= (edit
!= NULL
) && edit_ok_to_exit (edit
);
2239 WDialog
*h
= WIDGET (edit
)->owner
;
2241 if (edit
->locked
!= 0)
2242 unlock_file (edit
->filename_vpath
);
2246 if (edit_widget_is_editor (CONST_WIDGET (h
->current
->data
)))
2247 edit
= (WEdit
*) h
->current
->data
;
2250 edit
= find_editor (h
);
2252 widget_select (WIDGET (edit
));
2257 edit
->force
|= REDRAW_COMPLETELY
;
2262 /* --------------------------------------------------------------------------------------------- */
2264 if mark2 is -1 then marking is from mark1 to the cursor.
2265 Otherwise its between the markers. This handles this.
2266 Returns FALSE if no text is marked.
2270 eval_marks (WEdit
* edit
, off_t
* start_mark
, off_t
* end_mark
)
2274 if (edit
->mark1
== edit
->mark2
)
2276 *start_mark
= *end_mark
= 0;
2277 edit
->column2
= edit
->column1
= 0;
2281 if (edit
->end_mark_curs
< 0)
2282 end_mark_curs
= edit
->buffer
.curs1
;
2284 end_mark_curs
= edit
->end_mark_curs
;
2286 if (edit
->mark2
>= 0)
2288 *start_mark
= MIN (edit
->mark1
, edit
->mark2
);
2289 *end_mark
= MAX (edit
->mark1
, edit
->mark2
);
2293 *start_mark
= MIN (edit
->mark1
, end_mark_curs
);
2294 *end_mark
= MAX (edit
->mark1
, end_mark_curs
);
2295 edit
->column2
= edit
->curs_col
+ edit
->over_col
;
2298 if (edit
->column_highlight
2299 && ((edit
->mark1
> end_mark_curs
&& edit
->column1
< edit
->column2
)
2300 || (edit
->mark1
< end_mark_curs
&& edit
->column1
> edit
->column2
)))
2302 off_t start_bol
, start_eol
;
2303 off_t end_bol
, end_eol
;
2307 start_bol
= edit_buffer_get_bol (&edit
->buffer
, *start_mark
);
2308 start_eol
= edit_buffer_get_eol (&edit
->buffer
, start_bol
- 1) + 1;
2309 end_bol
= edit_buffer_get_bol (&edit
->buffer
, *end_mark
);
2310 end_eol
= edit_buffer_get_eol (&edit
->buffer
, *end_mark
);
2311 col1
= MIN (edit
->column1
, edit
->column2
);
2312 col2
= MAX (edit
->column1
, edit
->column2
);
2314 diff1
= edit_move_forward3 (edit
, start_bol
, col2
, 0) -
2315 edit_move_forward3 (edit
, start_bol
, col1
, 0);
2316 diff2
= edit_move_forward3 (edit
, end_bol
, col2
, 0) -
2317 edit_move_forward3 (edit
, end_bol
, col1
, 0);
2319 *start_mark
-= diff1
;
2321 *start_mark
= MAX (*start_mark
, start_eol
);
2322 *end_mark
= MIN (*end_mark
, end_eol
);
2327 /* --------------------------------------------------------------------------------------------- */
2330 edit_block_copy_cmd (WEdit
* edit
)
2332 off_t start_mark
, end_mark
, current
= edit
->buffer
.curs1
;
2333 off_t mark1
= 0, mark2
= 0;
2334 long c1
= 0, c2
= 0;
2336 unsigned char *copy_buf
;
2338 edit_update_curs_col (edit
);
2339 if (!eval_marks (edit
, &start_mark
, &end_mark
))
2342 copy_buf
= edit_get_block (edit
, start_mark
, end_mark
, &size
);
2344 /* all that gets pushed are deletes hence little space is used on the stack */
2346 edit_push_markers (edit
);
2348 if (edit
->column_highlight
)
2352 col_delta
= labs (edit
->column2
- edit
->column1
);
2353 edit_insert_column_of_text (edit
, copy_buf
, size
, col_delta
, &mark1
, &mark2
, &c1
, &c2
);
2357 int size_orig
= size
;
2360 edit_insert_ahead (edit
, copy_buf
[size
]);
2362 /* Place cursor at the end of text selection */
2363 if (option_cursor_after_inserted_block
)
2364 edit_cursor_move (edit
, size_orig
);
2368 edit_scroll_screen_over_cursor (edit
);
2370 if (edit
->column_highlight
)
2371 edit_set_markers (edit
, edit
->buffer
.curs1
, mark2
, c1
, c2
);
2372 else if (start_mark
< current
&& end_mark
> current
)
2373 edit_set_markers (edit
, start_mark
, end_mark
+ end_mark
- start_mark
, 0, 0);
2375 edit
->force
|= REDRAW_PAGE
;
2379 /* --------------------------------------------------------------------------------------------- */
2382 edit_block_move_cmd (WEdit
* edit
)
2385 unsigned char *copy_buf
= NULL
;
2386 off_t start_mark
, end_mark
;
2388 if (!eval_marks (edit
, &start_mark
, &end_mark
))
2391 if (!edit
->column_highlight
&& edit
->buffer
.curs1
> start_mark
&& edit
->buffer
.curs1
< end_mark
)
2394 if (edit
->mark2
< 0)
2395 edit_mark_cmd (edit
, FALSE
);
2396 edit_push_markers (edit
);
2398 if (edit
->column_highlight
)
2402 long c1
, c2
, b_width
;
2405 c1
= MIN (edit
->column1
, edit
->column2
);
2406 c2
= MAX (edit
->column1
, edit
->column2
);
2409 edit_update_curs_col (edit
);
2412 x2
= x
+ edit
->over_col
;
2414 /* do nothing when cursor inside first line of selected area */
2415 if ((edit_buffer_get_eol (&edit
->buffer
, edit
->buffer
.curs1
) ==
2416 edit_buffer_get_eol (&edit
->buffer
, start_mark
)) && x2
> c1
&& x2
<= c2
)
2419 if (edit
->buffer
.curs1
> start_mark
2420 && edit
->buffer
.curs1
< edit_buffer_get_eol (&edit
->buffer
, end_mark
))
2424 else if (x
> c1
&& x
<= c2
)
2427 /* save current selection into buffer */
2428 copy_buf
= edit_get_block (edit
, start_mark
, end_mark
, &size
);
2430 /* remove current selection */
2431 edit_block_delete_cmd (edit
);
2433 edit
->over_col
= MAX (0, edit
->over_col
- b_width
);
2434 /* calculate the cursor pos after delete block */
2435 current
= edit_move_forward3 (edit
, edit_buffer_get_current_bol (&edit
->buffer
), x
, 0);
2436 edit_cursor_move (edit
, current
- edit
->buffer
.curs1
);
2437 edit_scroll_screen_over_cursor (edit
);
2439 /* add TWS if need before block insertion */
2440 if (option_cursor_beyond_eol
&& edit
->over_col
> 0)
2441 edit_insert_over (edit
);
2443 edit_insert_column_of_text (edit
, copy_buf
, size
, b_width
, &mark1
, &mark2
, &c1
, &c2
);
2444 edit_set_markers (edit
, mark1
, mark2
, c1
, c2
);
2448 off_t count
, count_orig
;
2450 current
= edit
->buffer
.curs1
;
2451 copy_buf
= g_malloc0 (end_mark
- start_mark
);
2452 edit_cursor_move (edit
, start_mark
- edit
->buffer
.curs1
);
2453 edit_scroll_screen_over_cursor (edit
);
2455 for (count
= start_mark
; count
< end_mark
; count
++)
2456 copy_buf
[end_mark
- count
- 1] = edit_delete (edit
, TRUE
);
2458 edit_scroll_screen_over_cursor (edit
);
2459 edit_cursor_move (edit
,
2460 current
- edit
->buffer
.curs1
-
2461 (((current
- edit
->buffer
.curs1
) > 0) ? end_mark
- start_mark
: 0));
2462 edit_scroll_screen_over_cursor (edit
);
2464 while (count
-- > start_mark
)
2465 edit_insert_ahead (edit
, copy_buf
[end_mark
- count
- 1]);
2467 edit_set_markers (edit
, edit
->buffer
.curs1
, edit
->buffer
.curs1
+ end_mark
- start_mark
, 0,
2470 /* Place cursor at the end of text selection */
2471 if (option_cursor_after_inserted_block
)
2472 edit_cursor_move (edit
, count_orig
- start_mark
);
2475 edit_scroll_screen_over_cursor (edit
);
2477 edit
->force
|= REDRAW_PAGE
;
2480 /* --------------------------------------------------------------------------------------------- */
2481 /** returns 1 if canceelled by user */
2484 edit_block_delete_cmd (WEdit
* edit
)
2486 off_t start_mark
, end_mark
;
2488 if (eval_marks (edit
, &start_mark
, &end_mark
))
2489 return edit_block_delete (edit
);
2491 edit_delete_line (edit
);
2495 /* --------------------------------------------------------------------------------------------- */
2496 /** call with edit = 0 before shutdown to close memory leaks */
2499 edit_replace_cmd (WEdit
* edit
, gboolean again
)
2501 /* 1 = search string, 2 = replace with */
2502 static char *saved1
= NULL
; /* saved default[123] */
2503 static char *saved2
= NULL
;
2504 char *input1
= NULL
; /* user input from the dialog */
2505 char *input2
= NULL
;
2506 GString
*input2_str
= NULL
;
2509 long times_replaced
= 0;
2510 gboolean once_found
= FALSE
;
2511 edit_search_status_msg_t esm
;
2515 MC_PTR_FREE (saved1
);
2516 MC_PTR_FREE (saved2
);
2520 edit
->force
|= REDRAW_COMPLETELY
;
2522 if (again
&& !saved1
&& !saved2
)
2527 input1
= g_strdup (saved1
? saved1
: "");
2528 input2
= g_strdup (saved2
? saved2
: "");
2532 char *tmp_inp1
, *tmp_inp2
;
2534 disp1
= edit_replace_cmd__conv_to_display (saved1
? saved1
: "");
2535 disp2
= edit_replace_cmd__conv_to_display (saved2
? saved2
: "");
2537 edit_push_undo_action (edit
, KEY_PRESS
+ edit
->start_display
);
2539 editcmd_dialog_replace_show (edit
, disp1
, disp2
, &input1
, &input2
);
2544 if (input1
== NULL
|| *input1
== '\0')
2546 edit
->force
= REDRAW_COMPLETELY
;
2552 input1
= edit_replace_cmd__conv_to_input (input1
);
2553 input2
= edit_replace_cmd__conv_to_input (input2
);
2558 saved1
= g_strdup (input1
);
2560 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
)))
2615 edit_show_search_error (edit
, _("Search"));
2621 edit
->search_start
= edit
->search
->normal_offset
;
2622 /*returns negative on not found or error in pattern */
2624 if ((edit
->search_start
>= 0) && (edit
->search_start
< edit
->buffer
.size
))
2629 edit
->found_start
= edit
->search_start
;
2630 i
= edit
->found_len
= len
;
2632 edit_cursor_move (edit
, edit
->search_start
- edit
->buffer
.curs1
);
2633 edit_scroll_screen_over_cursor (edit
);
2635 if (edit
->replace_mode
== 0)
2640 l
= edit
->curs_row
- WIDGET (edit
)->lines
/ 3;
2642 edit_scroll_downward (edit
, l
);
2644 edit_scroll_upward (edit
, -l
);
2646 edit_scroll_screen_over_cursor (edit
);
2647 edit
->force
|= REDRAW_PAGE
;
2648 edit_render_keypress (edit
);
2650 /*so that undo stops at each query */
2651 edit_push_key_press (edit
);
2652 /* and prompt 2/3 down */
2653 disp1
= edit_replace_cmd__conv_to_display (saved1
);
2654 disp2
= edit_replace_cmd__conv_to_display (saved2
);
2655 prompt
= editcmd_dialog_replace_prompt_show (edit
, disp1
, disp2
, -1, -1);
2659 if (prompt
== B_REPLACE_ALL
)
2660 edit
->replace_mode
= 1;
2661 else if (prompt
== B_SKIP_REPLACE
)
2663 if (edit_search_options
.backwards
)
2664 edit
->search_start
--;
2666 edit
->search_start
++;
2667 continue; /* loop */
2669 else if (prompt
== B_CANCEL
)
2671 edit
->replace_mode
= -1;
2676 repl_str
= mc_search_prepare_replace_str (edit
->search
, input2_str
);
2678 if (edit
->search
->error
!= MC_SEARCH_E_OK
)
2680 edit_show_search_error (edit
, _("Replace"));
2681 g_string_free (repl_str
, TRUE
);
2685 /* delete then insert new */
2686 for (i
= 0; i
< len
; i
++)
2687 edit_delete (edit
, TRUE
);
2689 for (i
= 0; i
< repl_str
->len
; i
++)
2690 edit_insert (edit
, repl_str
->str
[i
]);
2692 edit
->found_len
= repl_str
->len
;
2693 g_string_free (repl_str
, TRUE
);
2696 /* so that we don't find the same string again */
2697 if (edit_search_options
.backwards
)
2699 edit
->search_start
--;
2703 edit
->search_start
+= edit
->found_len
+ (len
== 0 ? 1 : 0);
2705 if (edit
->search_start
>= edit
->buffer
.size
)
2709 edit_scroll_screen_over_cursor (edit
);
2713 /* try and find from right here for next search */
2714 edit
->search_start
= edit
->buffer
.curs1
;
2715 edit_update_curs_col (edit
);
2717 edit
->force
|= REDRAW_PAGE
;
2718 edit_render_keypress (edit
);
2720 if (times_replaced
== 0)
2721 query_dialog (_("Replace"), _(STR_E_NOTFOUND
), D_NORMAL
, 1, _("&OK"));
2725 while (edit
->replace_mode
>= 0);
2727 status_msg_deinit (STATUS_MSG (&esm
));
2728 edit_scroll_screen_over_cursor (edit
);
2729 edit
->force
|= REDRAW_COMPLETELY
;
2730 edit_render_keypress (edit
);
2732 if ((edit
->replace_mode
== 1) && (times_replaced
!= 0))
2733 message (D_NORMAL
, _("Replace"), _("%ld replacements made"), times_replaced
);
2738 if (input2_str
!= NULL
)
2739 g_string_free (input2_str
, TRUE
);
2742 /* --------------------------------------------------------------------------------------------- */
2745 edit_search_cmd_callback (const void *user_data
, gsize char_offset
, int *current_char
)
2747 WEdit
*edit
= ((const edit_search_status_msg_t
*) user_data
)->edit
;
2749 *current_char
= edit_buffer_get_byte (&edit
->buffer
, (off_t
) char_offset
);
2750 return MC_SEARCH_CB_OK
;
2753 /* --------------------------------------------------------------------------------------------- */
2756 edit_search_update_callback (const void *user_data
, gsize char_offset
)
2758 status_msg_t
*sm
= STATUS_MSG (user_data
);
2760 ((edit_search_status_msg_t
*) sm
)->offset
= (off_t
) char_offset
;
2762 return (sm
->update (sm
) == B_CANCEL
? MC_SEARCH_CB_ABORT
: MC_SEARCH_CB_OK
);
2765 /* --------------------------------------------------------------------------------------------- */
2768 edit_search_cmd (WEdit
* edit
, gboolean again
)
2776 else if (edit
->last_search_string
!= NULL
)
2777 edit_do_search (edit
);
2780 /* find last search string in history */
2783 history
= history_get (MC_HISTORY_SHARED_SEARCH
);
2784 if (history
!= NULL
&& history
->data
!= NULL
)
2786 edit
->last_search_string
= (char *) history
->data
;
2787 history
->data
= NULL
;
2788 history
= g_list_first (history
);
2789 g_list_free_full (history
, g_free
);
2792 edit
->search
= mc_search_new (edit
->last_search_string
, cp_source
);
2794 edit
->search
= mc_search_new (edit
->last_search_string
, NULL
);
2796 if (edit
->search
== NULL
)
2798 /* if not... then ask for an expression */
2799 MC_PTR_FREE (edit
->last_search_string
);
2804 edit
->search
->search_type
= edit_search_options
.type
;
2806 edit
->search
->is_all_charsets
= edit_search_options
.all_codepages
;
2808 edit
->search
->is_case_sensitive
= edit_search_options
.case_sens
;
2809 edit
->search
->whole_words
= edit_search_options
.whole_words
;
2810 edit
->search
->search_fn
= edit_search_cmd_callback
;
2811 edit
->search
->update_fn
= edit_search_update_callback
;
2812 edit
->search_line_type
= edit_get_search_line_type (edit
->search
);
2813 edit_do_search (edit
);
2818 /* if not... then ask for an expression */
2819 MC_PTR_FREE (edit
->last_search_string
);
2826 /* --------------------------------------------------------------------------------------------- */
2828 * Check if it's OK to close the file. If there are unsaved changes, ask user.
2830 * @return TRUE if it's OK to exit, FALSE to continue editing.
2834 edit_ok_to_exit (WEdit
* edit
)
2836 const char *fname
= N_("[NoName]");
2840 if (!edit
->modified
)
2843 if (edit
->filename_vpath
!= NULL
)
2844 fname
= vfs_path_as_str (edit
->filename_vpath
);
2850 if (!mc_global
.midnight_shutdown
)
2854 msg
= g_strdup_printf (_("File %s was modified.\nSave before close?"), fname
);
2855 act
= edit_query_dialog3 (_("Close file"), msg
, _("&Yes"), _("&No"), _("&Cancel"));
2859 msg
= g_strdup_printf (_("Midnight Commander is being shut down.\nSave modified file %s?"),
2861 act
= edit_query_dialog2 (_("Quit"), msg
, _("&Yes"), _("&No"));
2873 if (!mc_global
.midnight_shutdown
&& !edit_check_newline (&edit
->buffer
))
2875 edit_push_markers (edit
);
2876 edit_set_markers (edit
, 0, 0, 0, 0);
2877 if (!edit_save_cmd (edit
) || mc_global
.midnight_shutdown
)
2878 return mc_global
.midnight_shutdown
;
2883 case 2: /* Cancel quit */
2891 /* --------------------------------------------------------------------------------------------- */
2892 /** save block, returns TRUE on success */
2895 edit_save_block (WEdit
* edit
, const char *filename
, off_t start
, off_t finish
)
2901 vpath
= vfs_path_from_str (filename
);
2902 file
= mc_open (vpath
, O_CREAT
| O_WRONLY
| O_TRUNC
,
2903 S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IROTH
| O_BINARY
);
2904 vfs_path_free (vpath
);
2908 if (edit
->column_highlight
)
2912 r
= mc_write (file
, VERTICAL_MAGIC
, sizeof (VERTICAL_MAGIC
));
2915 unsigned char *block
, *p
;
2917 p
= block
= edit_get_block (edit
, start
, finish
, &len
);
2920 r
= mc_write (file
, p
, len
);
2935 len
= finish
- start
;
2936 buf
= g_malloc0 (TEMP_BUF_LEN
);
2937 while (start
!= finish
)
2939 end
= MIN (finish
, start
+ TEMP_BUF_LEN
);
2940 for (; i
< end
; i
++)
2941 buf
[i
- start
] = edit_buffer_get_byte (&edit
->buffer
, i
);
2942 len
-= mc_write (file
, (char *) buf
, end
- start
);
2952 /* --------------------------------------------------------------------------------------------- */
2955 edit_paste_from_history (WEdit
* edit
)
2958 edit_error_dialog (_("Error"), _("This function is not implemented"));
2961 /* --------------------------------------------------------------------------------------------- */
2964 edit_copy_to_X_buf_cmd (WEdit
* edit
)
2966 off_t start_mark
, end_mark
;
2968 if (!eval_marks (edit
, &start_mark
, &end_mark
))
2971 if (!edit_save_block_to_clip_file (edit
, start_mark
, end_mark
))
2973 edit_error_dialog (_("Copy to clipboard"), get_sys_error (_("Unable to save to file")));
2976 /* try use external clipboard utility */
2977 mc_event_raise (MCEVENT_GROUP_CORE
, "clipboard_file_to_ext_clip", NULL
);
2979 if (option_drop_selection_on_copy
)
2980 edit_mark_cmd (edit
, TRUE
);
2985 /* --------------------------------------------------------------------------------------------- */
2988 edit_cut_to_X_buf_cmd (WEdit
* edit
)
2990 off_t start_mark
, end_mark
;
2992 if (!eval_marks (edit
, &start_mark
, &end_mark
))
2995 if (!edit_save_block_to_clip_file (edit
, start_mark
, end_mark
))
2997 edit_error_dialog (_("Cut to clipboard"), _("Unable to save to file"));
3000 /* try use external clipboard utility */
3001 mc_event_raise (MCEVENT_GROUP_CORE
, "clipboard_file_to_ext_clip", NULL
);
3003 edit_block_delete_cmd (edit
);
3004 edit_mark_cmd (edit
, TRUE
);
3009 /* --------------------------------------------------------------------------------------------- */
3012 edit_paste_from_X_buf_cmd (WEdit
* edit
)
3017 /* try use external clipboard utility */
3018 mc_event_raise (MCEVENT_GROUP_CORE
, "clipboard_file_from_ext_clip", NULL
);
3019 tmp
= mc_config_get_full_vpath (EDIT_CLIP_FILE
);
3020 ret
= (edit_insert_file (edit
, tmp
) >= 0);
3021 vfs_path_free (tmp
);
3026 /* --------------------------------------------------------------------------------------------- */
3028 * Ask user for the line and go to that line.
3029 * Negative numbers mean line from the end (i.e. -1 is the last line).
3033 edit_goto_cmd (WEdit
* edit
)
3035 static gboolean first_run
= TRUE
;
3041 f
= input_dialog (_("Goto line"), _("Enter line:"), MC_HISTORY_EDIT_GOTO_LINE
,
3042 first_run
? NULL
: INPUT_LAST_TEXT
, INPUT_COMPLETE_NONE
);
3043 if (f
== NULL
|| *f
== '\0')
3049 l
= strtol (f
, &error
, 0);
3057 l
= edit
->buffer
.lines
+ l
+ 2;
3058 edit_move_display (edit
, l
- WIDGET (edit
)->lines
/ 2 - 1);
3059 edit_move_to_line (edit
, l
- 1);
3060 edit
->force
|= REDRAW_COMPLETELY
;
3067 /* --------------------------------------------------------------------------------------------- */
3068 /** Return TRUE on success */
3071 edit_save_block_cmd (WEdit
* edit
)
3073 off_t start_mark
, end_mark
;
3075 gboolean ret
= FALSE
;
3077 if (!eval_marks (edit
, &start_mark
, &end_mark
))
3080 tmp
= mc_config_get_full_path (EDIT_CLIP_FILE
);
3082 input_expand_dialog (_("Save block"), _("Enter file name:"),
3083 MC_HISTORY_EDIT_SAVE_BLOCK
, tmp
, INPUT_COMPLETE_FILENAMES
);
3085 edit_push_undo_action (edit
, KEY_PRESS
+ edit
->start_display
);
3087 if (exp
!= NULL
&& *exp
!= '\0')
3089 if (edit_save_block (edit
, exp
, start_mark
, end_mark
))
3092 edit_error_dialog (_("Save block"), get_sys_error (_("Cannot save file")));
3094 edit
->force
|= REDRAW_COMPLETELY
;
3103 /* --------------------------------------------------------------------------------------------- */
3105 /** returns TRUE on success */
3107 edit_insert_file_cmd (WEdit
* edit
)
3111 gboolean ret
= FALSE
;
3113 tmp
= mc_config_get_full_path (EDIT_CLIP_FILE
);
3114 exp
= input_expand_dialog (_("Insert file"), _("Enter file name:"),
3115 MC_HISTORY_EDIT_INSERT_FILE
, tmp
, INPUT_COMPLETE_FILENAMES
);
3118 edit_push_undo_action (edit
, KEY_PRESS
+ edit
->start_display
);
3120 if (exp
!= NULL
&& *exp
!= '\0')
3122 vfs_path_t
*exp_vpath
;
3124 exp_vpath
= vfs_path_from_str (exp
);
3125 ret
= (edit_insert_file (edit
, exp_vpath
) >= 0);
3126 vfs_path_free (exp_vpath
);
3129 edit_error_dialog (_("Insert file"), get_sys_error (_("Cannot insert file")));
3134 edit
->force
|= REDRAW_COMPLETELY
;
3138 /* --------------------------------------------------------------------------------------------- */
3139 /** sorts a block, returns -1 on system fail, 1 on cancel and 0 on success */
3142 edit_sort_cmd (WEdit
* edit
)
3144 char *exp
, *tmp
, *tmp_edit_block_name
, *tmp_edit_temp_name
;
3145 off_t start_mark
, end_mark
;
3148 if (!eval_marks (edit
, &start_mark
, &end_mark
))
3150 edit_error_dialog (_("Sort block"), _("You must first highlight a block of text"));
3154 tmp
= mc_config_get_full_path (EDIT_BLOCK_FILE
);
3155 edit_save_block (edit
, tmp
, start_mark
, end_mark
);
3158 exp
= input_dialog (_("Run sort"),
3159 _("Enter sort options (see manpage) separated by whitespace:"),
3160 MC_HISTORY_EDIT_SORT
, INPUT_LAST_TEXT
, INPUT_COMPLETE_NONE
);
3165 tmp_edit_block_name
= mc_config_get_full_path (EDIT_BLOCK_FILE
);
3166 tmp_edit_temp_name
= mc_config_get_full_path (EDIT_TEMP_FILE
);
3168 g_strconcat (" sort ", exp
, " ", tmp_edit_block_name
,
3169 " > ", tmp_edit_temp_name
, (char *) NULL
);
3170 g_free (tmp_edit_temp_name
);
3171 g_free (tmp_edit_block_name
);
3178 if (e
== -1 || e
== 127)
3179 edit_error_dialog (_("Sort"), get_sys_error (_("Cannot execute sort command")));
3184 sprintf (q
, "%d ", e
);
3185 tmp
= g_strdup_printf (_("Sort returned non-zero: %s"), q
);
3186 edit_error_dialog (_("Sort"), tmp
);
3192 edit
->force
|= REDRAW_COMPLETELY
;
3194 if (edit_block_delete_cmd (edit
))
3198 vfs_path_t
*tmp_vpath
;
3200 tmp_vpath
= mc_config_get_full_vpath (EDIT_TEMP_FILE
);
3201 edit_insert_file (edit
, tmp_vpath
);
3202 vfs_path_free (tmp_vpath
);
3207 /* --------------------------------------------------------------------------------------------- */
3209 * Ask user for a command, execute it and paste its output back to the
3214 edit_ext_cmd (WEdit
* edit
)
3216 char *exp
, *tmp
, *tmp_edit_temp_file
;
3220 input_dialog (_("Paste output of external command"),
3221 _("Enter shell command(s):"), MC_HISTORY_EDIT_PASTE_EXTCMD
, INPUT_LAST_TEXT
,
3222 INPUT_COMPLETE_FILENAMES
| INPUT_COMPLETE_VARIABLES
| INPUT_COMPLETE_USERNAMES
3223 | INPUT_COMPLETE_HOSTNAMES
| INPUT_COMPLETE_CD
| INPUT_COMPLETE_COMMANDS
|
3224 INPUT_COMPLETE_SHELL_ESC
);
3229 tmp_edit_temp_file
= mc_config_get_full_path (EDIT_TEMP_FILE
);
3230 tmp
= g_strconcat (exp
, " > ", tmp_edit_temp_file
, (char *) NULL
);
3231 g_free (tmp_edit_temp_file
);
3238 edit_error_dialog (_("External command"), get_sys_error (_("Cannot execute command")));
3242 edit
->force
|= REDRAW_COMPLETELY
;
3245 vfs_path_t
*tmp_vpath
;
3247 tmp_vpath
= mc_config_get_full_vpath (EDIT_TEMP_FILE
);
3248 edit_insert_file (edit
, tmp_vpath
);
3249 vfs_path_free (tmp_vpath
);
3254 /* --------------------------------------------------------------------------------------------- */
3255 /** if block is 1, a block must be highlighted and the shell command
3256 processes it. If block is 0 the shell command is a straight system
3257 command, that just produces some output which is to be inserted */
3260 edit_block_process_cmd (WEdit
* edit
, int macro_number
)
3263 char *macros_fname
= NULL
;
3265 fname
= g_strdup_printf ("%s.%i.sh", MC_EXTMACRO_FILE
, macro_number
);
3266 macros_fname
= g_build_filename (mc_config_get_data_path (), fname
, (char *) NULL
);
3267 user_menu (edit
, macros_fname
, 0);
3269 g_free (macros_fname
);
3270 edit
->force
|= REDRAW_COMPLETELY
;
3273 /* --------------------------------------------------------------------------------------------- */
3276 edit_mail_dialog (WEdit
* edit
)
3278 char *mail_to
, *mail_subject
, *mail_cc
;
3280 quick_widget_t quick_widgets
[] = {
3282 QUICK_LABEL (N_("mail -s <subject> -c <cc> <to>"), NULL
),
3283 QUICK_LABELED_INPUT (N_("To"), input_label_above
,
3284 INPUT_LAST_TEXT
, "mail-dlg-input-3",
3285 &mail_to
, NULL
, FALSE
, FALSE
, INPUT_COMPLETE_USERNAMES
),
3286 QUICK_LABELED_INPUT (N_("Subject"), input_label_above
,
3287 INPUT_LAST_TEXT
, "mail-dlg-input-2",
3288 &mail_subject
, NULL
, FALSE
, FALSE
, INPUT_COMPLETE_NONE
),
3289 QUICK_LABELED_INPUT (N_("Copies to"), input_label_above
,
3290 INPUT_LAST_TEXT
, "mail-dlg-input",
3291 &mail_cc
, NULL
, FALSE
, FALSE
, INPUT_COMPLETE_USERNAMES
),
3292 QUICK_BUTTONS_OK_CANCEL
,
3297 quick_dialog_t qdlg
= {
3299 N_("Mail"), "[Input Line Keys]",
3300 quick_widgets
, NULL
, NULL
3303 if (quick_dialog (&qdlg
) != B_CANCEL
)
3305 pipe_mail (&edit
->buffer
, mail_to
, mail_subject
, mail_cc
);
3307 g_free (mail_subject
);
3312 /* --------------------------------------------------------------------------------------------- */
3314 /*******************/
3315 /* Word Completion */
3316 /*******************/
3319 * Complete current word using regular expression search
3320 * backwards beginning at the current cursor position.
3324 edit_complete_word_cmd (WEdit
* edit
)
3326 gsize i
, max_len
, word_len
= 0, num_compl
= 0;
3327 off_t word_start
= 0;
3328 GString
*match_expr
;
3329 GString
*compl[MAX_WORD_COMPLETIONS
]; /* completions */
3331 /* search start of word to be completed */
3332 if (!edit_find_word_start (&edit
->buffer
, &word_start
, &word_len
))
3335 /* prepare match expression */
3336 /* match_expr = g_strdup_printf ("\\b%.*s[a-zA-Z_0-9]+", word_len, bufpos); */
3337 match_expr
= g_string_new ("(^|\\s+|\\b)");
3338 for (i
= 0; i
< word_len
; i
++)
3339 g_string_append_c (match_expr
, edit_buffer_get_byte (&edit
->buffer
, word_start
+ i
));
3340 g_string_append (match_expr
,
3341 "[^\\s\\.=\\+\\[\\]\\(\\)\\,\\;\\:\\\"\\'\\-\\?\\/\\|\\\\\\{\\}\\*\\&\\^\\%%\\$#@\\!]+");
3343 /* collect the possible completions */
3344 /* start search from begin to end of file */
3346 edit_collect_completions (edit
, word_start
, word_len
, match_expr
->str
, (GString
**) & compl,
3351 /* insert completed word if there is only one match */
3353 edit_complete_word_insert_recoded_completion (edit
, compl[0]->str
, word_len
);
3354 /* more than one possible completion => ask the user */
3359 /* !!! usually only a beep is expected and when <ALT-TAB> is !!! */
3360 /* !!! pressed again the selection dialog pops up, but that !!! */
3361 /* !!! seems to require a further internal state !!! */
3364 /* let the user select the preferred completion */
3365 curr_compl
= editcmd_dialog_completion_show (edit
, max_len
,
3366 (GString
**) & compl, num_compl
);
3368 if (curr_compl
!= NULL
)
3370 edit_complete_word_insert_recoded_completion (edit
, curr_compl
, word_len
);
3371 g_free (curr_compl
);
3376 g_string_free (match_expr
, TRUE
);
3377 /* release memory before return */
3378 for (i
= 0; i
< num_compl
; i
++)
3379 g_string_free (compl[i
], TRUE
);
3382 /* --------------------------------------------------------------------------------------------- */
3386 edit_select_codepage_cmd (WEdit
* edit
)
3388 if (do_select_codepage ())
3389 edit_set_codeset (edit
);
3391 edit
->force
= REDRAW_PAGE
;
3392 widget_redraw (WIDGET (edit
));
3396 /* --------------------------------------------------------------------------------------------- */
3399 edit_insert_literal_cmd (WEdit
* edit
)
3401 int char_for_insertion
;
3403 char_for_insertion
= editcmd_dialog_raw_key_query (_("Insert literal"),
3404 _("Press any key:"), FALSE
);
3405 edit_execute_key_command (edit
, -1, ascii_alpha_to_cntrl (char_for_insertion
));
3408 /* --------------------------------------------------------------------------------------------- */
3411 edit_begin_end_macro_cmd (WEdit
* edit
)
3413 /* edit is a pointer to the widget */
3416 long command
= macro_index
< 0 ? CK_MacroStartRecord
: CK_MacroStopRecord
;
3417 edit_execute_key_command (edit
, command
, -1);
3421 /* --------------------------------------------------------------------------------------------- */
3424 edit_begin_end_repeat_cmd (WEdit
* edit
)
3426 /* edit is a pointer to the widget */
3429 long command
= macro_index
< 0 ? CK_RepeatStartRecord
: CK_RepeatStopRecord
;
3430 edit_execute_key_command (edit
, command
, -1);
3434 /* --------------------------------------------------------------------------------------------- */
3437 edit_load_forward_cmd (WEdit
* edit
)
3440 && edit_query_dialog2 (_("Warning"),
3441 _("Current text was modified without a file save.\n"
3442 "Continue discards these changes"), _("C&ontinue"),
3445 edit
->force
|= REDRAW_COMPLETELY
;
3449 if (edit_stack_iterator
+ 1 >= MAX_HISTORY_MOVETO
)
3452 if (edit_history_moveto
[edit_stack_iterator
+ 1].line
< 1)
3455 edit_stack_iterator
++;
3456 if (edit_history_moveto
[edit_stack_iterator
].filename_vpath
!= NULL
)
3457 return edit_reload_line (edit
, edit_history_moveto
[edit_stack_iterator
].filename_vpath
,
3458 edit_history_moveto
[edit_stack_iterator
].line
);
3463 /* --------------------------------------------------------------------------------------------- */
3466 edit_load_back_cmd (WEdit
* edit
)
3469 && edit_query_dialog2 (_("Warning"),
3470 _("Current text was modified without a file save.\n"
3471 "Continue discards these changes"), _("C&ontinue"),
3474 edit
->force
|= REDRAW_COMPLETELY
;
3478 /* we are in the bottom of the stack, NO WAY! */
3479 if (edit_stack_iterator
== 0)
3482 edit_stack_iterator
--;
3483 if (edit_history_moveto
[edit_stack_iterator
].filename_vpath
!= NULL
)
3484 return edit_reload_line (edit
, edit_history_moveto
[edit_stack_iterator
].filename_vpath
,
3485 edit_history_moveto
[edit_stack_iterator
].line
);
3490 /* --------------------------------------------------------------------------------------------- */
3493 edit_get_match_keyword_cmd (WEdit
* edit
)
3495 gsize word_len
= 0, max_len
= 0;
3498 off_t word_start
= 0;
3499 GString
*match_expr
;
3502 char *tagfile
= NULL
;
3504 etags_hash_t def_hash
[MAX_DEFINITIONS
];
3506 for (i
= 0; i
< MAX_DEFINITIONS
; i
++)
3508 def_hash
[i
].filename
= NULL
;
3511 /* search start of word to be completed */
3512 if (!edit_find_word_start (&edit
->buffer
, &word_start
, &word_len
))
3515 /* prepare match expression */
3516 match_expr
= g_string_sized_new (word_len
);
3517 for (i
= 0; i
< word_len
; i
++)
3518 g_string_append_c (match_expr
, edit_buffer_get_byte (&edit
->buffer
, word_start
+ i
));
3520 ptr
= g_get_current_dir ();
3521 path
= g_strconcat (ptr
, PATH_SEP_STR
, (char *) NULL
);
3524 /* Recursive search file 'TAGS' in parent dirs */
3527 ptr
= g_path_get_dirname (path
);
3531 tagfile
= mc_build_filename (path
, TAGS_NAME
, (char *) NULL
);
3532 if (exist_file (tagfile
))
3535 while (strcmp (path
, PATH_SEP_STR
) != 0);
3540 etags_set_definition_hash (tagfile
, path
, match_expr
->str
, (etags_hash_t
*) & def_hash
);
3545 max_len
= MAX_WIDTH_DEF_DIALOG
;
3549 editcmd_dialog_select_definition_show (edit
, match_expr
->str
, max_len
, word_len
,
3550 (etags_hash_t
*) & def_hash
, num_def
);
3552 g_string_free (match_expr
, TRUE
);
3555 /* --------------------------------------------------------------------------------------------- */
3559 edit_suggest_current_word (WEdit
* edit
)
3563 off_t word_start
= 0;
3564 int retval
= B_SKIP_WORD
;
3565 GString
*match_word
;
3567 /* search start of word to spell check */
3568 match_word
= edit_buffer_get_word_from_pos (&edit
->buffer
, edit
->buffer
.curs1
, &word_start
,
3570 word_len
= match_word
->len
;
3573 if (mc_global
.source_codepage
>= 0 && (mc_global
.source_codepage
!= mc_global
.display_codepage
))
3577 tmp_word
= str_convert_to_display (match_word
->str
);
3578 g_string_free (match_word
, TRUE
);
3579 match_word
= tmp_word
;
3582 if (!aspell_check (match_word
->str
, (int) word_len
))
3588 suggest
= g_array_new (TRUE
, FALSE
, sizeof (char *));
3590 res
= aspell_suggest (suggest
, match_word
->str
, (int) word_len
);
3593 char *new_word
= NULL
;
3595 edit
->found_start
= word_start
;
3596 edit
->found_len
= word_len
;
3597 edit
->force
|= REDRAW_PAGE
;
3598 edit_scroll_screen_over_cursor (edit
);
3599 edit_render_keypress (edit
);
3601 retval
= spell_dialog_spell_suggest_show (edit
, match_word
->str
, &new_word
, suggest
);
3602 edit_cursor_move (edit
, word_len
- cut_len
);
3604 if (retval
== B_ENTER
&& new_word
!= NULL
)
3609 if (mc_global
.source_codepage
>= 0 &&
3610 (mc_global
.source_codepage
!= mc_global
.display_codepage
))
3614 tmp_word
= str_convert_to_input (new_word
);
3616 new_word
= g_string_free (tmp_word
, FALSE
);
3620 for (i
= 0; i
< word_len
; i
++)
3621 edit_backspace (edit
, TRUE
);
3622 for (; *new_word
; new_word
++)
3623 edit_insert (edit
, *new_word
);
3626 else if (retval
== B_ADD_WORD
&& match_word
!= NULL
)
3627 aspell_add_to_dict (match_word
->str
, (int) word_len
);
3631 for (i
= 0; i
< suggest
->len
; i
++)
3633 char *cur_sugg_word
;
3635 cur_sugg_word
= g_array_index (suggest
, char *, i
);
3636 g_free (cur_sugg_word
);
3638 g_array_free (suggest
, TRUE
);
3639 edit
->found_start
= 0;
3640 edit
->found_len
= 0;
3642 g_string_free (match_word
, TRUE
);
3646 /* --------------------------------------------------------------------------------------------- */
3649 edit_spellcheck_file (WEdit
* edit
)
3651 if (edit
->buffer
.curs_line
> 0)
3653 edit_cursor_move (edit
, -edit
->buffer
.curs1
);
3654 edit_move_to_prev_col (edit
, 0);
3655 edit_update_curs_row (edit
);
3662 c2
= edit_buffer_get_current_byte (&edit
->buffer
);
3666 if (edit
->buffer
.curs1
>= edit
->buffer
.size
)
3670 edit_cursor_move (edit
, 1);
3671 c2
= edit_buffer_get_current_byte (&edit
->buffer
);
3673 while (is_break_char (c1
) || is_break_char (c2
));
3675 while (edit_suggest_current_word (edit
) != B_CANCEL
);
3678 /* --------------------------------------------------------------------------------------------- */
3681 edit_set_spell_lang (void)
3685 lang_list
= g_array_new (TRUE
, FALSE
, sizeof (char *));
3686 if (aspell_get_lang_list (lang_list
) != 0)
3690 lang
= spell_dialog_lang_list_show (lang_list
);
3693 (void) aspell_set_lang (lang
);
3697 aspell_array_clean (lang_list
);
3699 #endif /* HAVE_ASPELL */
3701 /* --------------------------------------------------------------------------------------------- */