2 Editor high level editing commands
4 Copyright (C) 1996-2020
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/file_history.h" /* show_file_history() */
66 #include "src/setup.h" /* option_tab_spacing */
68 #include "src/selcodepage.h"
70 #include "src/keybind-defaults.h"
71 #include "src/util.h" /* check_for_default() */
73 #include "edit-impl.h"
74 #include "editwidget.h"
75 #include "editcmd_dialogs.h"
78 #include "spell_dialogs.h"
82 /*** global variables ****************************************************************************/
84 /* search and replace: */
85 int search_create_bookmark
= FALSE
;
87 /* queries on a save */
88 gboolean edit_confirm_save
= TRUE
;
90 /* whether we need to drop selection on copy to buffer */
91 gboolean option_drop_selection_on_copy
= TRUE
;
93 /*** file scope macro definitions ****************************************************************/
97 #define TEMP_BUF_LEN 1024
99 #define MAX_WORD_COMPLETIONS 100 /* in listbox */
101 /*** file scope type declarations ****************************************************************/
105 simple_status_msg_t status_msg
; /* base class */
110 } edit_search_status_msg_t
;
112 /*** file scope variables ************************************************************************/
114 static unsigned long edit_save_mode_radio_id
, edit_save_mode_input_id
;
116 /* --------------------------------------------------------------------------------------------- */
117 /*** file scope functions ************************************************************************/
118 /* --------------------------------------------------------------------------------------------- */
121 edit_search_status_update_cb (status_msg_t
* sm
)
123 simple_status_msg_t
*ssm
= SIMPLE_STATUS_MSG (sm
);
124 edit_search_status_msg_t
*esm
= (edit_search_status_msg_t
*) sm
;
125 Widget
*wd
= WIDGET (sm
->dlg
);
128 label_set_textv (ssm
->label
, _("Searching %s: %3d%%"), esm
->edit
->last_search_string
,
129 edit_buffer_calc_percent (&esm
->edit
->buffer
, esm
->offset
));
131 label_set_textv (ssm
->label
, _("Searching %s"), esm
->edit
->last_search_string
);
136 Widget
*lw
= WIDGET (ssm
->label
);
138 wd_width
= MAX (wd
->cols
, lw
->cols
+ 6);
139 widget_set_size (wd
, wd
->y
, wd
->x
, wd
->lines
, wd_width
);
140 widget_set_size (lw
, lw
->y
, wd
->x
+ (wd
->cols
- lw
->cols
) / 2, lw
->lines
, lw
->cols
);
144 return status_msg_common_update (sm
);
147 /* --------------------------------------------------------------------------------------------- */
150 edit_save_mode_callback (Widget
* w
, Widget
* sender
, widget_msg_t msg
, int parm
, void *data
)
154 case MSG_CHANGED_FOCUS
:
155 if (sender
!= NULL
&& sender
->id
== edit_save_mode_radio_id
)
159 ww
= dlg_find_by_id (DIALOG (w
), edit_save_mode_input_id
);
160 widget_disable (ww
, RADIO (sender
)->sel
!= 2);
164 return MSG_NOT_HANDLED
;
167 return dlg_default_callback (w
, sender
, msg
, parm
, data
);
171 /* --------------------------------------------------------------------------------------------- */
173 /* If 0 (quick save) then a) create/truncate <filename> file,
174 b) save to <filename>;
175 if 1 (safe save) then a) save to <tempnam>,
176 b) rename <tempnam> to <filename>;
177 if 2 (do backups) then a) save to <tempnam>,
178 b) rename <filename> to <filename.backup_ext>,
179 c) rename <tempnam> to <filename>. */
181 /* returns 0 on error, -1 on abort */
184 edit_save_file (WEdit
* edit
, const vfs_path_t
* filename_vpath
)
189 int this_save_mode
, rv
, fd
= -1;
190 vfs_path_t
*real_filename_vpath
;
191 vfs_path_t
*savename_vpath
= NULL
;
192 const char *start_filename
;
193 const vfs_path_element_t
*vpath_element
;
196 vpath_element
= vfs_path_get_by_index (filename_vpath
, 0);
197 if (vpath_element
== NULL
)
200 start_filename
= vpath_element
->path
;
201 if (*start_filename
== '\0')
204 if (!IS_PATH_SEP (*start_filename
) && edit
->dir_vpath
!= NULL
)
205 real_filename_vpath
= vfs_path_append_vpath_new (edit
->dir_vpath
, filename_vpath
, NULL
);
207 real_filename_vpath
= vfs_path_clone (filename_vpath
);
209 this_save_mode
= option_save_mode
;
210 if (this_save_mode
!= EDIT_QUICK_SAVE
)
212 if (!vfs_file_is_local (real_filename_vpath
)
213 || (fd
= mc_open (real_filename_vpath
, O_RDONLY
| O_BINARY
)) == -1)
216 * The file does not exists yet, so no safe save or
217 * backup are necessary.
219 this_save_mode
= EDIT_QUICK_SAVE
;
225 rv
= mc_stat (real_filename_vpath
, &sb
);
228 if (this_save_mode
== EDIT_QUICK_SAVE
&& !edit
->skip_detach_prompt
&& sb
.st_nlink
> 1)
230 rv
= edit_query_dialog3 (_("Warning"),
231 _("File has hard-links. Detach before saving?"),
232 _("&Yes"), _("&No"), _("&Cancel"));
236 this_save_mode
= EDIT_SAFE_SAVE
;
239 edit
->skip_detach_prompt
= 1;
242 vfs_path_free (real_filename_vpath
);
247 /* Prevent overwriting changes from other editor sessions. */
248 if (edit
->stat1
.st_mtime
!= 0 && edit
->stat1
.st_mtime
!= sb
.st_mtime
)
250 /* The default action is "Cancel". */
253 rv
= edit_query_dialog2 (_("Warning"),
254 _("The file has been modified in the meantime. Save anyway?"),
255 _("&Yes"), _("&Cancel"));
258 vfs_path_free (real_filename_vpath
);
264 if (this_save_mode
!= EDIT_QUICK_SAVE
)
266 char *savedir
, *saveprefix
;
268 savedir
= vfs_path_tokens_get (real_filename_vpath
, 0, -1);
270 savedir
= g_strdup (".");
272 /* Token-related function never return leading slash, so we need add it manually */
273 saveprefix
= mc_build_filename (PATH_SEP_STR
, savedir
, "cooledit", (char *) NULL
);
275 fd
= mc_mkstemps (&savename_vpath
, saveprefix
, NULL
);
277 if (savename_vpath
== NULL
)
279 vfs_path_free (real_filename_vpath
);
283 * Close for now because mc_mkstemps use pure open system call
284 * to create temporary file and it needs to be reopened by
285 * VFS-aware mc_open().
290 savename_vpath
= vfs_path_clone (real_filename_vpath
);
292 (void) mc_chown (savename_vpath
, edit
->stat1
.st_uid
, edit
->stat1
.st_gid
);
293 (void) mc_chmod (savename_vpath
, edit
->stat1
.st_mode
);
295 fd
= mc_open (savename_vpath
, O_CREAT
| O_WRONLY
| O_TRUNC
| O_BINARY
, edit
->stat1
.st_mode
);
300 p
= edit_get_write_filter (savename_vpath
, real_filename_vpath
);
306 file
= (FILE *) popen (p
, "w");
310 filelen
= edit_write_stream (edit
, file
);
314 if (pclose (file
) != 0)
316 tmp
= g_strdup_printf (_("Error writing to pipe: %s"), p
);
317 edit_error_dialog (_("Error"), tmp
);
326 tmp
= g_strdup_printf (_("Cannot open pipe for writing: %s"), p
);
327 edit_error_dialog (_("Error"), get_sys_error (tmp
));
334 else if (edit
->lb
== LB_ASIS
)
335 { /* do not change line breaks */
336 filelen
= edit_buffer_write_file (&edit
->buffer
, fd
);
338 if (filelen
!= edit
->buffer
.size
)
343 if (mc_close (fd
) != 0)
345 /* Update the file information, especially the mtime. */
346 if (mc_stat (savename_vpath
, &edit
->stat1
) == -1)
350 { /* change line breaks */
352 const vfs_path_element_t
*path_element
;
356 path_element
= vfs_path_get_by_index (savename_vpath
, -1);
357 file
= (FILE *) fopen (path_element
->path
, "w");
360 filelen
= edit_write_stream (edit
, file
);
367 msg
= g_strdup_printf (_("Cannot open file for writing: %s"), path_element
->path
);
368 edit_error_dialog (_("Error"), msg
);
374 if (filelen
!= edit
->buffer
.size
)
377 if (this_save_mode
== EDIT_DO_BACKUP
)
379 char *tmp_store_filename
;
380 vfs_path_element_t
*last_vpath_element
;
381 vfs_path_t
*tmp_vpath
;
384 g_assert (option_backup_ext
!= NULL
);
386 /* add backup extension to the path */
387 tmp_vpath
= vfs_path_clone (real_filename_vpath
);
388 last_vpath_element
= (vfs_path_element_t
*) vfs_path_get_by_index (tmp_vpath
, -1);
389 tmp_store_filename
= last_vpath_element
->path
;
390 last_vpath_element
->path
= g_strdup_printf ("%s%s", tmp_store_filename
, option_backup_ext
);
391 g_free (tmp_store_filename
);
393 ok
= (mc_rename (real_filename_vpath
, tmp_vpath
) != -1);
394 vfs_path_free (tmp_vpath
);
399 if (this_save_mode
!= EDIT_QUICK_SAVE
)
400 if (mc_rename (savename_vpath
, real_filename_vpath
) == -1)
403 vfs_path_free (real_filename_vpath
);
404 vfs_path_free (savename_vpath
);
407 /* FIXME: Is this safe ?
408 * if (this_save_mode != EDIT_QUICK_SAVE)
409 * mc_unlink (savename);
411 vfs_path_free (real_filename_vpath
);
412 vfs_path_free (savename_vpath
);
416 /* --------------------------------------------------------------------------------------------- */
419 edit_check_newline (const edit_buffer_t
* buf
)
421 return !(option_check_nl_at_eof
&& buf
->size
> 0
422 && edit_buffer_get_byte (buf
, buf
->size
- 1) != '\n'
423 && edit_query_dialog2 (_("Warning"),
424 _("The file you are saving does not end with a newline."),
425 _("C&ontinue"), _("&Cancel")) != 0);
428 /* --------------------------------------------------------------------------------------------- */
431 edit_get_save_file_as (WEdit
* edit
)
433 static LineBreaks cur_lb
= LB_ASIS
;
435 vfs_path_t
*ret_vpath
= NULL
;
437 const char *lb_names
[LB_NAMES
] = {
438 N_("&Do not change"),
439 N_("&Unix format (LF)"),
440 N_("&Windows/DOS format (CR LF)"),
441 N_("&Macintosh format (CR)")
444 quick_widget_t quick_widgets
[] = {
446 QUICK_LABELED_INPUT (N_("Enter file name:"), input_label_above
,
447 vfs_path_as_str (edit
->filename_vpath
), "save-as",
448 &filename_res
, NULL
, FALSE
, FALSE
, INPUT_COMPLETE_FILENAMES
),
449 QUICK_SEPARATOR (TRUE
),
450 QUICK_LABEL (N_("Change line breaks to:"), NULL
),
451 QUICK_RADIO (LB_NAMES
, lb_names
, (int *) &cur_lb
, NULL
),
452 QUICK_BUTTONS_OK_CANCEL
,
457 quick_dialog_t qdlg
= {
459 N_("Save As"), "[Save File As]",
460 quick_widgets
, NULL
, NULL
463 if (quick_dialog (&qdlg
) != B_CANCEL
)
468 fname
= tilde_expand (filename_res
);
469 g_free (filename_res
);
470 ret_vpath
= vfs_path_from_str (fname
);
477 /* --------------------------------------------------------------------------------------------- */
479 /** returns TRUE on success */
482 edit_save_cmd (WEdit
* edit
)
484 int res
, save_lock
= 0;
486 if (!edit
->locked
&& !edit
->delete_file
)
487 save_lock
= lock_file (edit
->filename_vpath
);
488 res
= edit_save_file (edit
, edit
->filename_vpath
);
490 /* Maintain modify (not save) lock on failure */
491 if ((res
> 0 && edit
->locked
) || save_lock
)
492 edit
->locked
= unlock_file (edit
->filename_vpath
);
494 /* On failure try 'save as', it does locking on its own */
496 return edit_save_as_cmd (edit
);
497 edit
->force
|= REDRAW_COMPLETELY
;
500 edit
->delete_file
= 0;
507 /* --------------------------------------------------------------------------------------------- */
511 * @param h screen the owner of editor window
512 * @param vpath vfs file path
513 * @return TRUE if file content was successfully loaded, FALSE otherwise
516 static inline gboolean
517 edit_load_file_from_filename (WDialog
* h
, const vfs_path_t
* vpath
)
519 Widget
*w
= WIDGET (h
);
521 return edit_add_window (h
, w
->y
+ 1, w
->x
, w
->lines
- 2, w
->cols
, vpath
, 0);
524 /* --------------------------------------------------------------------------------------------- */
527 edit_delete_column_of_text (WEdit
* edit
)
534 eval_marks (edit
, &m1
, &m2
);
535 n
= edit_buffer_get_forward_offset (&edit
->buffer
, m1
, 0, m2
) + 1;
536 c
= (long) edit_move_forward3 (edit
, edit_buffer_get_bol (&edit
->buffer
, m1
), 0, m1
);
537 d
= (long) edit_move_forward3 (edit
, edit_buffer_get_bol (&edit
->buffer
, m2
), 0, m2
);
538 b
= MAX (MIN (c
, d
), MIN (edit
->column1
, edit
->column2
));
539 c
= MAX (c
, MAX (edit
->column1
, edit
->column2
));
543 r
= edit_buffer_get_current_bol (&edit
->buffer
);
544 p
= edit_move_forward3 (edit
, r
, b
, 0);
545 q
= edit_move_forward3 (edit
, r
, c
, 0);
550 edit_cursor_move (edit
, p
- edit
->buffer
.curs1
);
553 /* delete line between margins */
554 if (edit_buffer_get_current_byte (&edit
->buffer
) != '\n')
555 edit_delete (edit
, TRUE
);
559 /* move to next line except on the last delete */
560 edit_cursor_move (edit
,
561 edit_buffer_get_forward_offset (&edit
->buffer
, edit
->buffer
.curs1
, 1,
562 0) - edit
->buffer
.curs1
);
566 /* --------------------------------------------------------------------------------------------- */
567 /** if success return 0 */
570 edit_block_delete (WEdit
* edit
)
572 off_t start_mark
, end_mark
;
574 long curs_line
, c1
, c2
;
576 if (!eval_marks (edit
, &start_mark
, &end_mark
))
579 if (edit
->column_highlight
&& edit
->mark2
< 0)
580 edit_mark_cmd (edit
, FALSE
);
581 if ((end_mark
- start_mark
) > option_max_undo
/ 2)
583 /* Warning message with a query to continue or cancel the operation */
584 if (edit_query_dialog2
587 ("Block is large, you may not be able to undo this action"),
588 _("C&ontinue"), _("&Cancel")))
593 c1
= MIN (edit
->column1
, edit
->column2
);
594 c2
= MAX (edit
->column1
, edit
->column2
);
598 edit_push_markers (edit
);
600 curs_line
= edit
->buffer
.curs_line
;
602 curs_pos
= edit
->curs_col
+ edit
->over_col
;
604 /* move cursor to start of selection */
605 edit_cursor_move (edit
, start_mark
- edit
->buffer
.curs1
);
606 edit_scroll_screen_over_cursor (edit
);
608 if (start_mark
< end_mark
)
610 if (edit
->column_highlight
)
615 edit_mark_cmd (edit
, FALSE
);
616 edit_delete_column_of_text (edit
);
617 /* move cursor to the saved position */
618 edit_move_to_line (edit
, curs_line
);
619 /* calculate line width and cursor position before cut */
620 line_width
= edit_move_forward3 (edit
, edit_buffer_get_current_bol (&edit
->buffer
), 0,
621 edit_buffer_get_current_eol (&edit
->buffer
));
622 if (option_cursor_beyond_eol
&& curs_pos
> line_width
)
623 edit
->over_col
= curs_pos
- line_width
;
629 for (count
= start_mark
; count
< end_mark
; count
++)
630 edit_delete (edit
, TRUE
);
633 edit_set_markers (edit
, 0, 0, 0, 0);
634 edit
->force
|= REDRAW_PAGE
;
638 /* --------------------------------------------------------------------------------------------- */
640 * Get EOL symbol for searching.
642 * @param edit editor object
647 edit_search_get_current_end_line_char (const WEdit
* edit
)
658 /* --------------------------------------------------------------------------------------------- */
660 * Checking if search condition have BOL(^) or EOL ($) regexp special characters.
662 * @param search search object
663 * @return result of checks.
666 static edit_search_line_t
667 edit_get_search_line_type (mc_search_t
* search
)
669 edit_search_line_t search_line_type
= 0;
671 if (search
->search_type
!= MC_SEARCH_T_REGEX
)
672 return search_line_type
;
674 if (*search
->original
== '^')
675 search_line_type
|= AT_START_LINE
;
677 if (search
->original
[search
->original_len
- 1] == '$')
678 search_line_type
|= AT_END_LINE
;
679 return search_line_type
;
682 /* --------------------------------------------------------------------------------------------- */
684 * Calculating the start position of next line.
686 * @param buf editor buffer object
687 * @param current_pos current position
688 * @param max_pos max position
689 * @param end_string_symbol end of line symbol
690 * @return start position of next line
694 edit_calculate_start_of_next_line (const edit_buffer_t
* buf
, off_t current_pos
, off_t max_pos
,
695 char end_string_symbol
)
699 for (i
= current_pos
; i
< max_pos
; i
++)
702 if (edit_buffer_get_byte (buf
, i
) == end_string_symbol
)
709 /* --------------------------------------------------------------------------------------------- */
711 * Calculating the end position of previous line.
713 * @param buf editor buffer object
714 * @param current_pos current position
715 * @param end_string_symbol end of line symbol
716 * @return end position of previous line
720 edit_calculate_end_of_previous_line (const edit_buffer_t
* buf
, off_t current_pos
,
721 char end_string_symbol
)
725 for (i
= current_pos
- 1; i
>= 0; i
--)
726 if (edit_buffer_get_byte (buf
, i
) == end_string_symbol
)
732 /* --------------------------------------------------------------------------------------------- */
734 * Calculating the start position of previous line.
736 * @param buf editor buffer object
737 * @param current_pos current position
738 * @param end_string_symbol end of line symbol
739 * @return start position of previous line
743 edit_calculate_start_of_previous_line (const edit_buffer_t
* buf
, off_t current_pos
,
744 char end_string_symbol
)
746 current_pos
= edit_calculate_end_of_previous_line (buf
, current_pos
, end_string_symbol
);
747 current_pos
= edit_calculate_end_of_previous_line (buf
, current_pos
, end_string_symbol
);
749 return (current_pos
+ 1);
752 /* --------------------------------------------------------------------------------------------- */
754 * Calculating the start position of current line.
756 * @param buf editor buffer object
757 * @param current_pos current position
758 * @param end_string_symbol end of line symbol
759 * @return start position of current line
763 edit_calculate_start_of_current_line (const edit_buffer_t
* buf
, off_t current_pos
,
764 char end_string_symbol
)
766 current_pos
= edit_calculate_end_of_previous_line (buf
, current_pos
, end_string_symbol
);
768 return (current_pos
+ 1);
771 /* --------------------------------------------------------------------------------------------- */
773 * Fixing (if needed) search start position if 'only in selection' option present.
775 * @param edit editor object
779 edit_search_fix_search_start_if_selection (WEdit
* edit
)
781 off_t start_mark
= 0;
784 if (!edit_search_options
.only_in_selection
)
787 if (!eval_marks (edit
, &start_mark
, &end_mark
))
790 if (edit_search_options
.backwards
)
792 if (edit
->search_start
> end_mark
|| edit
->search_start
<= start_mark
)
793 edit
->search_start
= end_mark
;
797 if (edit
->search_start
< start_mark
|| edit
->search_start
>= end_mark
)
798 edit
->search_start
= start_mark
;
802 /* --------------------------------------------------------------------------------------------- */
805 editcmd_find (edit_search_status_msg_t
* esm
, gsize
* len
)
807 WEdit
*edit
= esm
->edit
;
808 off_t search_start
= edit
->search_start
;
810 off_t start_mark
= 0;
811 off_t end_mark
= edit
->buffer
.size
;
812 char end_string_symbol
;
814 end_string_symbol
= edit_search_get_current_end_line_char (edit
);
816 /* prepare for search */
817 if (edit_search_options
.only_in_selection
)
819 if (!eval_marks (edit
, &start_mark
, &end_mark
))
821 mc_search_set_error (edit
->search
, MC_SEARCH_E_NOTFOUND
, "%s", _(STR_E_NOTFOUND
));
825 /* fix the start and the end of search block positions */
826 if ((edit
->search_line_type
& AT_START_LINE
) != 0
828 || edit_buffer_get_byte (&edit
->buffer
, start_mark
- 1) != end_string_symbol
))
830 edit_calculate_start_of_next_line (&edit
->buffer
, start_mark
, edit
->buffer
.size
,
833 if ((edit
->search_line_type
& AT_END_LINE
) != 0
834 && (end_mark
- 1 != edit
->buffer
.size
835 || edit_buffer_get_byte (&edit
->buffer
, end_mark
) != end_string_symbol
))
837 edit_calculate_end_of_previous_line (&edit
->buffer
, end_mark
, end_string_symbol
);
839 if (start_mark
>= end_mark
)
841 mc_search_set_error (edit
->search
, MC_SEARCH_E_NOTFOUND
, "%s", _(STR_E_NOTFOUND
));
845 else if (edit_search_options
.backwards
)
846 end_mark
= MAX (1, edit
->buffer
.curs1
) - 1;
849 if (edit_search_options
.backwards
)
851 /* backward search */
852 search_end
= end_mark
;
854 if ((edit
->search_line_type
& AT_START_LINE
) != 0)
856 edit_calculate_start_of_current_line (&edit
->buffer
, search_start
,
859 while (search_start
>= start_mark
)
863 if (search_end
> (off_t
) (search_start
+ edit
->search
->original_len
)
864 && mc_search_is_fixed_search_str (edit
->search
))
865 search_end
= search_start
+ edit
->search
->original_len
;
867 ok
= mc_search_run (edit
->search
, (void *) esm
, search_start
, search_end
, len
);
869 if (ok
&& edit
->search
->normal_offset
== search_start
)
872 /* We abort the search in case of a pattern error, or if the user aborts
873 the search. In other words: in all cases except "string not found". */
874 if (!ok
&& edit
->search
->error
!= MC_SEARCH_E_NOTFOUND
)
877 if ((edit
->search_line_type
& AT_START_LINE
) != 0)
879 edit_calculate_start_of_previous_line (&edit
->buffer
, search_start
,
885 mc_search_set_error (edit
->search
, MC_SEARCH_E_NOTFOUND
, "%s", _(STR_E_NOTFOUND
));
890 if ((edit
->search_line_type
& AT_START_LINE
) != 0 && search_start
!= start_mark
)
892 edit_calculate_start_of_next_line (&edit
->buffer
, search_start
, end_mark
,
895 return mc_search_run (edit
->search
, (void *) esm
, search_start
, end_mark
, len
);
898 /* --------------------------------------------------------------------------------------------- */
901 edit_replace_cmd__conv_to_display (const char *str
)
906 tmp
= str_convert_to_display (str
);
910 return g_string_free (tmp
, FALSE
);
911 g_string_free (tmp
, TRUE
);
914 return g_strdup (str
);
917 /* --------------------------------------------------------------------------------------------- */
920 edit_replace_cmd__conv_to_input (char *str
)
925 tmp
= str_convert_to_input (str
);
927 return g_string_free (tmp
, FALSE
);
928 g_string_free (tmp
, TRUE
);
930 return g_strdup (str
);
933 /* --------------------------------------------------------------------------------------------- */
936 edit_show_search_error (const WEdit
* edit
, const char *title
)
938 if (edit
->search
->error
== MC_SEARCH_E_NOTFOUND
)
939 edit_query_dialog (title
, _(STR_E_NOTFOUND
));
940 else if (edit
->search
->error_str
!= NULL
)
941 edit_query_dialog (title
, edit
->search
->error_str
);
944 /* --------------------------------------------------------------------------------------------- */
947 edit_do_search (WEdit
* edit
)
949 edit_search_status_msg_t esm
;
952 if (edit
->search
== NULL
)
953 edit
->search_start
= edit
->buffer
.curs1
;
955 edit_push_undo_action (edit
, KEY_PRESS
+ edit
->start_display
);
959 esm
.offset
= edit
->search_start
;
961 status_msg_init (STATUS_MSG (&esm
), _("Search"), 1.0, simple_status_msg_init_cb
,
962 edit_search_status_update_cb
, NULL
);
964 if (search_create_bookmark
)
966 gboolean found
= FALSE
;
967 long l
= 0, l_last
= -1;
970 search_create_bookmark
= FALSE
;
971 book_mark_flush (edit
, -1);
973 while (mc_search_run (edit
->search
, (void *) &esm
, q
, edit
->buffer
.size
, &len
))
976 edit
->search_start
= edit
->search
->normal_offset
;
978 l
+= edit_buffer_count_lines (&edit
->buffer
, q
, edit
->search
->normal_offset
);
981 book_mark_insert (edit
, l
, BOOK_MARK_FOUND_COLOR
);
984 q
= edit
->search
->normal_offset
+ 1;
988 edit_error_dialog (_("Search"), _(STR_E_NOTFOUND
));
990 edit_cursor_move (edit
, edit
->search_start
- edit
->buffer
.curs1
);
994 if (edit
->found_len
!= 0 && edit
->search_start
== edit
->found_start
+ 1
995 && edit_search_options
.backwards
)
996 edit
->search_start
--;
998 if (edit
->found_len
!= 0 && edit
->search_start
== edit
->found_start
- 1
999 && !edit_search_options
.backwards
)
1000 edit
->search_start
++;
1002 if (editcmd_find (&esm
, &len
))
1004 edit
->found_start
= edit
->search_start
= edit
->search
->normal_offset
;
1005 edit
->found_len
= len
;
1007 edit_cursor_move (edit
, edit
->search_start
- edit
->buffer
.curs1
);
1008 edit_scroll_screen_over_cursor (edit
);
1009 if (edit_search_options
.backwards
)
1010 edit
->search_start
--;
1012 edit
->search_start
++;
1016 edit
->search_start
= edit
->buffer
.curs1
;
1017 edit_show_search_error (edit
, _("Search"));
1021 status_msg_deinit (STATUS_MSG (&esm
));
1023 edit
->force
|= REDRAW_COMPLETELY
;
1024 edit_scroll_screen_over_cursor (edit
);
1027 /* --------------------------------------------------------------------------------------------- */
1030 edit_search (WEdit
* edit
)
1032 if (editcmd_dialog_search_show (edit
))
1034 edit
->search_line_type
= edit_get_search_line_type (edit
->search
);
1035 edit_search_fix_search_start_if_selection (edit
);
1036 edit_do_search (edit
);
1040 /* --------------------------------------------------------------------------------------------- */
1041 /** Return a null terminated length of text. Result must be g_free'd */
1043 static unsigned char *
1044 edit_get_block (WEdit
* edit
, off_t start
, off_t finish
, off_t
* l
)
1046 unsigned char *s
, *r
;
1048 r
= s
= g_malloc0 (finish
- start
+ 1);
1049 if (edit
->column_highlight
)
1052 /* copy from buffer, excluding chars that are out of the column 'margins' */
1053 while (start
< finish
)
1058 x
= edit_move_forward3 (edit
, edit_buffer_get_bol (&edit
->buffer
, start
), 0, start
);
1059 c
= edit_buffer_get_byte (&edit
->buffer
, start
);
1060 if ((x
>= edit
->column1
&& x
< edit
->column2
)
1061 || (x
>= edit
->column2
&& x
< edit
->column1
) || c
== '\n')
1071 *l
= finish
- start
;
1072 while (start
< finish
)
1073 *s
++ = edit_buffer_get_byte (&edit
->buffer
, start
++);
1079 /* --------------------------------------------------------------------------------------------- */
1080 /** copies a block to clipboard file */
1083 edit_save_block_to_clip_file (WEdit
* edit
, off_t start
, off_t finish
)
1088 tmp
= mc_config_get_full_path (EDIT_CLIP_FILE
);
1089 ret
= edit_save_block (edit
, tmp
, start
, finish
);
1094 /* --------------------------------------------------------------------------------------------- */
1097 pipe_mail (const edit_buffer_t
* buf
, char *to
, char *subject
, char *cc
)
1102 to
= name_quote (to
, FALSE
);
1103 subject
= name_quote (subject
, FALSE
);
1104 cc
= name_quote (cc
, FALSE
);
1105 s
= g_strconcat ("mail -s ", subject
, *cc
? " -c " : "", cc
, " ", to
, (char *) NULL
);
1120 for (i
= 0; i
< buf
->size
; i
++)
1121 fputc (edit_buffer_get_byte (buf
, i
), p
);
1126 /* --------------------------------------------------------------------------------------------- */
1127 /** find first character of current word */
1130 edit_find_word_start (const edit_buffer_t
* buf
, off_t
* word_start
, gsize
* word_len
)
1135 /* return if at begin of file */
1136 if (buf
->curs1
<= 0)
1139 c
= edit_buffer_get_previous_byte (buf
);
1140 /* return if not at end or in word */
1141 if (is_break_char (c
))
1144 /* search start of word to be completed */
1150 c
= edit_buffer_get_byte (buf
, buf
->curs1
- i
- 1);
1152 if (is_break_char (c
))
1154 /* return if word starts with digit */
1163 *word_start
= buf
->curs1
- i
; /* start found */
1164 *word_len
= (gsize
) i
;
1168 /* --------------------------------------------------------------------------------------------- */
1170 * Get current word under cursor
1172 * @param esm status message window
1173 * @param srch mc_search object
1174 * @param word_start start word position
1176 * @return newly allocated string or NULL if no any words under cursor
1180 edit_collect_completions_get_current_word (edit_search_status_msg_t
* esm
, mc_search_t
* srch
,
1183 WEdit
*edit
= esm
->edit
;
1188 if (!mc_search_run (srch
, (void *) esm
, word_start
, edit
->buffer
.size
, &len
))
1191 temp
= g_string_sized_new (len
);
1193 for (i
= 0; i
< (off_t
) len
; i
++)
1197 chr
= edit_buffer_get_byte (&edit
->buffer
, word_start
+ i
);
1199 g_string_append_c (temp
, chr
);
1202 return g_string_free (temp
, temp
->len
== 0);
1205 /* --------------------------------------------------------------------------------------------- */
1206 /** collect the possible completions */
1209 edit_collect_completions (WEdit
* edit
, off_t word_start
, gsize word_len
,
1210 char *match_expr
, GString
** compl, gsize
* num
)
1218 off_t last_byte
, start
= -1;
1220 gboolean entire_file
;
1221 edit_search_status_msg_t esm
;
1224 srch
= mc_search_new (match_expr
, cp_source
);
1226 srch
= mc_search_new (match_expr
, NULL
);
1232 mc_config_get_bool (mc_global
.main_config
, CONFIG_APP_SECTION
,
1233 "editor_wordcompletion_collect_entire_file", 0);
1235 last_byte
= entire_file
? edit
->buffer
.size
: word_start
;
1237 srch
->search_type
= MC_SEARCH_T_REGEX
;
1238 srch
->is_case_sensitive
= TRUE
;
1239 srch
->search_fn
= edit_search_cmd_callback
;
1240 srch
->update_fn
= edit_search_update_callback
;
1244 esm
.offset
= entire_file
? 0 : word_start
;
1246 status_msg_init (STATUS_MSG (&esm
), _("Collect completions"), 1.0, simple_status_msg_init_cb
,
1247 edit_search_status_update_cb
, NULL
);
1249 current_word
= edit_collect_completions_get_current_word (&esm
, srch
, word_start
);
1251 temp
= g_string_new ("");
1253 /* collect max MAX_WORD_COMPLETIONS completions */
1254 while (mc_search_run (srch
, (void *) &esm
, start
+ 1, last_byte
, &len
))
1256 g_string_set_size (temp
, 0);
1257 start
= srch
->normal_offset
;
1259 /* add matched completion if not yet added */
1260 for (i
= 0; i
< len
; i
++)
1262 skip
= edit_buffer_get_byte (&edit
->buffer
, start
+ i
);
1266 /* skip current word */
1267 if (start
+ (off_t
) i
== word_start
)
1270 g_string_append_c (temp
, skip
);
1276 if (current_word
!= NULL
&& strcmp (current_word
, temp
->str
) == 0)
1281 for (i
= 0; i
< *num
; i
++)
1284 ((char *) &compl[i
]->str
[word_len
],
1285 (char *) &temp
->str
[word_len
], MAX (len
, compl[i
]->len
) - word_len
) == 0)
1287 GString
*this = compl[i
];
1288 for (++i
; i
< *num
; i
++)
1289 compl[i
- 1] = compl[i
];
1290 compl[*num
- 1] = this;
1292 break; /* skip it, already added */
1298 if (*num
== MAX_WORD_COMPLETIONS
)
1300 g_string_free (compl[0], TRUE
);
1301 for (i
= 1; i
< *num
; i
++)
1302 compl[i
- 1] = compl[i
];
1309 recoded
= str_convert_to_display (temp
->str
);
1310 if (recoded
->len
!= 0)
1311 g_string_assign (temp
, recoded
->str
);
1313 g_string_free (recoded
, TRUE
);
1316 compl[(*num
)++] = g_string_new_len (temp
->str
, temp
->len
);
1319 /* note the maximal length needed for the completion dialog */
1324 status_msg_deinit (STATUS_MSG (&esm
));
1325 mc_search_free (srch
);
1326 g_string_free (temp
, TRUE
);
1327 g_free (current_word
);
1332 /* --------------------------------------------------------------------------------------------- */
1335 edit_insert_column_of_text (WEdit
* edit
, unsigned char *data
, off_t size
, long width
,
1336 off_t
* start_pos
, off_t
* end_pos
, long *col1
, long *col2
)
1341 cursor
= edit
->buffer
.curs1
;
1342 col
= edit_get_col (edit
);
1344 for (i
= 0; i
< size
; i
++)
1346 if (data
[i
] != '\n')
1347 edit_insert (edit
, data
[i
]);
1349 { /* fill in and move to next line */
1353 if (edit_buffer_get_current_byte (&edit
->buffer
) != '\n')
1355 for (l
= width
- (edit_get_col (edit
) - col
); l
> 0; l
-= space_width
)
1356 edit_insert (edit
, ' ');
1358 for (p
= edit
->buffer
.curs1
;; p
++)
1360 if (p
== edit
->buffer
.size
)
1362 edit_cursor_move (edit
, edit
->buffer
.size
- edit
->buffer
.curs1
);
1363 edit_insert_ahead (edit
, '\n');
1367 if (edit_buffer_get_byte (&edit
->buffer
, p
) == '\n')
1373 edit_cursor_move (edit
, edit_move_forward3 (edit
, p
, col
, 0) - edit
->buffer
.curs1
);
1375 for (l
= col
- edit_get_col (edit
); l
>= space_width
; l
-= space_width
)
1376 edit_insert (edit
, ' ');
1381 *col2
= col
+ width
;
1382 *start_pos
= cursor
;
1383 *end_pos
= edit
->buffer
.curs1
;
1384 edit_cursor_move (edit
, cursor
- edit
->buffer
.curs1
);
1387 /* --------------------------------------------------------------------------------------------- */
1390 edit_macro_comparator (gconstpointer
* macro1
, gconstpointer
* macro2
)
1392 const macros_t
*m1
= (const macros_t
*) macro1
;
1393 const macros_t
*m2
= (const macros_t
*) macro2
;
1395 return m1
->hotkey
- m2
->hotkey
;
1398 /* --------------------------------------------------------------------------------------------- */
1401 edit_macro_sort_by_hotkey (void)
1403 if (macros_list
!= NULL
&& macros_list
->len
!= 0)
1404 g_array_sort (macros_list
, (GCompareFunc
) edit_macro_comparator
);
1407 /* --------------------------------------------------------------------------------------------- */
1410 edit_get_macro (WEdit
* edit
, int hotkey
, const macros_t
** macros
, guint
* indx
)
1412 const macros_t
*array_start
= &g_array_index (macros_list
, struct macros_t
, 0);
1414 macros_t search_macro
;
1418 search_macro
.hotkey
= hotkey
;
1419 result
= bsearch (&search_macro
, macros_list
->data
, macros_list
->len
,
1420 sizeof (macros_t
), (GCompareFunc
) edit_macro_comparator
);
1422 if (result
!= NULL
&& result
->macro
!= NULL
)
1424 *indx
= (result
- array_start
);
1432 /* --------------------------------------------------------------------------------------------- */
1433 /** returns FALSE on error */
1436 edit_delete_macro (WEdit
* edit
, int hotkey
)
1438 mc_config_t
*macros_config
= NULL
;
1439 const char *section_name
= "editor";
1440 gchar
*macros_fname
;
1443 const macros_t
*macros
= NULL
;
1445 /* clear array of actions for current hotkey */
1446 while (edit_get_macro (edit
, hotkey
, ¯os
, &indx
))
1448 if (macros
->macro
!= NULL
)
1449 g_array_free (macros
->macro
, TRUE
);
1451 g_array_remove_index (macros_list
, indx
);
1452 edit_macro_sort_by_hotkey ();
1455 macros_fname
= mc_config_get_full_path (MC_MACRO_FILE
);
1456 macros_config
= mc_config_init (macros_fname
, FALSE
);
1457 g_free (macros_fname
);
1459 if (macros_config
== NULL
)
1462 skeyname
= lookup_key_by_code (hotkey
);
1463 while (mc_config_del_key (macros_config
, section_name
, skeyname
))
1466 mc_config_save_file (macros_config
, NULL
);
1467 mc_config_deinit (macros_config
);
1471 /* --------------------------------------------------------------------------------------------- */
1473 * Callback for the iteration of objects in the 'editors' array.
1474 * Toggle syntax highlighting in editor object.
1476 * @param data probably WEdit object
1477 * @param user_data unused
1481 edit_syntax_onoff_cb (void *data
, void *user_data
)
1485 if (edit_widget_is_editor (CONST_WIDGET (data
)))
1487 WEdit
*edit
= (WEdit
*) data
;
1489 if (option_syntax_highlighting
)
1490 edit_load_syntax (edit
, NULL
, edit
->syntax_type
);
1491 edit
->force
|= REDRAW_PAGE
;
1495 /* --------------------------------------------------------------------------------------------- */
1497 * Callback for the iteration of objects in the 'editors' array.
1498 * Redraw editor object.
1500 * @param data probably WEdit object
1501 * @param user_data unused
1505 edit_redraw_page_cb (void *data
, void *user_data
)
1509 if (edit_widget_is_editor (CONST_WIDGET (data
)))
1510 ((WEdit
*) data
)->force
|= REDRAW_PAGE
;
1513 /* --------------------------------------------------------------------------------------------- */
1515 * Insert autocompleted word into editor.
1517 * @param edit editor object
1518 * @param completion word for completion
1519 * @param word_len offset from beginning for insert
1523 edit_complete_word_insert_recoded_completion (WEdit
* edit
, char *completion
, gsize word_len
)
1528 temp
= str_convert_to_input (completion
);
1530 for (completion
= temp
->str
+ word_len
; *completion
!= '\0'; completion
++)
1531 edit_insert (edit
, *completion
);
1532 g_string_free (temp
, TRUE
);
1534 for (completion
+= word_len
; *completion
!= '\0'; completion
++)
1535 edit_insert (edit
, *completion
);
1539 /* --------------------------------------------------------------------------------------------- */
1540 /*** public functions ****************************************************************************/
1541 /* --------------------------------------------------------------------------------------------- */
1544 edit_refresh_cmd (void)
1551 /* --------------------------------------------------------------------------------------------- */
1553 * Toggle syntax highlighting in all editor windows.
1555 * @param h root widget for all windows
1559 edit_syntax_onoff_cmd (WDialog
* h
)
1561 option_syntax_highlighting
= !option_syntax_highlighting
;
1562 g_list_foreach (GROUP (h
)->widgets
, edit_syntax_onoff_cb
, NULL
);
1566 /* --------------------------------------------------------------------------------------------- */
1568 * Toggle tabs showing in all editor windows.
1570 * @param h root widget for all windows
1574 edit_show_tabs_tws_cmd (WDialog
* h
)
1576 enable_show_tabs_tws
= !enable_show_tabs_tws
;
1577 g_list_foreach (GROUP (h
)->widgets
, edit_redraw_page_cb
, NULL
);
1581 /* --------------------------------------------------------------------------------------------- */
1583 * Toggle right margin showing in all editor windows.
1585 * @param h root widget for all windows
1589 edit_show_margin_cmd (WDialog
* h
)
1591 show_right_margin
= !show_right_margin
;
1592 g_list_foreach (GROUP (h
)->widgets
, edit_redraw_page_cb
, NULL
);
1596 /* --------------------------------------------------------------------------------------------- */
1598 * Toggle line numbers showing in all editor windows.
1600 * @param h root widget for all windows
1604 edit_show_numbers_cmd (WDialog
* h
)
1606 option_line_state
= !option_line_state
;
1607 option_line_state_width
= option_line_state
? LINE_STATE_WIDTH
: 0;
1608 g_list_foreach (GROUP (h
)->widgets
, edit_redraw_page_cb
, NULL
);
1612 /* --------------------------------------------------------------------------------------------- */
1615 edit_save_mode_cmd (void)
1617 char *str_result
= NULL
;
1619 const char *str
[] = {
1622 N_("&Do backups with following extension:")
1628 for (i
= 0; i
< 3; i
++)
1632 g_assert (option_backup_ext
!= NULL
);
1635 quick_widget_t quick_widgets
[] = {
1637 QUICK_RADIO (3, str
, &option_save_mode
, &edit_save_mode_radio_id
),
1638 QUICK_INPUT (option_backup_ext
, "edit-backup-ext", &str_result
,
1639 &edit_save_mode_input_id
, FALSE
, FALSE
, INPUT_COMPLETE_NONE
),
1640 QUICK_SEPARATOR (TRUE
),
1641 QUICK_CHECKBOX (N_("Check &POSIX new line"), &option_check_nl_at_eof
, NULL
),
1642 QUICK_BUTTONS_OK_CANCEL
,
1647 quick_dialog_t qdlg
= {
1649 N_("Edit Save Mode"), "[Edit Save Mode]",
1650 quick_widgets
, edit_save_mode_callback
, NULL
1653 if (quick_dialog (&qdlg
) != B_CANCEL
)
1655 g_free (option_backup_ext
);
1656 option_backup_ext
= str_result
;
1661 /* --------------------------------------------------------------------------------------------- */
1664 edit_set_filename (WEdit
* edit
, const vfs_path_t
* name_vpath
)
1666 vfs_path_free (edit
->filename_vpath
);
1667 edit
->filename_vpath
= vfs_path_clone (name_vpath
);
1669 if (edit
->dir_vpath
== NULL
)
1670 edit
->dir_vpath
= vfs_path_clone (vfs_get_raw_current_dir ());
1673 /* --------------------------------------------------------------------------------------------- */
1674 /* Here we want to warn the users of overwriting an existing file,
1675 but only if they have made a change to the filename */
1676 /* returns TRUE on success */
1678 edit_save_as_cmd (WEdit
* edit
)
1680 /* This heads the 'Save As' dialog box */
1681 vfs_path_t
*exp_vpath
;
1683 int different_filename
= 0;
1685 if (!edit_check_newline (&edit
->buffer
))
1688 exp_vpath
= edit_get_save_file_as (edit
);
1689 edit_push_undo_action (edit
, KEY_PRESS
+ edit
->start_display
);
1691 if (exp_vpath
!= NULL
)
1693 if (vfs_path_len (exp_vpath
) == 0)
1699 if (!vfs_path_equal (edit
->filename_vpath
, exp_vpath
))
1704 if (mc_stat (exp_vpath
, &sb
) == 0 && !S_ISREG (sb
.st_mode
))
1706 edit_error_dialog (_("Save as"),
1708 ("Cannot save: destination is not a regular file")));
1712 different_filename
= 1;
1713 file
= mc_open (exp_vpath
, O_RDONLY
| O_BINARY
);
1717 /* the file exists */
1719 /* Overwrite the current file or cancel the operation */
1720 if (edit_query_dialog2
1722 _("A file already exists with this name"), _("&Overwrite"), _("&Cancel")))
1727 edit
->stat1
.st_mode
|= S_IWUSR
;
1729 save_lock
= lock_file (exp_vpath
);
1733 /* filenames equal, check if already locked */
1734 if (!edit
->locked
&& !edit
->delete_file
)
1735 save_lock
= lock_file (exp_vpath
);
1738 if (different_filename
)
1741 * Allow user to write into saved (under another name) file
1742 * even if original file had r/o user permissions.
1744 edit
->stat1
.st_mode
|= S_IWRITE
;
1747 rv
= edit_save_file (edit
, exp_vpath
);
1751 /* Successful, so unlock both files */
1752 if (different_filename
)
1755 unlock_file (exp_vpath
);
1757 edit
->locked
= unlock_file (edit
->filename_vpath
);
1761 if (edit
->locked
|| save_lock
)
1762 edit
->locked
= unlock_file (edit
->filename_vpath
);
1765 edit_set_filename (edit
, exp_vpath
);
1766 if (edit
->lb
!= LB_ASIS
)
1767 edit_reload (edit
, exp_vpath
);
1769 edit
->delete_file
= 0;
1770 if (different_filename
)
1771 edit_load_syntax (edit
, NULL
, edit
->syntax_type
);
1772 vfs_path_free (exp_vpath
);
1773 edit
->force
|= REDRAW_COMPLETELY
;
1776 edit_error_dialog (_("Save as"), get_sys_error (_("Cannot save file")));
1779 /* Failed, so maintain modify (not save) lock */
1781 unlock_file (exp_vpath
);
1788 vfs_path_free (exp_vpath
);
1789 edit
->force
|= REDRAW_COMPLETELY
;
1793 /* {{{ Macro stuff starts here */
1794 /* --------------------------------------------------------------------------------------------- */
1797 edit_delete_macro_cmd (WEdit
* edit
)
1801 hotkey
= editcmd_dialog_raw_key_query (_("Delete macro"), _("Press macro hotkey:"), TRUE
);
1803 if (hotkey
!= 0 && !edit_delete_macro (edit
, hotkey
))
1804 message (D_ERROR
, _("Delete macro"), _("Macro not deleted"));
1807 /* --------------------------------------------------------------------------------------------- */
1809 /** returns FALSE on error */
1811 edit_execute_macro (WEdit
* edit
, int hotkey
)
1813 gboolean res
= FALSE
;
1817 const macros_t
*macros
;
1820 if (edit_get_macro (edit
, hotkey
, ¯os
, &indx
) &&
1821 macros
->macro
!= NULL
&& macros
->macro
->len
!= 0)
1825 edit
->force
|= REDRAW_PAGE
;
1827 for (i
= 0; i
< macros
->macro
->len
; i
++)
1829 const macro_action_t
*m_act
;
1831 m_act
= &g_array_index (macros
->macro
, struct macro_action_t
, i
);
1832 edit_execute_cmd (edit
, m_act
->action
, m_act
->ch
);
1841 /* --------------------------------------------------------------------------------------------- */
1843 /** returns FALSE on error */
1845 edit_store_macro_cmd (WEdit
* edit
)
1849 GString
*marcros_string
;
1850 mc_config_t
*macros_config
= NULL
;
1851 const char *section_name
= "editor";
1852 gchar
*macros_fname
;
1853 GArray
*macros
; /* current macro */
1855 gboolean have_macro
= FALSE
;
1856 char *skeyname
= NULL
;
1859 editcmd_dialog_raw_key_query (_("Save macro"), _("Press the macro's new hotkey:"), TRUE
);
1860 if (hotkey
== ESC_CHAR
)
1863 tmp_act
= keybind_lookup_keymap_command (editor_map
, hotkey
);
1865 /* return FALSE if try assign macro into restricted hotkeys */
1866 if (tmp_act
== CK_MacroStartRecord
1867 || tmp_act
== CK_MacroStopRecord
|| tmp_act
== CK_MacroStartStopRecord
)
1870 edit_delete_macro (edit
, hotkey
);
1872 macros_fname
= mc_config_get_full_path (MC_MACRO_FILE
);
1873 macros_config
= mc_config_init (macros_fname
, FALSE
);
1874 g_free (macros_fname
);
1876 if (macros_config
== NULL
)
1879 edit_push_undo_action (edit
, KEY_PRESS
+ edit
->start_display
);
1881 marcros_string
= g_string_sized_new (250);
1882 macros
= g_array_new (TRUE
, FALSE
, sizeof (macro_action_t
));
1884 skeyname
= lookup_key_by_code (hotkey
);
1886 for (i
= 0; i
< macro_index
; i
++)
1888 macro_action_t m_act
;
1889 const char *action_name
;
1891 action_name
= keybind_lookup_actionname (record_macro_buf
[i
].action
);
1893 if (action_name
== NULL
)
1896 m_act
.action
= record_macro_buf
[i
].action
;
1897 m_act
.ch
= record_macro_buf
[i
].ch
;
1898 g_array_append_val (macros
, m_act
);
1900 g_string_append_printf (marcros_string
, "%s:%i;", action_name
,
1901 (int) record_macro_buf
[i
].ch
);
1906 macro
.hotkey
= hotkey
;
1907 macro
.macro
= macros
;
1908 g_array_append_val (macros_list
, macro
);
1909 mc_config_set_string (macros_config
, section_name
, skeyname
, marcros_string
->str
);
1912 mc_config_del_key (macros_config
, section_name
, skeyname
);
1915 edit_macro_sort_by_hotkey ();
1917 g_string_free (marcros_string
, TRUE
);
1918 mc_config_save_file (macros_config
, NULL
);
1919 mc_config_deinit (macros_config
);
1923 /* --------------------------------------------------------------------------------------------- */
1926 edit_repeat_macro_cmd (WEdit
* edit
)
1933 f
= input_dialog (_("Repeat last commands"), _("Repeat times:"), MC_HISTORY_EDIT_REPEAT
, NULL
,
1934 INPUT_COMPLETE_NONE
);
1935 if (f
== NULL
|| *f
== '\0')
1941 count_repeat
= strtol (f
, &error
, 0);
1951 edit_push_undo_action (edit
, KEY_PRESS
+ edit
->start_display
);
1952 edit
->force
|= REDRAW_PAGE
;
1954 for (j
= 0; j
< count_repeat
; j
++)
1955 for (i
= 0; i
< macro_index
; i
++)
1956 edit_execute_cmd (edit
, record_macro_buf
[i
].action
, record_macro_buf
[i
].ch
);
1957 edit_update_screen (edit
);
1961 /* --------------------------------------------------------------------------------------------- */
1962 /** return FALSE on error */
1965 edit_load_macro_cmd (WEdit
* edit
)
1967 mc_config_t
*macros_config
= NULL
;
1968 gchar
**profile_keys
, **keys
;
1969 gchar
**values
, **curr_values
;
1970 const char *section_name
= "editor";
1971 gchar
*macros_fname
;
1975 if (macros_list
== NULL
|| macros_list
->len
!= 0)
1978 macros_fname
= mc_config_get_full_path (MC_MACRO_FILE
);
1979 macros_config
= mc_config_init (macros_fname
, TRUE
);
1980 g_free (macros_fname
);
1982 if (macros_config
== NULL
)
1985 keys
= mc_config_get_keys (macros_config
, section_name
, NULL
);
1987 for (profile_keys
= keys
; *profile_keys
!= NULL
; profile_keys
++)
1990 gboolean have_macro
= FALSE
;
1994 macros
= g_array_new (TRUE
, FALSE
, sizeof (macro_action_t
));
1995 values
= mc_config_get_string_list (macros_config
, section_name
, *profile_keys
, NULL
);
1996 hotkey
= lookup_key (*profile_keys
, NULL
);
1998 for (curr_values
= values
; *curr_values
!= NULL
&& *curr_values
[0] != '\0'; curr_values
++)
2000 char **macro_pair
= NULL
;
2002 macro_pair
= g_strsplit (*curr_values
, ":", 2);
2003 if (macro_pair
!= NULL
)
2005 macro_action_t m_act
;
2006 if (macro_pair
[0] == NULL
|| macro_pair
[0][0] == '\0')
2010 m_act
.action
= keybind_lookup_action (macro_pair
[0]);
2011 MC_PTR_FREE (macro_pair
[0]);
2013 if (macro_pair
[1] == NULL
|| macro_pair
[1][0] == '\0')
2017 m_act
.ch
= strtol (macro_pair
[1], NULL
, 0);
2018 MC_PTR_FREE (macro_pair
[1]);
2020 if (m_act
.action
!= 0)
2022 /* a shell command */
2023 if ((m_act
.action
/ CK_PipeBlock (0)) == 1)
2025 m_act
.action
= CK_PipeBlock (0) + (m_act
.ch
> 0 ? m_act
.ch
: 0);
2028 g_array_append_val (macros
, m_act
);
2031 g_strfreev (macro_pair
);
2037 macro
.hotkey
= hotkey
;
2038 macro
.macro
= macros
;
2039 g_array_append_val (macros_list
, macro
);
2041 g_strfreev (values
);
2044 mc_config_deinit (macros_config
);
2045 edit_macro_sort_by_hotkey ();
2049 /* }}} Macro stuff end here */
2051 /* --------------------------------------------------------------------------------------------- */
2052 /** returns TRUE on success */
2055 edit_save_confirm_cmd (WEdit
* edit
)
2057 if (edit
->filename_vpath
== NULL
)
2058 return edit_save_as_cmd (edit
);
2060 if (!edit_check_newline (&edit
->buffer
))
2063 if (edit_confirm_save
)
2068 f
= g_strdup_printf (_("Confirm save file: \"%s\""),
2069 vfs_path_as_str (edit
->filename_vpath
));
2070 ok
= (edit_query_dialog2 (_("Save file"), f
, _("&Save"), _("&Cancel")) == 0);
2075 return edit_save_cmd (edit
);
2078 /* --------------------------------------------------------------------------------------------- */
2080 * Ask file to edit and load it.
2082 * @return TRUE on success or cancel of ask.
2086 edit_load_cmd (WDialog
* h
)
2089 gboolean ret
= TRUE
; /* possible cancel */
2091 exp
= input_expand_dialog (_("Load"), _("Enter file name:"),
2092 MC_HISTORY_EDIT_LOAD
, INPUT_LAST_TEXT
,
2093 INPUT_COMPLETE_FILENAMES
| INPUT_COMPLETE_CD
);
2095 if (exp
!= NULL
&& *exp
!= '\0')
2097 vfs_path_t
*exp_vpath
;
2099 exp_vpath
= vfs_path_from_str (exp
);
2100 ret
= edit_load_file_from_filename (h
, exp_vpath
);
2101 vfs_path_free (exp_vpath
);
2109 /* --------------------------------------------------------------------------------------------- */
2111 * Show history od edited or viewed files and open selected file.
2113 * @return TRUE on success, FALSE otherwise.
2117 edit_load_file_from_history (WDialog
* h
)
2121 gboolean ret
= TRUE
; /* possible cancel */
2123 exp
= show_file_history (CONST_WIDGET (h
), &action
);
2124 if (exp
!= NULL
&& (action
== CK_Edit
|| action
== CK_Enter
))
2126 vfs_path_t
*exp_vpath
;
2128 exp_vpath
= vfs_path_from_str (exp
);
2129 ret
= edit_load_file_from_filename (h
, exp_vpath
);
2130 vfs_path_free (exp_vpath
);
2138 /* --------------------------------------------------------------------------------------------- */
2140 * Load syntax file to edit.
2142 * @return TRUE on success
2146 edit_load_syntax_file (WDialog
* h
)
2148 vfs_path_t
*extdir_vpath
;
2150 gboolean ret
= FALSE
;
2152 if (geteuid () == 0)
2153 dir
= query_dialog (_("Syntax file edit"),
2154 _("Which syntax file you want to edit?"), D_NORMAL
, 2,
2155 _("&User"), _("&System wide"));
2158 vfs_path_build_filename (mc_global
.sysconfig_dir
, "syntax", "Syntax", (char *) NULL
);
2159 if (!exist_file (vfs_path_get_last_path_str (extdir_vpath
)))
2161 vfs_path_free (extdir_vpath
);
2163 vfs_path_build_filename (mc_global
.share_data_dir
, "syntax", "Syntax", (char *) NULL
);
2168 vfs_path_t
*user_syntax_file_vpath
;
2170 user_syntax_file_vpath
= mc_config_get_full_vpath (EDIT_SYNTAX_FILE
);
2171 check_for_default (extdir_vpath
, user_syntax_file_vpath
);
2172 ret
= edit_load_file_from_filename (h
, user_syntax_file_vpath
);
2173 vfs_path_free (user_syntax_file_vpath
);
2176 ret
= edit_load_file_from_filename (h
, extdir_vpath
);
2178 vfs_path_free (extdir_vpath
);
2183 /* --------------------------------------------------------------------------------------------- */
2185 * Load menu file to edit.
2187 * @return TRUE on success
2191 edit_load_menu_file (WDialog
* h
)
2193 vfs_path_t
*buffer_vpath
;
2194 vfs_path_t
*menufile_vpath
;
2199 dir
= query_dialog (_("Menu edit"),
2200 _("Which menu file do you want to edit?"), D_NORMAL
,
2201 geteuid () != 0 ? 2 : 3, _("&Local"), _("&User"), _("&System wide"));
2204 vfs_path_build_filename (mc_global
.sysconfig_dir
, EDIT_GLOBAL_MENU
, (char *) NULL
);
2205 if (!exist_file (vfs_path_get_last_path_str (menufile_vpath
)))
2207 vfs_path_free (menufile_vpath
);
2209 vfs_path_build_filename (mc_global
.share_data_dir
, EDIT_GLOBAL_MENU
, (char *) NULL
);
2215 buffer_vpath
= vfs_path_from_str (EDIT_LOCAL_MENU
);
2216 check_for_default (menufile_vpath
, buffer_vpath
);
2217 chmod (vfs_path_get_last_path_str (buffer_vpath
), 0600);
2221 buffer_vpath
= mc_config_get_full_vpath (EDIT_HOME_MENU
);
2222 check_for_default (menufile_vpath
, buffer_vpath
);
2227 vfs_path_build_filename (mc_global
.sysconfig_dir
, EDIT_GLOBAL_MENU
, (char *) NULL
);
2228 if (!exist_file (vfs_path_get_last_path_str (buffer_vpath
)))
2230 vfs_path_free (buffer_vpath
);
2232 vfs_path_build_filename (mc_global
.share_data_dir
, EDIT_GLOBAL_MENU
, (char *) NULL
);
2237 vfs_path_free (menufile_vpath
);
2241 ret
= edit_load_file_from_filename (h
, buffer_vpath
);
2243 vfs_path_free (buffer_vpath
);
2244 vfs_path_free (menufile_vpath
);
2249 /* --------------------------------------------------------------------------------------------- */
2251 * Close window with opened file.
2253 * @return TRUE if file was closed.
2257 edit_close_cmd (WEdit
* edit
)
2261 ret
= (edit
!= NULL
) && edit_ok_to_exit (edit
);
2265 WGroup
*g
= WIDGET (edit
)->owner
;
2266 WDialog
*h
= DIALOG (g
);
2268 if (edit
->locked
!= 0)
2269 unlock_file (edit
->filename_vpath
);
2271 group_del_widget (edit
);
2272 widget_destroy (WIDGET (edit
));
2274 if (edit_widget_is_editor (CONST_WIDGET (g
->current
->data
)))
2275 edit
= (WEdit
*) (g
->current
->data
);
2278 edit
= find_editor (h
);
2280 widget_select (WIDGET (edit
));
2285 edit
->force
|= REDRAW_COMPLETELY
;
2290 /* --------------------------------------------------------------------------------------------- */
2292 if mark2 is -1 then marking is from mark1 to the cursor.
2293 Otherwise its between the markers. This handles this.
2294 Returns FALSE if no text is marked.
2298 eval_marks (WEdit
* edit
, off_t
* start_mark
, off_t
* end_mark
)
2302 if (edit
->mark1
== edit
->mark2
)
2304 *start_mark
= *end_mark
= 0;
2305 edit
->column2
= edit
->column1
= 0;
2309 if (edit
->end_mark_curs
< 0)
2310 end_mark_curs
= edit
->buffer
.curs1
;
2312 end_mark_curs
= edit
->end_mark_curs
;
2314 if (edit
->mark2
>= 0)
2316 *start_mark
= MIN (edit
->mark1
, edit
->mark2
);
2317 *end_mark
= MAX (edit
->mark1
, edit
->mark2
);
2321 *start_mark
= MIN (edit
->mark1
, end_mark_curs
);
2322 *end_mark
= MAX (edit
->mark1
, end_mark_curs
);
2323 edit
->column2
= edit
->curs_col
+ edit
->over_col
;
2326 if (edit
->column_highlight
2327 && ((edit
->mark1
> end_mark_curs
&& edit
->column1
< edit
->column2
)
2328 || (edit
->mark1
< end_mark_curs
&& edit
->column1
> edit
->column2
)))
2330 off_t start_bol
, start_eol
;
2331 off_t end_bol
, end_eol
;
2335 start_bol
= edit_buffer_get_bol (&edit
->buffer
, *start_mark
);
2336 start_eol
= edit_buffer_get_eol (&edit
->buffer
, start_bol
- 1) + 1;
2337 end_bol
= edit_buffer_get_bol (&edit
->buffer
, *end_mark
);
2338 end_eol
= edit_buffer_get_eol (&edit
->buffer
, *end_mark
);
2339 col1
= MIN (edit
->column1
, edit
->column2
);
2340 col2
= MAX (edit
->column1
, edit
->column2
);
2342 diff1
= edit_move_forward3 (edit
, start_bol
, col2
, 0) -
2343 edit_move_forward3 (edit
, start_bol
, col1
, 0);
2344 diff2
= edit_move_forward3 (edit
, end_bol
, col2
, 0) -
2345 edit_move_forward3 (edit
, end_bol
, col1
, 0);
2347 *start_mark
-= diff1
;
2349 *start_mark
= MAX (*start_mark
, start_eol
);
2350 *end_mark
= MIN (*end_mark
, end_eol
);
2355 /* --------------------------------------------------------------------------------------------- */
2358 edit_block_copy_cmd (WEdit
* edit
)
2360 off_t start_mark
, end_mark
, current
= edit
->buffer
.curs1
;
2361 off_t mark1
= 0, mark2
= 0;
2362 long c1
= 0, c2
= 0;
2364 unsigned char *copy_buf
;
2366 edit_update_curs_col (edit
);
2367 if (!eval_marks (edit
, &start_mark
, &end_mark
))
2370 copy_buf
= edit_get_block (edit
, start_mark
, end_mark
, &size
);
2372 /* all that gets pushed are deletes hence little space is used on the stack */
2374 edit_push_markers (edit
);
2376 if (edit
->column_highlight
)
2380 col_delta
= labs (edit
->column2
- edit
->column1
);
2381 edit_insert_column_of_text (edit
, copy_buf
, size
, col_delta
, &mark1
, &mark2
, &c1
, &c2
);
2385 int size_orig
= size
;
2388 edit_insert_ahead (edit
, copy_buf
[size
]);
2390 /* Place cursor at the end of text selection */
2391 if (option_cursor_after_inserted_block
)
2392 edit_cursor_move (edit
, size_orig
);
2396 edit_scroll_screen_over_cursor (edit
);
2398 if (edit
->column_highlight
)
2399 edit_set_markers (edit
, edit
->buffer
.curs1
, mark2
, c1
, c2
);
2400 else if (start_mark
< current
&& end_mark
> current
)
2401 edit_set_markers (edit
, start_mark
, end_mark
+ end_mark
- start_mark
, 0, 0);
2403 edit
->force
|= REDRAW_PAGE
;
2407 /* --------------------------------------------------------------------------------------------- */
2410 edit_block_move_cmd (WEdit
* edit
)
2413 unsigned char *copy_buf
= NULL
;
2414 off_t start_mark
, end_mark
;
2416 if (!eval_marks (edit
, &start_mark
, &end_mark
))
2419 if (!edit
->column_highlight
&& edit
->buffer
.curs1
> start_mark
&& edit
->buffer
.curs1
< end_mark
)
2422 if (edit
->mark2
< 0)
2423 edit_mark_cmd (edit
, FALSE
);
2424 edit_push_markers (edit
);
2426 if (edit
->column_highlight
)
2430 long c1
, c2
, b_width
;
2433 c1
= MIN (edit
->column1
, edit
->column2
);
2434 c2
= MAX (edit
->column1
, edit
->column2
);
2437 edit_update_curs_col (edit
);
2440 x2
= x
+ edit
->over_col
;
2442 /* do nothing when cursor inside first line of selected area */
2443 if ((edit_buffer_get_eol (&edit
->buffer
, edit
->buffer
.curs1
) ==
2444 edit_buffer_get_eol (&edit
->buffer
, start_mark
)) && x2
> c1
&& x2
<= c2
)
2447 if (edit
->buffer
.curs1
> start_mark
2448 && edit
->buffer
.curs1
< edit_buffer_get_eol (&edit
->buffer
, end_mark
))
2452 else if (x
> c1
&& x
<= c2
)
2455 /* save current selection into buffer */
2456 copy_buf
= edit_get_block (edit
, start_mark
, end_mark
, &size
);
2458 /* remove current selection */
2459 edit_block_delete_cmd (edit
);
2461 edit
->over_col
= MAX (0, edit
->over_col
- b_width
);
2462 /* calculate the cursor pos after delete block */
2463 current
= edit_move_forward3 (edit
, edit_buffer_get_current_bol (&edit
->buffer
), x
, 0);
2464 edit_cursor_move (edit
, current
- edit
->buffer
.curs1
);
2465 edit_scroll_screen_over_cursor (edit
);
2467 /* add TWS if need before block insertion */
2468 if (option_cursor_beyond_eol
&& edit
->over_col
> 0)
2469 edit_insert_over (edit
);
2471 edit_insert_column_of_text (edit
, copy_buf
, size
, b_width
, &mark1
, &mark2
, &c1
, &c2
);
2472 edit_set_markers (edit
, mark1
, mark2
, c1
, c2
);
2476 off_t count
, count_orig
;
2478 current
= edit
->buffer
.curs1
;
2479 copy_buf
= g_malloc0 (end_mark
- start_mark
);
2480 edit_cursor_move (edit
, start_mark
- edit
->buffer
.curs1
);
2481 edit_scroll_screen_over_cursor (edit
);
2483 for (count
= start_mark
; count
< end_mark
; count
++)
2484 copy_buf
[end_mark
- count
- 1] = edit_delete (edit
, TRUE
);
2486 edit_scroll_screen_over_cursor (edit
);
2487 edit_cursor_move (edit
,
2488 current
- edit
->buffer
.curs1
-
2489 (((current
- edit
->buffer
.curs1
) > 0) ? end_mark
- start_mark
: 0));
2490 edit_scroll_screen_over_cursor (edit
);
2492 while (count
-- > start_mark
)
2493 edit_insert_ahead (edit
, copy_buf
[end_mark
- count
- 1]);
2495 edit_set_markers (edit
, edit
->buffer
.curs1
, edit
->buffer
.curs1
+ end_mark
- start_mark
, 0,
2498 /* Place cursor at the end of text selection */
2499 if (option_cursor_after_inserted_block
)
2500 edit_cursor_move (edit
, count_orig
- start_mark
);
2503 edit_scroll_screen_over_cursor (edit
);
2505 edit
->force
|= REDRAW_PAGE
;
2508 /* --------------------------------------------------------------------------------------------- */
2509 /** returns 1 if canceelled by user */
2512 edit_block_delete_cmd (WEdit
* edit
)
2514 off_t start_mark
, end_mark
;
2516 if (eval_marks (edit
, &start_mark
, &end_mark
))
2517 return edit_block_delete (edit
);
2519 edit_delete_line (edit
);
2523 /* --------------------------------------------------------------------------------------------- */
2524 /** call with edit = 0 before shutdown to close memory leaks */
2527 edit_replace_cmd (WEdit
* edit
, gboolean again
)
2529 /* 1 = search string, 2 = replace with */
2530 static char *saved1
= NULL
; /* saved default[123] */
2531 static char *saved2
= NULL
;
2532 char *input1
= NULL
; /* user input from the dialog */
2533 char *input2
= NULL
;
2534 GString
*input2_str
= NULL
;
2537 long times_replaced
= 0;
2538 gboolean once_found
= FALSE
;
2539 edit_search_status_msg_t esm
;
2543 MC_PTR_FREE (saved1
);
2544 MC_PTR_FREE (saved2
);
2548 edit
->force
|= REDRAW_COMPLETELY
;
2550 if (again
&& !saved1
&& !saved2
)
2555 input1
= g_strdup (saved1
? saved1
: "");
2556 input2
= g_strdup (saved2
? saved2
: "");
2560 char *tmp_inp1
, *tmp_inp2
;
2562 disp1
= edit_replace_cmd__conv_to_display (saved1
? saved1
: "");
2563 disp2
= edit_replace_cmd__conv_to_display (saved2
? saved2
: "");
2565 edit_push_undo_action (edit
, KEY_PRESS
+ edit
->start_display
);
2567 editcmd_dialog_replace_show (edit
, disp1
, disp2
, &input1
, &input2
);
2572 if (input1
== NULL
|| *input1
== '\0')
2574 edit
->force
= REDRAW_COMPLETELY
;
2580 input1
= edit_replace_cmd__conv_to_input (input1
);
2581 input2
= edit_replace_cmd__conv_to_input (input2
);
2586 saved1
= g_strdup (input1
);
2588 saved2
= g_strdup (input2
);
2590 mc_search_free (edit
->search
);
2591 edit
->search
= NULL
;
2594 input2_str
= g_string_new (input2
);
2596 if (edit
->search
== NULL
)
2599 edit
->search
= mc_search_new (input1
, cp_source
);
2601 edit
->search
= mc_search_new (input1
, NULL
);
2603 if (edit
->search
== NULL
)
2605 edit
->search_start
= edit
->buffer
.curs1
;
2608 edit
->search
->search_type
= edit_search_options
.type
;
2610 edit
->search
->is_all_charsets
= edit_search_options
.all_codepages
;
2612 edit
->search
->is_case_sensitive
= edit_search_options
.case_sens
;
2613 edit
->search
->whole_words
= edit_search_options
.whole_words
;
2614 edit
->search
->search_fn
= edit_search_cmd_callback
;
2615 edit
->search
->update_fn
= edit_search_update_callback
;
2616 edit
->search_line_type
= edit_get_search_line_type (edit
->search
);
2617 edit_search_fix_search_start_if_selection (edit
);
2620 if (edit
->found_len
&& edit
->search_start
== edit
->found_start
+ 1
2621 && edit_search_options
.backwards
)
2622 edit
->search_start
--;
2624 if (edit
->found_len
&& edit
->search_start
== edit
->found_start
- 1
2625 && !edit_search_options
.backwards
)
2626 edit
->search_start
++;
2630 esm
.offset
= edit
->search_start
;
2632 status_msg_init (STATUS_MSG (&esm
), _("Search"), 1.0, simple_status_msg_init_cb
,
2633 edit_search_status_update_cb
, NULL
);
2639 if (!editcmd_find (&esm
, &len
))
2641 if (!(edit
->search
->error
== MC_SEARCH_E_OK
||
2642 (once_found
&& edit
->search
->error
== MC_SEARCH_E_NOTFOUND
)))
2643 edit_show_search_error (edit
, _("Search"));
2649 edit
->search_start
= edit
->search
->normal_offset
;
2650 /*returns negative on not found or error in pattern */
2652 if ((edit
->search_start
>= 0) && (edit
->search_start
< edit
->buffer
.size
))
2657 edit
->found_start
= edit
->search_start
;
2658 i
= edit
->found_len
= len
;
2660 edit_cursor_move (edit
, edit
->search_start
- edit
->buffer
.curs1
);
2661 edit_scroll_screen_over_cursor (edit
);
2663 if (edit
->replace_mode
== 0)
2668 l
= edit
->curs_row
- WIDGET (edit
)->lines
/ 3;
2670 edit_scroll_downward (edit
, l
);
2672 edit_scroll_upward (edit
, -l
);
2674 edit_scroll_screen_over_cursor (edit
);
2675 edit
->force
|= REDRAW_PAGE
;
2676 edit_render_keypress (edit
);
2678 /*so that undo stops at each query */
2679 edit_push_key_press (edit
);
2680 /* and prompt 2/3 down */
2681 disp1
= edit_replace_cmd__conv_to_display (saved1
);
2682 disp2
= edit_replace_cmd__conv_to_display (saved2
);
2683 prompt
= editcmd_dialog_replace_prompt_show (edit
, disp1
, disp2
, -1, -1);
2687 if (prompt
== B_REPLACE_ALL
)
2688 edit
->replace_mode
= 1;
2689 else if (prompt
== B_SKIP_REPLACE
)
2691 if (edit_search_options
.backwards
)
2692 edit
->search_start
--;
2694 edit
->search_start
++;
2695 continue; /* loop */
2697 else if (prompt
== B_CANCEL
)
2699 edit
->replace_mode
= -1;
2704 repl_str
= mc_search_prepare_replace_str (edit
->search
, input2_str
);
2706 if (edit
->search
->error
!= MC_SEARCH_E_OK
)
2708 edit_show_search_error (edit
, _("Replace"));
2709 g_string_free (repl_str
, TRUE
);
2713 /* delete then insert new */
2714 for (i
= 0; i
< len
; i
++)
2715 edit_delete (edit
, TRUE
);
2717 for (i
= 0; i
< repl_str
->len
; i
++)
2718 edit_insert (edit
, repl_str
->str
[i
]);
2720 edit
->found_len
= repl_str
->len
;
2721 g_string_free (repl_str
, TRUE
);
2724 /* so that we don't find the same string again */
2725 if (edit_search_options
.backwards
)
2727 edit
->search_start
--;
2731 edit
->search_start
+= edit
->found_len
+ (len
== 0 ? 1 : 0);
2733 if (edit
->search_start
>= edit
->buffer
.size
)
2737 edit_scroll_screen_over_cursor (edit
);
2741 /* try and find from right here for next search */
2742 edit
->search_start
= edit
->buffer
.curs1
;
2743 edit_update_curs_col (edit
);
2745 edit
->force
|= REDRAW_PAGE
;
2746 edit_render_keypress (edit
);
2748 if (times_replaced
== 0)
2749 query_dialog (_("Replace"), _(STR_E_NOTFOUND
), D_NORMAL
, 1, _("&OK"));
2753 while (edit
->replace_mode
>= 0);
2755 status_msg_deinit (STATUS_MSG (&esm
));
2756 edit_scroll_screen_over_cursor (edit
);
2757 edit
->force
|= REDRAW_COMPLETELY
;
2758 edit_render_keypress (edit
);
2760 if ((edit
->replace_mode
== 1) && (times_replaced
!= 0))
2761 message (D_NORMAL
, _("Replace"), _("%ld replacements made"), times_replaced
);
2766 if (input2_str
!= NULL
)
2767 g_string_free (input2_str
, TRUE
);
2770 /* --------------------------------------------------------------------------------------------- */
2773 edit_search_cmd_callback (const void *user_data
, gsize char_offset
, int *current_char
)
2775 WEdit
*edit
= ((const edit_search_status_msg_t
*) user_data
)->edit
;
2777 *current_char
= edit_buffer_get_byte (&edit
->buffer
, (off_t
) char_offset
);
2778 return MC_SEARCH_CB_OK
;
2781 /* --------------------------------------------------------------------------------------------- */
2784 edit_search_update_callback (const void *user_data
, gsize char_offset
)
2786 status_msg_t
*sm
= STATUS_MSG (user_data
);
2788 ((edit_search_status_msg_t
*) sm
)->offset
= (off_t
) char_offset
;
2790 return (sm
->update (sm
) == B_CANCEL
? MC_SEARCH_CB_ABORT
: MC_SEARCH_CB_OK
);
2793 /* --------------------------------------------------------------------------------------------- */
2796 edit_search_cmd (WEdit
* edit
, gboolean again
)
2804 else if (edit
->last_search_string
!= NULL
)
2805 edit_do_search (edit
);
2808 /* find last search string in history */
2811 history
= mc_config_history_get (MC_HISTORY_SHARED_SEARCH
);
2812 if (history
!= NULL
&& history
->data
!= NULL
)
2814 edit
->last_search_string
= (char *) history
->data
;
2815 history
->data
= NULL
;
2816 history
= g_list_first (history
);
2817 g_list_free_full (history
, g_free
);
2820 edit
->search
= mc_search_new (edit
->last_search_string
, cp_source
);
2822 edit
->search
= mc_search_new (edit
->last_search_string
, NULL
);
2824 if (edit
->search
== NULL
)
2826 /* if not... then ask for an expression */
2827 MC_PTR_FREE (edit
->last_search_string
);
2832 edit
->search
->search_type
= edit_search_options
.type
;
2834 edit
->search
->is_all_charsets
= edit_search_options
.all_codepages
;
2836 edit
->search
->is_case_sensitive
= edit_search_options
.case_sens
;
2837 edit
->search
->whole_words
= edit_search_options
.whole_words
;
2838 edit
->search
->search_fn
= edit_search_cmd_callback
;
2839 edit
->search
->update_fn
= edit_search_update_callback
;
2840 edit
->search_line_type
= edit_get_search_line_type (edit
->search
);
2841 edit_do_search (edit
);
2846 /* if not... then ask for an expression */
2847 MC_PTR_FREE (edit
->last_search_string
);
2854 /* --------------------------------------------------------------------------------------------- */
2856 * Check if it's OK to close the file. If there are unsaved changes, ask user.
2858 * @return TRUE if it's OK to exit, FALSE to continue editing.
2862 edit_ok_to_exit (WEdit
* edit
)
2864 const char *fname
= N_("[NoName]");
2868 if (!edit
->modified
)
2871 if (edit
->filename_vpath
!= NULL
)
2872 fname
= vfs_path_as_str (edit
->filename_vpath
);
2878 if (!mc_global
.midnight_shutdown
)
2882 msg
= g_strdup_printf (_("File %s was modified.\nSave before close?"), fname
);
2883 act
= edit_query_dialog3 (_("Close file"), msg
, _("&Yes"), _("&No"), _("&Cancel"));
2887 msg
= g_strdup_printf (_("Midnight Commander is being shut down.\nSave modified file %s?"),
2889 act
= edit_query_dialog2 (_("Quit"), msg
, _("&Yes"), _("&No"));
2901 if (!mc_global
.midnight_shutdown
&& !edit_check_newline (&edit
->buffer
))
2903 edit_push_markers (edit
);
2904 edit_set_markers (edit
, 0, 0, 0, 0);
2905 if (!edit_save_cmd (edit
) || mc_global
.midnight_shutdown
)
2906 return mc_global
.midnight_shutdown
;
2911 case 2: /* Cancel quit */
2919 /* --------------------------------------------------------------------------------------------- */
2920 /** save block, returns TRUE on success */
2923 edit_save_block (WEdit
* edit
, const char *filename
, off_t start
, off_t finish
)
2929 vpath
= vfs_path_from_str (filename
);
2930 file
= mc_open (vpath
, O_CREAT
| O_WRONLY
| O_TRUNC
,
2931 S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IROTH
| O_BINARY
);
2932 vfs_path_free (vpath
);
2936 if (edit
->column_highlight
)
2940 r
= mc_write (file
, VERTICAL_MAGIC
, sizeof (VERTICAL_MAGIC
));
2943 unsigned char *block
, *p
;
2945 p
= block
= edit_get_block (edit
, start
, finish
, &len
);
2948 r
= mc_write (file
, p
, len
);
2963 len
= finish
- start
;
2964 buf
= g_malloc0 (TEMP_BUF_LEN
);
2965 while (start
!= finish
)
2967 end
= MIN (finish
, start
+ TEMP_BUF_LEN
);
2968 for (; i
< end
; i
++)
2969 buf
[i
- start
] = edit_buffer_get_byte (&edit
->buffer
, i
);
2970 len
-= mc_write (file
, (char *) buf
, end
- start
);
2980 /* --------------------------------------------------------------------------------------------- */
2983 edit_paste_from_history (WEdit
* edit
)
2986 edit_error_dialog (_("Error"), _("This function is not implemented"));
2989 /* --------------------------------------------------------------------------------------------- */
2992 edit_copy_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 (_("Copy to clipboard"), get_sys_error (_("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 if (option_drop_selection_on_copy
)
3008 edit_mark_cmd (edit
, TRUE
);
3013 /* --------------------------------------------------------------------------------------------- */
3016 edit_cut_to_X_buf_cmd (WEdit
* edit
)
3018 off_t start_mark
, end_mark
;
3020 if (!eval_marks (edit
, &start_mark
, &end_mark
))
3023 if (!edit_save_block_to_clip_file (edit
, start_mark
, end_mark
))
3025 edit_error_dialog (_("Cut to clipboard"), _("Unable to save to file"));
3028 /* try use external clipboard utility */
3029 mc_event_raise (MCEVENT_GROUP_CORE
, "clipboard_file_to_ext_clip", NULL
);
3031 edit_block_delete_cmd (edit
);
3032 edit_mark_cmd (edit
, TRUE
);
3037 /* --------------------------------------------------------------------------------------------- */
3040 edit_paste_from_X_buf_cmd (WEdit
* edit
)
3045 /* try use external clipboard utility */
3046 mc_event_raise (MCEVENT_GROUP_CORE
, "clipboard_file_from_ext_clip", NULL
);
3047 tmp
= mc_config_get_full_vpath (EDIT_CLIP_FILE
);
3048 ret
= (edit_insert_file (edit
, tmp
) >= 0);
3049 vfs_path_free (tmp
);
3054 /* --------------------------------------------------------------------------------------------- */
3056 * Ask user for the line and go to that line.
3057 * Negative numbers mean line from the end (i.e. -1 is the last line).
3061 edit_goto_cmd (WEdit
* edit
)
3063 static gboolean first_run
= TRUE
;
3069 f
= input_dialog (_("Goto line"), _("Enter line:"), MC_HISTORY_EDIT_GOTO_LINE
,
3070 first_run
? NULL
: INPUT_LAST_TEXT
, INPUT_COMPLETE_NONE
);
3071 if (f
== NULL
|| *f
== '\0')
3077 l
= strtol (f
, &error
, 0);
3085 l
= edit
->buffer
.lines
+ l
+ 2;
3086 edit_move_display (edit
, l
- WIDGET (edit
)->lines
/ 2 - 1);
3087 edit_move_to_line (edit
, l
- 1);
3088 edit
->force
|= REDRAW_COMPLETELY
;
3095 /* --------------------------------------------------------------------------------------------- */
3096 /** Return TRUE on success */
3099 edit_save_block_cmd (WEdit
* edit
)
3101 off_t start_mark
, end_mark
;
3103 gboolean ret
= FALSE
;
3105 if (!eval_marks (edit
, &start_mark
, &end_mark
))
3108 tmp
= mc_config_get_full_path (EDIT_CLIP_FILE
);
3110 input_expand_dialog (_("Save block"), _("Enter file name:"),
3111 MC_HISTORY_EDIT_SAVE_BLOCK
, tmp
, INPUT_COMPLETE_FILENAMES
);
3113 edit_push_undo_action (edit
, KEY_PRESS
+ edit
->start_display
);
3115 if (exp
!= NULL
&& *exp
!= '\0')
3117 if (edit_save_block (edit
, exp
, start_mark
, end_mark
))
3120 edit_error_dialog (_("Save block"), get_sys_error (_("Cannot save file")));
3122 edit
->force
|= REDRAW_COMPLETELY
;
3131 /* --------------------------------------------------------------------------------------------- */
3133 /** returns TRUE on success */
3135 edit_insert_file_cmd (WEdit
* edit
)
3139 gboolean ret
= FALSE
;
3141 tmp
= mc_config_get_full_path (EDIT_CLIP_FILE
);
3142 exp
= input_expand_dialog (_("Insert file"), _("Enter file name:"),
3143 MC_HISTORY_EDIT_INSERT_FILE
, tmp
, INPUT_COMPLETE_FILENAMES
);
3146 edit_push_undo_action (edit
, KEY_PRESS
+ edit
->start_display
);
3148 if (exp
!= NULL
&& *exp
!= '\0')
3150 vfs_path_t
*exp_vpath
;
3152 exp_vpath
= vfs_path_from_str (exp
);
3153 ret
= (edit_insert_file (edit
, exp_vpath
) >= 0);
3154 vfs_path_free (exp_vpath
);
3157 edit_error_dialog (_("Insert file"), get_sys_error (_("Cannot insert file")));
3162 edit
->force
|= REDRAW_COMPLETELY
;
3166 /* --------------------------------------------------------------------------------------------- */
3167 /** sorts a block, returns -1 on system fail, 1 on cancel and 0 on success */
3170 edit_sort_cmd (WEdit
* edit
)
3172 char *exp
, *tmp
, *tmp_edit_block_name
, *tmp_edit_temp_name
;
3173 off_t start_mark
, end_mark
;
3176 if (!eval_marks (edit
, &start_mark
, &end_mark
))
3178 edit_error_dialog (_("Sort block"), _("You must first highlight a block of text"));
3182 tmp
= mc_config_get_full_path (EDIT_BLOCK_FILE
);
3183 edit_save_block (edit
, tmp
, start_mark
, end_mark
);
3186 exp
= input_dialog (_("Run sort"),
3187 _("Enter sort options (see manpage) separated by whitespace:"),
3188 MC_HISTORY_EDIT_SORT
, INPUT_LAST_TEXT
, INPUT_COMPLETE_NONE
);
3193 tmp_edit_block_name
= mc_config_get_full_path (EDIT_BLOCK_FILE
);
3194 tmp_edit_temp_name
= mc_config_get_full_path (EDIT_TEMP_FILE
);
3196 g_strconcat (" sort ", exp
, " ", tmp_edit_block_name
,
3197 " > ", tmp_edit_temp_name
, (char *) NULL
);
3198 g_free (tmp_edit_temp_name
);
3199 g_free (tmp_edit_block_name
);
3206 if (e
== -1 || e
== 127)
3207 edit_error_dialog (_("Sort"), get_sys_error (_("Cannot execute sort command")));
3212 sprintf (q
, "%d ", e
);
3213 tmp
= g_strdup_printf (_("Sort returned non-zero: %s"), q
);
3214 edit_error_dialog (_("Sort"), tmp
);
3220 edit
->force
|= REDRAW_COMPLETELY
;
3222 if (edit_block_delete_cmd (edit
))
3226 vfs_path_t
*tmp_vpath
;
3228 tmp_vpath
= mc_config_get_full_vpath (EDIT_TEMP_FILE
);
3229 edit_insert_file (edit
, tmp_vpath
);
3230 vfs_path_free (tmp_vpath
);
3235 /* --------------------------------------------------------------------------------------------- */
3237 * Ask user for a command, execute it and paste its output back to the
3242 edit_ext_cmd (WEdit
* edit
)
3244 char *exp
, *tmp
, *tmp_edit_temp_file
;
3248 input_dialog (_("Paste output of external command"),
3249 _("Enter shell command(s):"), MC_HISTORY_EDIT_PASTE_EXTCMD
, INPUT_LAST_TEXT
,
3250 INPUT_COMPLETE_FILENAMES
| INPUT_COMPLETE_VARIABLES
| INPUT_COMPLETE_USERNAMES
3251 | INPUT_COMPLETE_HOSTNAMES
| INPUT_COMPLETE_CD
| INPUT_COMPLETE_COMMANDS
|
3252 INPUT_COMPLETE_SHELL_ESC
);
3257 tmp_edit_temp_file
= mc_config_get_full_path (EDIT_TEMP_FILE
);
3258 tmp
= g_strconcat (exp
, " > ", tmp_edit_temp_file
, (char *) NULL
);
3259 g_free (tmp_edit_temp_file
);
3266 edit_error_dialog (_("External command"), get_sys_error (_("Cannot execute command")));
3270 edit
->force
|= REDRAW_COMPLETELY
;
3273 vfs_path_t
*tmp_vpath
;
3275 tmp_vpath
= mc_config_get_full_vpath (EDIT_TEMP_FILE
);
3276 edit_insert_file (edit
, tmp_vpath
);
3277 vfs_path_free (tmp_vpath
);
3282 /* --------------------------------------------------------------------------------------------- */
3283 /** if block is 1, a block must be highlighted and the shell command
3284 processes it. If block is 0 the shell command is a straight system
3285 command, that just produces some output which is to be inserted */
3288 edit_block_process_cmd (WEdit
* edit
, int macro_number
)
3291 char *macros_fname
= NULL
;
3293 fname
= g_strdup_printf ("%s.%i.sh", MC_EXTMACRO_FILE
, macro_number
);
3294 macros_fname
= g_build_filename (mc_config_get_data_path (), fname
, (char *) NULL
);
3295 user_menu (edit
, macros_fname
, 0);
3297 g_free (macros_fname
);
3298 edit
->force
|= REDRAW_COMPLETELY
;
3301 /* --------------------------------------------------------------------------------------------- */
3304 edit_mail_dialog (WEdit
* edit
)
3306 char *mail_to
, *mail_subject
, *mail_cc
;
3308 quick_widget_t quick_widgets
[] = {
3310 QUICK_LABEL (N_("mail -s <subject> -c <cc> <to>"), NULL
),
3311 QUICK_LABELED_INPUT (N_("To"), input_label_above
,
3312 INPUT_LAST_TEXT
, "mail-dlg-input-3",
3313 &mail_to
, NULL
, FALSE
, FALSE
, INPUT_COMPLETE_USERNAMES
),
3314 QUICK_LABELED_INPUT (N_("Subject"), input_label_above
,
3315 INPUT_LAST_TEXT
, "mail-dlg-input-2",
3316 &mail_subject
, NULL
, FALSE
, FALSE
, INPUT_COMPLETE_NONE
),
3317 QUICK_LABELED_INPUT (N_("Copies to"), input_label_above
,
3318 INPUT_LAST_TEXT
, "mail-dlg-input",
3319 &mail_cc
, NULL
, FALSE
, FALSE
, INPUT_COMPLETE_USERNAMES
),
3320 QUICK_BUTTONS_OK_CANCEL
,
3325 quick_dialog_t qdlg
= {
3327 N_("Mail"), "[Input Line Keys]",
3328 quick_widgets
, NULL
, NULL
3331 if (quick_dialog (&qdlg
) != B_CANCEL
)
3333 pipe_mail (&edit
->buffer
, mail_to
, mail_subject
, mail_cc
);
3335 g_free (mail_subject
);
3340 /* --------------------------------------------------------------------------------------------- */
3342 /*******************/
3343 /* Word Completion */
3344 /*******************/
3347 * Complete current word using regular expression search
3348 * backwards beginning at the current cursor position.
3352 edit_complete_word_cmd (WEdit
* edit
)
3354 gsize i
, max_len
, word_len
= 0, num_compl
= 0;
3355 off_t word_start
= 0;
3356 GString
*match_expr
;
3357 GString
*compl[MAX_WORD_COMPLETIONS
]; /* completions */
3359 /* search start of word to be completed */
3360 if (!edit_find_word_start (&edit
->buffer
, &word_start
, &word_len
))
3363 /* prepare match expression */
3364 /* match_expr = g_strdup_printf ("\\b%.*s[a-zA-Z_0-9]+", word_len, bufpos); */
3365 match_expr
= g_string_new ("(^|\\s+|\\b)");
3366 for (i
= 0; i
< word_len
; i
++)
3367 g_string_append_c (match_expr
, edit_buffer_get_byte (&edit
->buffer
, word_start
+ i
));
3368 g_string_append (match_expr
,
3369 "[^\\s\\.=\\+\\[\\]\\(\\)\\,\\;\\:\\\"\\'\\-\\?\\/\\|\\\\\\{\\}\\*\\&\\^\\%%\\$#@\\!]+");
3371 /* collect the possible completions */
3372 /* start search from begin to end of file */
3374 edit_collect_completions (edit
, word_start
, word_len
, match_expr
->str
, (GString
**) & compl,
3379 /* insert completed word if there is only one match */
3381 edit_complete_word_insert_recoded_completion (edit
, compl[0]->str
, word_len
);
3382 /* more than one possible completion => ask the user */
3387 /* !!! usually only a beep is expected and when <ALT-TAB> is !!! */
3388 /* !!! pressed again the selection dialog pops up, but that !!! */
3389 /* !!! seems to require a further internal state !!! */
3392 /* let the user select the preferred completion */
3393 curr_compl
= editcmd_dialog_completion_show (edit
, max_len
,
3394 (GString
**) & compl, num_compl
);
3396 if (curr_compl
!= NULL
)
3398 edit_complete_word_insert_recoded_completion (edit
, curr_compl
, word_len
);
3399 g_free (curr_compl
);
3404 g_string_free (match_expr
, TRUE
);
3405 /* release memory before return */
3406 for (i
= 0; i
< num_compl
; i
++)
3407 g_string_free (compl[i
], TRUE
);
3410 /* --------------------------------------------------------------------------------------------- */
3414 edit_select_codepage_cmd (WEdit
* edit
)
3416 if (do_select_codepage ())
3417 edit_set_codeset (edit
);
3419 edit
->force
= REDRAW_PAGE
;
3420 widget_draw (WIDGET (edit
));
3424 /* --------------------------------------------------------------------------------------------- */
3427 edit_insert_literal_cmd (WEdit
* edit
)
3429 int char_for_insertion
;
3431 char_for_insertion
= editcmd_dialog_raw_key_query (_("Insert literal"),
3432 _("Press any key:"), FALSE
);
3433 edit_execute_key_command (edit
, -1, ascii_alpha_to_cntrl (char_for_insertion
));
3436 /* --------------------------------------------------------------------------------------------- */
3439 edit_begin_end_macro_cmd (WEdit
* edit
)
3441 /* edit is a pointer to the widget */
3444 long command
= macro_index
< 0 ? CK_MacroStartRecord
: CK_MacroStopRecord
;
3445 edit_execute_key_command (edit
, command
, -1);
3449 /* --------------------------------------------------------------------------------------------- */
3452 edit_begin_end_repeat_cmd (WEdit
* edit
)
3454 /* edit is a pointer to the widget */
3457 long command
= macro_index
< 0 ? CK_RepeatStartRecord
: CK_RepeatStopRecord
;
3458 edit_execute_key_command (edit
, command
, -1);
3462 /* --------------------------------------------------------------------------------------------- */
3465 edit_load_forward_cmd (WEdit
* edit
)
3468 && edit_query_dialog2 (_("Warning"),
3469 _("Current text was modified without a file save.\n"
3470 "Continue discards these changes"), _("C&ontinue"),
3473 edit
->force
|= REDRAW_COMPLETELY
;
3477 if (edit_stack_iterator
+ 1 >= MAX_HISTORY_MOVETO
)
3480 if (edit_history_moveto
[edit_stack_iterator
+ 1].line
< 1)
3483 edit_stack_iterator
++;
3484 if (edit_history_moveto
[edit_stack_iterator
].filename_vpath
!= NULL
)
3485 return edit_reload_line (edit
, edit_history_moveto
[edit_stack_iterator
].filename_vpath
,
3486 edit_history_moveto
[edit_stack_iterator
].line
);
3491 /* --------------------------------------------------------------------------------------------- */
3494 edit_load_back_cmd (WEdit
* edit
)
3497 && edit_query_dialog2 (_("Warning"),
3498 _("Current text was modified without a file save.\n"
3499 "Continue discards these changes"), _("C&ontinue"),
3502 edit
->force
|= REDRAW_COMPLETELY
;
3506 /* we are in the bottom of the stack, NO WAY! */
3507 if (edit_stack_iterator
== 0)
3510 edit_stack_iterator
--;
3511 if (edit_history_moveto
[edit_stack_iterator
].filename_vpath
!= NULL
)
3512 return edit_reload_line (edit
, edit_history_moveto
[edit_stack_iterator
].filename_vpath
,
3513 edit_history_moveto
[edit_stack_iterator
].line
);
3518 /* --------------------------------------------------------------------------------------------- */
3521 edit_get_match_keyword_cmd (WEdit
* edit
)
3523 gsize word_len
= 0, max_len
= 0;
3526 off_t word_start
= 0;
3527 GString
*match_expr
;
3530 char *tagfile
= NULL
;
3532 etags_hash_t def_hash
[MAX_DEFINITIONS
];
3534 for (i
= 0; i
< MAX_DEFINITIONS
; i
++)
3536 def_hash
[i
].filename
= NULL
;
3539 /* search start of word to be completed */
3540 if (!edit_find_word_start (&edit
->buffer
, &word_start
, &word_len
))
3543 /* prepare match expression */
3544 match_expr
= g_string_sized_new (word_len
);
3545 for (i
= 0; i
< word_len
; i
++)
3546 g_string_append_c (match_expr
, edit_buffer_get_byte (&edit
->buffer
, word_start
+ i
));
3548 ptr
= g_get_current_dir ();
3549 path
= g_strconcat (ptr
, PATH_SEP_STR
, (char *) NULL
);
3552 /* Recursive search file 'TAGS' in parent dirs */
3555 ptr
= g_path_get_dirname (path
);
3559 tagfile
= mc_build_filename (path
, TAGS_NAME
, (char *) NULL
);
3560 if (exist_file (tagfile
))
3563 while (strcmp (path
, PATH_SEP_STR
) != 0);
3568 etags_set_definition_hash (tagfile
, path
, match_expr
->str
, (etags_hash_t
*) & def_hash
);
3573 max_len
= MAX_WIDTH_DEF_DIALOG
;
3577 editcmd_dialog_select_definition_show (edit
, match_expr
->str
, max_len
, word_len
,
3578 (etags_hash_t
*) & def_hash
, num_def
);
3580 g_string_free (match_expr
, TRUE
);
3583 /* --------------------------------------------------------------------------------------------- */
3587 edit_suggest_current_word (WEdit
* edit
)
3591 off_t word_start
= 0;
3592 int retval
= B_SKIP_WORD
;
3593 GString
*match_word
;
3595 /* search start of word to spell check */
3596 match_word
= edit_buffer_get_word_from_pos (&edit
->buffer
, edit
->buffer
.curs1
, &word_start
,
3598 word_len
= match_word
->len
;
3601 if (mc_global
.source_codepage
>= 0 && (mc_global
.source_codepage
!= mc_global
.display_codepage
))
3605 tmp_word
= str_convert_to_display (match_word
->str
);
3606 g_string_free (match_word
, TRUE
);
3607 match_word
= tmp_word
;
3610 if (!aspell_check (match_word
->str
, (int) word_len
))
3616 suggest
= g_array_new (TRUE
, FALSE
, sizeof (char *));
3618 res
= aspell_suggest (suggest
, match_word
->str
, (int) word_len
);
3621 char *new_word
= NULL
;
3623 edit
->found_start
= word_start
;
3624 edit
->found_len
= word_len
;
3625 edit
->force
|= REDRAW_PAGE
;
3626 edit_scroll_screen_over_cursor (edit
);
3627 edit_render_keypress (edit
);
3629 retval
= spell_dialog_spell_suggest_show (edit
, match_word
->str
, &new_word
, suggest
);
3630 edit_cursor_move (edit
, word_len
- cut_len
);
3632 if (retval
== B_ENTER
&& new_word
!= NULL
)
3637 if (mc_global
.source_codepage
>= 0 &&
3638 (mc_global
.source_codepage
!= mc_global
.display_codepage
))
3642 tmp_word
= str_convert_to_input (new_word
);
3644 new_word
= g_string_free (tmp_word
, FALSE
);
3648 for (i
= 0; i
< word_len
; i
++)
3649 edit_backspace (edit
, TRUE
);
3650 for (; *new_word
; new_word
++)
3651 edit_insert (edit
, *new_word
);
3654 else if (retval
== B_ADD_WORD
&& match_word
!= NULL
)
3655 aspell_add_to_dict (match_word
->str
, (int) word_len
);
3659 for (i
= 0; i
< suggest
->len
; i
++)
3661 char *cur_sugg_word
;
3663 cur_sugg_word
= g_array_index (suggest
, char *, i
);
3664 g_free (cur_sugg_word
);
3666 g_array_free (suggest
, TRUE
);
3667 edit
->found_start
= 0;
3668 edit
->found_len
= 0;
3670 g_string_free (match_word
, TRUE
);
3674 /* --------------------------------------------------------------------------------------------- */
3677 edit_spellcheck_file (WEdit
* edit
)
3679 if (edit
->buffer
.curs_line
> 0)
3681 edit_cursor_move (edit
, -edit
->buffer
.curs1
);
3682 edit_move_to_prev_col (edit
, 0);
3683 edit_update_curs_row (edit
);
3690 c2
= edit_buffer_get_current_byte (&edit
->buffer
);
3694 if (edit
->buffer
.curs1
>= edit
->buffer
.size
)
3698 edit_cursor_move (edit
, 1);
3699 c2
= edit_buffer_get_current_byte (&edit
->buffer
);
3701 while (is_break_char (c1
) || is_break_char (c2
));
3703 while (edit_suggest_current_word (edit
) != B_CANCEL
);
3706 /* --------------------------------------------------------------------------------------------- */
3709 edit_set_spell_lang (void)
3713 lang_list
= g_array_new (TRUE
, FALSE
, sizeof (char *));
3714 if (aspell_get_lang_list (lang_list
) != 0)
3718 lang
= spell_dialog_lang_list_show (lang_list
);
3721 (void) aspell_set_lang (lang
);
3725 aspell_array_clean (lang_list
);
3727 #endif /* HAVE_ASPELL */
3729 /* --------------------------------------------------------------------------------------------- */