2 Editor high level editing commands
4 Copyright (C) 1996-2015
5 Free Software Foundation, Inc.
9 Andrew Borodin <aborodin@vmail.ru>, 2012-2014
10 Ilia Maslakov <il.smind@gmail.com>, 2012
12 This file is part of the Midnight Commander.
14 The Midnight Commander is free software: you can redistribute it
15 and/or modify it under the terms of the GNU General Public License as
16 published by the Free Software Foundation, either version 3 of the License,
17 or (at your option) any later version.
19 The Midnight Commander is distributed in the hope that it will be useful,
20 but WITHOUT ANY WARRANTY; without even the implied warranty of
21 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 GNU General Public License for more details.
24 You should have received a copy of the GNU General Public License
25 along with this program. If not, see <http://www.gnu.org/licenses/>.
29 * \brief Source: editor high level editing commands
34 /* #define PIPE_BLOCKS_SO_READ_BYTE_BY_BYTE */
45 #include <sys/types.h>
52 #include "lib/global.h"
53 #include "lib/tty/tty.h"
54 #include "lib/tty/key.h" /* XCTRL */
55 #include "lib/mcconfig.h"
57 #include "lib/strutil.h" /* utf string functions */
59 #include "lib/util.h" /* tilde_expand() */
60 #include "lib/vfs/vfs.h"
61 #include "lib/widget.h"
62 #include "lib/event.h" /* mc_event_raise() */
64 #include "lib/charsets.h"
67 #include "src/history.h"
68 #include "src/setup.h" /* option_tab_spacing */
70 #include "src/selcodepage.h"
72 #include "src/keybind-defaults.h"
73 #include "src/util.h" /* check_for_default() */
75 #include "edit-impl.h"
76 #include "editwidget.h"
77 #include "editcmd_dialogs.h"
80 #include "spell_dialogs.h"
84 /*** global variables ****************************************************************************/
86 /* search and replace: */
87 int search_create_bookmark
= FALSE
;
89 /* queries on a save */
90 int edit_confirm_save
= 1;
92 /* whether we need to drop selection on copy to buffer */
93 int option_drop_selection_on_copy
= 1;
95 /*** file scope macro definitions ****************************************************************/
99 #define TEMP_BUF_LEN 1024
101 #define INPUT_INDEX 9
103 /* thanks to Liviu Daia <daia@stoilow.imar.ro> for getting this
104 (and the above) routines to work properly - paul */
106 #define is_digit(x) ((x) >= '0' && (x) <= '9')
108 #define MAX_WORD_COMPLETIONS 100 /* in listbox */
110 /*** file scope type declarations ****************************************************************/
114 simple_status_msg_t status_msg
; /* base class */
119 } edit_search_status_msg_t
;
121 /*** file scope variables ************************************************************************/
123 static unsigned long edit_save_mode_radio_id
, edit_save_mode_input_id
;
125 /* --------------------------------------------------------------------------------------------- */
126 /*** file scope functions ************************************************************************/
127 /* --------------------------------------------------------------------------------------------- */
130 edit_search_status_update_cb (status_msg_t
* sm
)
132 simple_status_msg_t
*ssm
= SIMPLE_STATUS_MSG (sm
);
133 edit_search_status_msg_t
*esm
= (edit_search_status_msg_t
*) sm
;
134 Widget
*wd
= WIDGET (sm
->dlg
);
137 label_set_textv (ssm
->label
, _("Searching %s: %3d%%"), esm
->edit
->last_search_string
,
138 edit_buffer_calc_percent (&esm
->edit
->buffer
, esm
->offset
));
140 label_set_textv (ssm
->label
, _("Searching %s"), esm
->edit
->last_search_string
);
145 Widget
*lw
= WIDGET (ssm
->label
);
147 wd_width
= max (wd
->cols
, lw
->cols
+ 6);
148 widget_set_size (wd
, wd
->y
, wd
->x
, wd
->lines
, wd_width
);
149 widget_set_size (lw
, lw
->y
, wd
->x
+ (wd
->cols
- lw
->cols
) / 2, lw
->lines
, lw
->cols
);
153 return status_msg_common_update (sm
);
156 /* --------------------------------------------------------------------------------------------- */
159 edit_save_mode_callback (Widget
* w
, Widget
* sender
, widget_msg_t msg
, int parm
, void *data
)
164 if (sender
!= NULL
&& sender
->id
== edit_save_mode_radio_id
)
168 ww
= dlg_find_by_id (DIALOG (w
), edit_save_mode_input_id
);
169 widget_disable (ww
, RADIO (sender
)->sel
!= 2);
173 return MSG_NOT_HANDLED
;
176 return dlg_default_callback (w
, sender
, msg
, parm
, data
);
180 /* --------------------------------------------------------------------------------------------- */
182 /* If 0 (quick save) then a) create/truncate <filename> file,
183 b) save to <filename>;
184 if 1 (safe save) then a) save to <tempnam>,
185 b) rename <tempnam> to <filename>;
186 if 2 (do backups) then a) save to <tempnam>,
187 b) rename <filename> to <filename.backup_ext>,
188 c) rename <tempnam> to <filename>. */
190 /* returns 0 on error, -1 on abort */
193 edit_save_file (WEdit
* edit
, const vfs_path_t
* filename_vpath
)
198 int this_save_mode
, rv
, fd
= -1;
199 vfs_path_t
*real_filename_vpath
;
200 vfs_path_t
*savename_vpath
= NULL
;
201 const char *start_filename
;
202 const vfs_path_element_t
*vpath_element
;
205 vpath_element
= vfs_path_get_by_index (filename_vpath
, 0);
206 if (vpath_element
== NULL
)
209 start_filename
= vpath_element
->path
;
210 if (*start_filename
== '\0')
213 if (!IS_PATH_SEP (*start_filename
) && edit
->dir_vpath
!= NULL
)
214 real_filename_vpath
= vfs_path_append_vpath_new (edit
->dir_vpath
, filename_vpath
, NULL
);
216 real_filename_vpath
= vfs_path_clone (filename_vpath
);
218 this_save_mode
= option_save_mode
;
219 if (this_save_mode
!= EDIT_QUICK_SAVE
)
221 if (!vfs_file_is_local (real_filename_vpath
)
222 || (fd
= mc_open (real_filename_vpath
, O_RDONLY
| O_BINARY
)) == -1)
225 * The file does not exists yet, so no safe save or
226 * backup are necessary.
228 this_save_mode
= EDIT_QUICK_SAVE
;
234 rv
= mc_stat (real_filename_vpath
, &sb
);
237 if (this_save_mode
== EDIT_QUICK_SAVE
&& !edit
->skip_detach_prompt
&& sb
.st_nlink
> 1)
239 rv
= edit_query_dialog3 (_("Warning"),
240 _("File has hard-links. Detach before saving?"),
241 _("&Yes"), _("&No"), _("&Cancel"));
245 this_save_mode
= EDIT_SAFE_SAVE
;
248 edit
->skip_detach_prompt
= 1;
251 vfs_path_free (real_filename_vpath
);
256 /* Prevent overwriting changes from other editor sessions. */
257 if (edit
->stat1
.st_mtime
!= 0 && edit
->stat1
.st_mtime
!= sb
.st_mtime
)
259 /* The default action is "Cancel". */
262 rv
= edit_query_dialog2 (_("Warning"),
263 _("The file has been modified in the meantime. Save anyway?"),
264 _("&Yes"), _("&Cancel"));
267 vfs_path_free (real_filename_vpath
);
273 if (this_save_mode
!= EDIT_QUICK_SAVE
)
275 char *savedir
, *saveprefix
;
277 savedir
= vfs_path_tokens_get (real_filename_vpath
, 0, -1);
279 savedir
= g_strdup (".");
281 /* Token-related function never return leading slash, so we need add it manually */
282 saveprefix
= mc_build_filename (PATH_SEP_STR
, savedir
, "cooledit", NULL
);
284 fd
= mc_mkstemps (&savename_vpath
, saveprefix
, NULL
);
286 if (savename_vpath
== NULL
)
288 vfs_path_free (real_filename_vpath
);
292 * Close for now because mc_mkstemps use pure open system call
293 * to create temporary file and it needs to be reopened by
294 * VFS-aware mc_open().
299 savename_vpath
= vfs_path_clone (real_filename_vpath
);
301 (void) mc_chown (savename_vpath
, edit
->stat1
.st_uid
, edit
->stat1
.st_gid
);
302 (void) mc_chmod (savename_vpath
, edit
->stat1
.st_mode
);
304 fd
= mc_open (savename_vpath
, O_CREAT
| O_WRONLY
| O_TRUNC
| O_BINARY
, edit
->stat1
.st_mode
);
309 p
= edit_get_write_filter (savename_vpath
, real_filename_vpath
);
315 file
= (FILE *) popen (p
, "w");
319 filelen
= edit_write_stream (edit
, file
);
323 if (pclose (file
) != 0)
325 tmp
= g_strdup_printf (_("Error writing to pipe: %s"), p
);
326 edit_error_dialog (_("Error"), tmp
);
335 tmp
= g_strdup_printf (_("Cannot open pipe for writing: %s"), p
);
336 edit_error_dialog (_("Error"), get_sys_error (tmp
));
343 else if (edit
->lb
== LB_ASIS
)
344 { /* do not change line breaks */
345 filelen
= edit_buffer_write_file (&edit
->buffer
, fd
);
347 if (filelen
!= edit
->buffer
.size
)
352 if (mc_close (fd
) != 0)
354 /* Update the file information, especially the mtime. */
355 if (mc_stat (savename_vpath
, &edit
->stat1
) == -1)
359 { /* change line breaks */
361 const vfs_path_element_t
*path_element
;
365 path_element
= vfs_path_get_by_index (savename_vpath
, -1);
366 file
= (FILE *) fopen (path_element
->path
, "w");
369 filelen
= edit_write_stream (edit
, file
);
376 msg
= g_strdup_printf (_("Cannot open file for writing: %s"), path_element
->path
);
377 edit_error_dialog (_("Error"), msg
);
383 if (filelen
!= edit
->buffer
.size
)
386 if (this_save_mode
== EDIT_DO_BACKUP
)
388 char *tmp_store_filename
;
389 vfs_path_element_t
*last_vpath_element
;
390 vfs_path_t
*tmp_vpath
;
394 assert (option_backup_ext
!= NULL
);
396 /* add backup extension to the path */
397 tmp_vpath
= vfs_path_clone (real_filename_vpath
);
398 last_vpath_element
= (vfs_path_element_t
*) vfs_path_get_by_index (tmp_vpath
, -1);
399 tmp_store_filename
= last_vpath_element
->path
;
400 last_vpath_element
->path
= g_strdup_printf ("%s%s", tmp_store_filename
, option_backup_ext
);
401 g_free (tmp_store_filename
);
403 ok
= (mc_rename (real_filename_vpath
, tmp_vpath
) != -1);
404 vfs_path_free (tmp_vpath
);
409 if (this_save_mode
!= EDIT_QUICK_SAVE
)
410 if (mc_rename (savename_vpath
, real_filename_vpath
) == -1)
413 vfs_path_free (real_filename_vpath
);
414 vfs_path_free (savename_vpath
);
417 /* FIXME: Is this safe ?
418 * if (this_save_mode != EDIT_QUICK_SAVE)
419 * mc_unlink (savename);
421 vfs_path_free (real_filename_vpath
);
422 vfs_path_free (savename_vpath
);
426 /* --------------------------------------------------------------------------------------------- */
429 edit_check_newline (const edit_buffer_t
* buf
)
431 return !(option_check_nl_at_eof
&& buf
->size
> 0
432 && edit_buffer_get_byte (buf
, buf
->size
- 1) != '\n'
433 && edit_query_dialog2 (_("Warning"),
434 _("The file you are saving does not end with a newline."),
435 _("C&ontinue"), _("&Cancel")) != 0);
438 /* --------------------------------------------------------------------------------------------- */
441 edit_get_save_file_as (WEdit
* edit
)
443 static LineBreaks cur_lb
= LB_ASIS
;
445 vfs_path_t
*ret_vpath
= NULL
;
447 const char *lb_names
[LB_NAMES
] = {
448 N_("&Do not change"),
449 N_("&Unix format (LF)"),
450 N_("&Windows/DOS format (CR LF)"),
451 N_("&Macintosh format (CR)")
454 quick_widget_t quick_widgets
[] = {
456 QUICK_LABELED_INPUT (N_("Enter file name:"), input_label_above
,
457 vfs_path_as_str (edit
->filename_vpath
), "save-as",
458 &filename_res
, NULL
, FALSE
, FALSE
, INPUT_COMPLETE_FILENAMES
),
459 QUICK_SEPARATOR (TRUE
),
460 QUICK_LABEL (N_("Change line breaks to:"), NULL
),
461 QUICK_RADIO (LB_NAMES
, lb_names
, (int *) &cur_lb
, NULL
),
462 QUICK_BUTTONS_OK_CANCEL
,
467 quick_dialog_t qdlg
= {
469 N_("Save As"), "[Save File As]",
470 quick_widgets
, NULL
, NULL
473 if (quick_dialog (&qdlg
) != B_CANCEL
)
478 fname
= tilde_expand (filename_res
);
479 g_free (filename_res
);
480 ret_vpath
= vfs_path_from_str (fname
);
487 /* --------------------------------------------------------------------------------------------- */
489 /** returns TRUE on success */
492 edit_save_cmd (WEdit
* edit
)
494 int res
, save_lock
= 0;
496 if (!edit
->locked
&& !edit
->delete_file
)
497 save_lock
= lock_file (edit
->filename_vpath
);
498 res
= edit_save_file (edit
, edit
->filename_vpath
);
500 /* Maintain modify (not save) lock on failure */
501 if ((res
> 0 && edit
->locked
) || save_lock
)
502 edit
->locked
= unlock_file (edit
->filename_vpath
);
504 /* On failure try 'save as', it does locking on its own */
506 return edit_save_as_cmd (edit
);
507 edit
->force
|= REDRAW_COMPLETELY
;
510 edit
->delete_file
= 0;
517 /* --------------------------------------------------------------------------------------------- */
521 * @param h screen the owner of editor window
522 * @param vpath vfs file path
523 * @return TRUE if file content was successfully loaded, FALSE otherwise
526 static inline gboolean
527 edit_load_file_from_filename (WDialog
* h
, const vfs_path_t
* vpath
)
529 Widget
*w
= WIDGET (h
);
531 return edit_add_window (h
, w
->y
+ 1, w
->x
, w
->lines
- 2, w
->cols
, vpath
, 0);
534 /* --------------------------------------------------------------------------------------------- */
537 edit_delete_column_of_text (WEdit
* edit
)
544 eval_marks (edit
, &m1
, &m2
);
545 n
= edit_buffer_move_forward (&edit
->buffer
, m1
, 0, m2
) + 1;
546 c
= (long) edit_move_forward3 (edit
, edit_buffer_get_bol (&edit
->buffer
, m1
), 0, m1
);
547 d
= (long) edit_move_forward3 (edit
, edit_buffer_get_bol (&edit
->buffer
, m2
), 0, m2
);
548 b
= max (min (c
, d
), min (edit
->column1
, edit
->column2
));
549 c
= max (c
, max (edit
->column1
, edit
->column2
));
553 r
= edit_buffer_get_current_bol (&edit
->buffer
);
554 p
= edit_move_forward3 (edit
, r
, b
, 0);
555 q
= edit_move_forward3 (edit
, r
, c
, 0);
560 edit_cursor_move (edit
, p
- edit
->buffer
.curs1
);
563 /* delete line between margins */
564 if (edit_buffer_get_current_byte (&edit
->buffer
) != '\n')
565 edit_delete (edit
, TRUE
);
569 /* move to next line except on the last delete */
570 edit_cursor_move (edit
,
571 edit_buffer_move_forward (&edit
->buffer
, edit
->buffer
.curs1
, 1,
572 0) - edit
->buffer
.curs1
);
576 /* --------------------------------------------------------------------------------------------- */
577 /** if success return 0 */
580 edit_block_delete (WEdit
* edit
)
582 off_t start_mark
, end_mark
;
584 long curs_line
, c1
, c2
;
586 if (!eval_marks (edit
, &start_mark
, &end_mark
))
589 if (edit
->column_highlight
&& edit
->mark2
< 0)
590 edit_mark_cmd (edit
, FALSE
);
591 if ((end_mark
- start_mark
) > option_max_undo
/ 2)
593 /* Warning message with a query to continue or cancel the operation */
594 if (edit_query_dialog2
597 ("Block is large, you may not be able to undo this action"),
598 _("C&ontinue"), _("&Cancel")))
603 c1
= min (edit
->column1
, edit
->column2
);
604 c2
= max (edit
->column1
, edit
->column2
);
608 edit_push_markers (edit
);
610 curs_line
= edit
->buffer
.curs_line
;
612 curs_pos
= edit
->curs_col
+ edit
->over_col
;
614 /* move cursor to start of selection */
615 edit_cursor_move (edit
, start_mark
- edit
->buffer
.curs1
);
616 edit_scroll_screen_over_cursor (edit
);
618 if (start_mark
< end_mark
)
620 if (edit
->column_highlight
)
625 edit_mark_cmd (edit
, FALSE
);
626 edit_delete_column_of_text (edit
);
627 /* move cursor to the saved position */
628 edit_move_to_line (edit
, curs_line
);
629 /* calculate line width and cursor position before cut */
630 line_width
= edit_move_forward3 (edit
, edit_buffer_get_current_bol (&edit
->buffer
), 0,
631 edit_buffer_get_current_eol (&edit
->buffer
));
632 if (option_cursor_beyond_eol
&& curs_pos
> line_width
)
633 edit
->over_col
= curs_pos
- line_width
;
639 for (count
= start_mark
; count
< end_mark
; count
++)
640 edit_delete (edit
, TRUE
);
643 edit_set_markers (edit
, 0, 0, 0, 0);
644 edit
->force
|= REDRAW_PAGE
;
648 /* --------------------------------------------------------------------------------------------- */
650 * Get EOL symbol for searching.
652 * @param edit editor object
657 edit_search_get_current_end_line_char (const WEdit
* edit
)
668 /* --------------------------------------------------------------------------------------------- */
670 * Checking if search condition have BOL(^) or EOL ($) regexp special characters.
672 * @param search search object
673 * @return result of checks.
676 static edit_search_line_t
677 edit_get_search_line_type (mc_search_t
* search
)
679 edit_search_line_t search_line_type
= 0;
681 if (search
->search_type
!= MC_SEARCH_T_REGEX
)
682 return search_line_type
;
684 if (*search
->original
== '^')
685 search_line_type
|= AT_START_LINE
;
687 if (search
->original
[search
->original_len
- 1] == '$')
688 search_line_type
|= AT_END_LINE
;
689 return search_line_type
;
692 /* --------------------------------------------------------------------------------------------- */
694 * Calculating the start position of next line.
696 * @param buf editor buffer object
697 * @param current_pos current position
698 * @param max_pos max position
699 * @param end_string_symbol end of line symbol
700 * @return start position of next line
704 edit_calculate_start_of_next_line (const edit_buffer_t
* buf
, off_t current_pos
, off_t max_pos
,
705 char end_string_symbol
)
709 for (i
= current_pos
; i
< max_pos
; i
++)
712 if (edit_buffer_get_byte (buf
, i
) == end_string_symbol
)
719 /* --------------------------------------------------------------------------------------------- */
721 * Calculating the end position of previous line.
723 * @param buf editor buffer object
724 * @param current_pos current position
725 * @param end_string_symbol end of line symbol
726 * @return end position of previous line
730 edit_calculate_end_of_previous_line (const edit_buffer_t
* buf
, off_t current_pos
,
731 char end_string_symbol
)
735 for (i
= current_pos
- 1; i
>= 0; i
--)
736 if (edit_buffer_get_byte (buf
, i
) == end_string_symbol
)
742 /* --------------------------------------------------------------------------------------------- */
744 * Calculating the start position of previous line.
746 * @param buf editor buffer object
747 * @param current_pos current position
748 * @param end_string_symbol end of line symbol
749 * @return start position of previous line
753 edit_calculate_start_of_previous_line (const edit_buffer_t
* buf
, off_t current_pos
,
754 char end_string_symbol
)
756 current_pos
= edit_calculate_end_of_previous_line (buf
, current_pos
, end_string_symbol
);
757 current_pos
= edit_calculate_end_of_previous_line (buf
, current_pos
, end_string_symbol
);
759 return (current_pos
+ 1);
762 /* --------------------------------------------------------------------------------------------- */
764 * Calculating the start position of current line.
766 * @param buf editor buffer object
767 * @param current_pos current position
768 * @param end_string_symbol end of line symbol
769 * @return start position of current line
773 edit_calculate_start_of_current_line (const edit_buffer_t
* buf
, off_t current_pos
,
774 char end_string_symbol
)
776 current_pos
= edit_calculate_end_of_previous_line (buf
, current_pos
, end_string_symbol
);
778 return (current_pos
+ 1);
781 /* --------------------------------------------------------------------------------------------- */
783 * Fixing (if needed) search start position if 'only in selection' option present.
785 * @param edit editor object
789 edit_search_fix_search_start_if_selection (WEdit
* edit
)
791 off_t start_mark
= 0;
794 if (!edit_search_options
.only_in_selection
)
797 if (!eval_marks (edit
, &start_mark
, &end_mark
) != 0)
800 if (edit_search_options
.backwards
)
802 if (edit
->search_start
> end_mark
|| edit
->search_start
<= start_mark
)
803 edit
->search_start
= end_mark
;
807 if (edit
->search_start
< start_mark
|| edit
->search_start
>= end_mark
)
808 edit
->search_start
= start_mark
;
812 /* --------------------------------------------------------------------------------------------- */
815 editcmd_find (edit_search_status_msg_t
* esm
, gsize
* len
)
817 WEdit
*edit
= esm
->edit
;
818 off_t search_start
= edit
->search_start
;
820 off_t start_mark
= 0;
821 off_t end_mark
= edit
->buffer
.size
;
822 char end_string_symbol
;
824 end_string_symbol
= edit_search_get_current_end_line_char (edit
);
826 /* prepare for search */
827 if (edit_search_options
.only_in_selection
)
829 if (!eval_marks (edit
, &start_mark
, &end_mark
))
831 edit
->search
->error
= MC_SEARCH_E_NOTFOUND
;
832 edit
->search
->error_str
= g_strdup (_("Search string not found"));
836 /* fix the start and the end of search block positions */
837 if ((edit
->search_line_type
& AT_START_LINE
) != 0
839 || edit_buffer_get_byte (&edit
->buffer
, start_mark
- 1) != end_string_symbol
))
842 edit_calculate_start_of_next_line (&edit
->buffer
, start_mark
, edit
->buffer
.size
,
845 if ((edit
->search_line_type
& AT_END_LINE
) != 0
846 && (end_mark
- 1 != edit
->buffer
.size
847 || edit_buffer_get_byte (&edit
->buffer
, end_mark
) != end_string_symbol
))
849 edit_calculate_end_of_previous_line (&edit
->buffer
, end_mark
, end_string_symbol
);
850 if (start_mark
>= end_mark
)
852 edit
->search
->error
= MC_SEARCH_E_NOTFOUND
;
853 edit
->search
->error_str
= g_strdup (_("Search string not found"));
859 if (edit_search_options
.backwards
)
860 end_mark
= max (1, edit
->buffer
.curs1
) - 1;
864 if (edit_search_options
.backwards
)
866 /* backward search */
867 search_end
= end_mark
;
869 if ((edit
->search_line_type
& AT_START_LINE
) != 0)
871 edit_calculate_start_of_current_line (&edit
->buffer
, search_start
,
874 while (search_start
>= start_mark
)
876 if (search_end
> (off_t
) (search_start
+ edit
->search
->original_len
)
877 && mc_search_is_fixed_search_str (edit
->search
))
879 search_end
= search_start
+ edit
->search
->original_len
;
881 if (mc_search_run (edit
->search
, (void *) esm
, search_start
, search_end
, len
)
882 && edit
->search
->normal_offset
== search_start
)
888 if ((edit
->search_line_type
& AT_START_LINE
) != 0)
890 edit_calculate_start_of_previous_line (&edit
->buffer
, search_start
,
895 edit
->search
->error_str
= g_strdup (_("Search string not found"));
900 if ((edit
->search_line_type
& AT_START_LINE
) != 0 && search_start
!= start_mark
)
902 edit_calculate_start_of_next_line (&edit
->buffer
, search_start
, end_mark
,
904 return mc_search_run (edit
->search
, (void *) esm
, search_start
, end_mark
, len
);
909 /* --------------------------------------------------------------------------------------------- */
912 edit_replace_cmd__conv_to_display (char *str
)
917 tmp
= str_convert_to_display (str
);
921 return g_string_free (tmp
, FALSE
);
922 g_string_free (tmp
, TRUE
);
925 return g_strdup (str
);
928 /* --------------------------------------------------------------------------------------------- */
931 edit_replace_cmd__conv_to_input (char *str
)
936 tmp
= str_convert_to_input (str
);
940 return g_string_free (tmp
, FALSE
);
941 g_string_free (tmp
, TRUE
);
944 return g_strdup (str
);
947 /* --------------------------------------------------------------------------------------------- */
950 edit_do_search (WEdit
* edit
)
952 edit_search_status_msg_t esm
;
955 if (edit
->search
== NULL
)
956 edit
->search_start
= edit
->buffer
.curs1
;
958 edit_push_undo_action (edit
, KEY_PRESS
+ edit
->start_display
);
962 esm
.offset
= edit
->search_start
;
964 status_msg_init (STATUS_MSG (&esm
), _("Search"), 1.0, simple_status_msg_init_cb
,
965 edit_search_status_update_cb
, NULL
);
967 if (search_create_bookmark
)
969 int found
= 0, books
= 0;
970 long l
= 0, l_last
= -1;
973 search_create_bookmark
= FALSE
;
974 book_mark_flush (edit
, -1);
976 while (mc_search_run (edit
->search
, (void *) &esm
, q
, edit
->buffer
.size
, &len
))
979 edit
->search_start
= edit
->search
->normal_offset
;
981 l
+= edit_buffer_count_lines (&edit
->buffer
, q
, edit
->search
->normal_offset
);
984 book_mark_insert (edit
, l
, BOOK_MARK_FOUND_COLOR
);
988 q
= edit
->search
->normal_offset
+ 1;
992 edit_error_dialog (_("Search"), _("Search string not found"));
994 edit_cursor_move (edit
, edit
->search_start
- edit
->buffer
.curs1
);
998 if (edit
->found_len
!= 0 && edit
->search_start
== edit
->found_start
+ 1
999 && edit_search_options
.backwards
)
1000 edit
->search_start
--;
1002 if (edit
->found_len
!= 0 && edit
->search_start
== edit
->found_start
- 1
1003 && !edit_search_options
.backwards
)
1004 edit
->search_start
++;
1006 if (editcmd_find (&esm
, &len
))
1008 edit
->found_start
= edit
->search_start
= edit
->search
->normal_offset
;
1009 edit
->found_len
= len
;
1011 edit_cursor_move (edit
, edit
->search_start
- edit
->buffer
.curs1
);
1012 edit_scroll_screen_over_cursor (edit
);
1013 if (edit_search_options
.backwards
)
1014 edit
->search_start
--;
1016 edit
->search_start
++;
1020 edit
->search_start
= edit
->buffer
.curs1
;
1021 if (edit
->search
->error_str
!= NULL
)
1022 edit_query_dialog (_("Search"), edit
->search
->error_str
);
1026 status_msg_deinit (STATUS_MSG (&esm
));
1028 edit
->force
|= REDRAW_COMPLETELY
;
1029 edit_scroll_screen_over_cursor (edit
);
1032 /* --------------------------------------------------------------------------------------------- */
1035 edit_search (WEdit
* edit
)
1037 if (editcmd_dialog_search_show (edit
))
1039 edit
->search_line_type
= edit_get_search_line_type (edit
->search
);
1040 edit_search_fix_search_start_if_selection (edit
);
1041 edit_do_search (edit
);
1045 /* --------------------------------------------------------------------------------------------- */
1046 /** Return a null terminated length of text. Result must be g_free'd */
1048 static unsigned char *
1049 edit_get_block (WEdit
* edit
, off_t start
, off_t finish
, off_t
* l
)
1051 unsigned char *s
, *r
;
1053 r
= s
= g_malloc0 (finish
- start
+ 1);
1054 if (edit
->column_highlight
)
1057 /* copy from buffer, excluding chars that are out of the column 'margins' */
1058 while (start
< finish
)
1063 x
= edit_move_forward3 (edit
, edit_buffer_get_bol (&edit
->buffer
, start
), 0, start
);
1064 c
= edit_buffer_get_byte (&edit
->buffer
, start
);
1065 if ((x
>= edit
->column1
&& x
< edit
->column2
)
1066 || (x
>= edit
->column2
&& x
< edit
->column1
) || c
== '\n')
1076 *l
= finish
- start
;
1077 while (start
< finish
)
1078 *s
++ = edit_buffer_get_byte (&edit
->buffer
, start
++);
1084 /* --------------------------------------------------------------------------------------------- */
1085 /** copies a block to clipboard file */
1088 edit_save_block_to_clip_file (WEdit
* edit
, off_t start
, off_t finish
)
1093 tmp
= mc_config_get_full_path (EDIT_CLIP_FILE
);
1094 ret
= edit_save_block (edit
, tmp
, start
, finish
);
1099 /* --------------------------------------------------------------------------------------------- */
1102 pipe_mail (const edit_buffer_t
* buf
, char *to
, char *subject
, char *cc
)
1107 to
= name_quote (to
, FALSE
);
1108 subject
= name_quote (subject
, FALSE
);
1109 cc
= name_quote (cc
, FALSE
);
1110 s
= g_strconcat ("mail -s ", subject
, *cc
? " -c " : "", cc
, " ", to
, (char *) NULL
);
1125 for (i
= 0; i
< buf
->size
; i
++)
1126 fputc (edit_buffer_get_byte (buf
, i
), p
);
1131 /* --------------------------------------------------------------------------------------------- */
1132 /** find first character of current word */
1135 edit_find_word_start (const edit_buffer_t
* buf
, off_t
* word_start
, gsize
* word_len
)
1140 /* return if at begin of file */
1141 if (buf
->curs1
<= 0)
1144 c
= edit_buffer_get_previous_byte (buf
);
1145 /* return if not at end or in word */
1146 if (is_break_char (c
))
1149 /* search start of word to be completed */
1155 c
= edit_buffer_get_byte (buf
, buf
->curs1
- i
- 1);
1157 if (is_break_char (c
))
1159 /* return if word starts with digit */
1168 *word_start
= buf
->curs1
- i
; /* start found */
1169 *word_len
= (gsize
) i
;
1173 /* --------------------------------------------------------------------------------------------- */
1175 * Get current word under cursor
1177 * @param esm status message window
1178 * @param srch mc_search object
1179 * @param word_start start word position
1181 * @return newly allocated string or NULL if no any words under cursor
1185 edit_collect_completions_get_current_word (edit_search_status_msg_t
* esm
, mc_search_t
* srch
,
1188 WEdit
*edit
= esm
->edit
;
1193 if (!mc_search_run (srch
, (void *) esm
, word_start
, edit
->buffer
.size
, &len
))
1196 temp
= g_string_sized_new (len
);
1198 for (i
= 0; i
< (off_t
) len
; i
++)
1202 chr
= edit_buffer_get_byte (&edit
->buffer
, word_start
+ i
);
1204 g_string_append_c (temp
, chr
);
1207 return g_string_free (temp
, temp
->len
== 0);
1210 /* --------------------------------------------------------------------------------------------- */
1211 /** collect the possible completions */
1214 edit_collect_completions (WEdit
* edit
, off_t word_start
, gsize word_len
,
1215 char *match_expr
, GString
** compl, gsize
* num
)
1223 off_t last_byte
, start
= -1;
1225 gboolean entire_file
;
1226 edit_search_status_msg_t esm
;
1229 srch
= mc_search_new (match_expr
, -1, cp_source
);
1231 srch
= mc_search_new (match_expr
, -1, NULL
);
1237 mc_config_get_bool (mc_main_config
, CONFIG_APP_SECTION
,
1238 "editor_wordcompletion_collect_entire_file", 0);
1240 last_byte
= entire_file
? edit
->buffer
.size
: word_start
;
1242 srch
->search_type
= MC_SEARCH_T_REGEX
;
1243 srch
->is_case_sensitive
= TRUE
;
1244 srch
->search_fn
= edit_search_cmd_callback
;
1245 srch
->update_fn
= edit_search_update_callback
;
1249 esm
.offset
= entire_file
? 0 : word_start
;
1251 status_msg_init (STATUS_MSG (&esm
), _("Collect completions"), 1.0, simple_status_msg_init_cb
,
1252 edit_search_status_update_cb
, NULL
);
1254 current_word
= edit_collect_completions_get_current_word (&esm
, srch
, word_start
);
1256 temp
= g_string_new ("");
1258 /* collect max MAX_WORD_COMPLETIONS completions */
1259 while (mc_search_run (srch
, (void *) &esm
, start
+ 1, last_byte
, &len
))
1261 g_string_set_size (temp
, 0);
1262 start
= srch
->normal_offset
;
1264 /* add matched completion if not yet added */
1265 for (i
= 0; i
< len
; i
++)
1267 skip
= edit_buffer_get_byte (&edit
->buffer
, start
+ i
);
1271 /* skip current word */
1272 if (start
+ (off_t
) i
== word_start
)
1275 g_string_append_c (temp
, skip
);
1281 if (current_word
!= NULL
&& strcmp (current_word
, temp
->str
) == 0)
1286 for (i
= 0; i
< *num
; i
++)
1289 ((char *) &compl[i
]->str
[word_len
],
1290 (char *) &temp
->str
[word_len
], max (len
, compl[i
]->len
) - word_len
) == 0)
1292 GString
*this = compl[i
];
1293 for (++i
; i
< *num
; i
++)
1294 compl[i
- 1] = compl[i
];
1295 compl[*num
- 1] = this;
1297 break; /* skip it, already added */
1303 if (*num
== MAX_WORD_COMPLETIONS
)
1305 g_string_free (compl[0], TRUE
);
1306 for (i
= 1; i
< *num
; i
++)
1307 compl[i
- 1] = compl[i
];
1313 recoded
= str_convert_to_display (temp
->str
);
1315 if (recoded
&& recoded
->len
)
1316 g_string_assign (temp
, recoded
->str
);
1318 g_string_free (recoded
, TRUE
);
1321 compl[(*num
)++] = g_string_new_len (temp
->str
, temp
->len
);
1324 /* note the maximal length needed for the completion dialog */
1329 status_msg_deinit (STATUS_MSG (&esm
));
1330 mc_search_free (srch
);
1331 g_string_free (temp
, TRUE
);
1332 g_free (current_word
);
1337 /* --------------------------------------------------------------------------------------------- */
1340 edit_insert_column_of_text (WEdit
* edit
, unsigned char *data
, off_t size
, long width
,
1341 off_t
* start_pos
, off_t
* end_pos
, long *col1
, long *col2
)
1346 cursor
= edit
->buffer
.curs1
;
1347 col
= edit_get_col (edit
);
1349 for (i
= 0; i
< size
; i
++)
1351 if (data
[i
] != '\n')
1352 edit_insert (edit
, data
[i
]);
1354 { /* fill in and move to next line */
1358 if (edit_buffer_get_current_byte (&edit
->buffer
) != '\n')
1360 for (l
= width
- (edit_get_col (edit
) - col
); l
> 0; l
-= space_width
)
1361 edit_insert (edit
, ' ');
1363 for (p
= edit
->buffer
.curs1
;; p
++)
1365 if (p
== edit
->buffer
.size
)
1367 edit_cursor_move (edit
, edit
->buffer
.size
- edit
->buffer
.curs1
);
1368 edit_insert_ahead (edit
, '\n');
1372 if (edit_buffer_get_byte (&edit
->buffer
, p
) == '\n')
1378 edit_cursor_move (edit
, edit_move_forward3 (edit
, p
, col
, 0) - edit
->buffer
.curs1
);
1380 for (l
= col
- edit_get_col (edit
); l
>= space_width
; l
-= space_width
)
1381 edit_insert (edit
, ' ');
1386 *col2
= col
+ width
;
1387 *start_pos
= cursor
;
1388 *end_pos
= edit
->buffer
.curs1
;
1389 edit_cursor_move (edit
, cursor
- edit
->buffer
.curs1
);
1392 /* --------------------------------------------------------------------------------------------- */
1395 edit_macro_comparator (gconstpointer
* macro1
, gconstpointer
* macro2
)
1397 const macros_t
*m1
= (const macros_t
*) macro1
;
1398 const macros_t
*m2
= (const macros_t
*) macro2
;
1400 return m1
->hotkey
- m2
->hotkey
;
1403 /* --------------------------------------------------------------------------------------------- */
1406 edit_macro_sort_by_hotkey (void)
1408 if (macros_list
!= NULL
&& macros_list
->len
!= 0)
1409 g_array_sort (macros_list
, (GCompareFunc
) edit_macro_comparator
);
1412 /* --------------------------------------------------------------------------------------------- */
1415 edit_get_macro (WEdit
* edit
, int hotkey
, const macros_t
** macros
, guint
* indx
)
1417 const macros_t
*array_start
= &g_array_index (macros_list
, struct macros_t
, 0);
1419 macros_t search_macro
;
1423 search_macro
.hotkey
= hotkey
;
1424 result
= bsearch (&search_macro
, macros_list
->data
, macros_list
->len
,
1425 sizeof (macros_t
), (GCompareFunc
) edit_macro_comparator
);
1427 if (result
!= NULL
&& result
->macro
!= NULL
)
1429 *indx
= (result
- array_start
);
1437 /* --------------------------------------------------------------------------------------------- */
1438 /** returns FALSE on error */
1441 edit_delete_macro (WEdit
* edit
, int hotkey
)
1443 mc_config_t
*macros_config
= NULL
;
1444 const char *section_name
= "editor";
1445 gchar
*macros_fname
;
1448 const macros_t
*macros
= NULL
;
1450 /* clear array of actions for current hotkey */
1451 while (edit_get_macro (edit
, hotkey
, ¯os
, &indx
))
1453 if (macros
->macro
!= NULL
)
1454 g_array_free (macros
->macro
, TRUE
);
1456 g_array_remove_index (macros_list
, indx
);
1457 edit_macro_sort_by_hotkey ();
1460 macros_fname
= mc_config_get_full_path (MC_MACRO_FILE
);
1461 macros_config
= mc_config_init (macros_fname
, FALSE
);
1462 g_free (macros_fname
);
1464 if (macros_config
== NULL
)
1467 skeyname
= lookup_key_by_code (hotkey
);
1468 while (mc_config_del_key (macros_config
, section_name
, skeyname
))
1471 mc_config_save_file (macros_config
, NULL
);
1472 mc_config_deinit (macros_config
);
1476 /* --------------------------------------------------------------------------------------------- */
1478 * Callback for the iteration of objects in the 'editors' array.
1479 * Toggle syntax highlighting in editor object.
1481 * @param data probably WEdit object
1482 * @param user_data unused
1486 edit_syntax_onoff_cb (void *data
, void *user_data
)
1490 if (edit_widget_is_editor ((const Widget
*) data
))
1492 WEdit
*edit
= (WEdit
*) data
;
1494 if (option_syntax_highlighting
)
1495 edit_load_syntax (edit
, NULL
, edit
->syntax_type
);
1496 edit
->force
|= REDRAW_PAGE
;
1500 /* --------------------------------------------------------------------------------------------- */
1502 * Callback for the iteration of objects in the 'editors' array.
1503 * Redraw editor object.
1505 * @param data probably WEdit object
1506 * @param user_data unused
1510 edit_redraw_page_cb (void *data
, void *user_data
)
1514 if (edit_widget_is_editor ((const Widget
*) data
))
1515 ((WEdit
*) data
)->force
|= REDRAW_PAGE
;
1518 /* --------------------------------------------------------------------------------------------- */
1520 * Insert autocompleted word into editor.
1522 * @param edit editor object
1523 * @param completion word for completion
1524 * @param word_len offset from beginning for insert
1528 edit_complete_word_insert_recoded_completion (WEdit
* edit
, char *completion
, gsize word_len
)
1533 temp
= str_convert_to_input (completion
);
1535 for (completion
= temp
->str
+ word_len
; *completion
!= '\0'; completion
++)
1536 edit_insert (edit
, *completion
);
1537 g_string_free (temp
, TRUE
);
1539 for (completion
+= word_len
; *completion
!= '\0'; completion
++)
1540 edit_insert (edit
, *completion
);
1544 /* --------------------------------------------------------------------------------------------- */
1545 /*** public functions ****************************************************************************/
1546 /* --------------------------------------------------------------------------------------------- */
1549 edit_refresh_cmd (void)
1556 /* --------------------------------------------------------------------------------------------- */
1558 * Toggle syntax highlighting in all editor windows.
1560 * @param h root widget for all windows
1564 edit_syntax_onoff_cmd (WDialog
* h
)
1566 option_syntax_highlighting
= !option_syntax_highlighting
;
1567 g_list_foreach (h
->widgets
, edit_syntax_onoff_cb
, NULL
);
1571 /* --------------------------------------------------------------------------------------------- */
1573 * Toggle tabs showing in all editor windows.
1575 * @param h root widget for all windows
1579 edit_show_tabs_tws_cmd (WDialog
* h
)
1581 enable_show_tabs_tws
= !enable_show_tabs_tws
;
1582 g_list_foreach (h
->widgets
, edit_redraw_page_cb
, NULL
);
1586 /* --------------------------------------------------------------------------------------------- */
1588 * Toggle right margin showing in all editor windows.
1590 * @param h root widget for all windows
1594 edit_show_margin_cmd (WDialog
* h
)
1596 show_right_margin
= !show_right_margin
;
1597 g_list_foreach (h
->widgets
, edit_redraw_page_cb
, NULL
);
1601 /* --------------------------------------------------------------------------------------------- */
1603 * Toggle line numbers showing in all editor windows.
1605 * @param h root widget for all windows
1609 edit_show_numbers_cmd (WDialog
* h
)
1611 option_line_state
= !option_line_state
;
1612 option_line_state_width
= option_line_state
? LINE_STATE_WIDTH
: 0;
1613 g_list_foreach (h
->widgets
, edit_redraw_page_cb
, NULL
);
1617 /* --------------------------------------------------------------------------------------------- */
1620 edit_save_mode_cmd (void)
1622 char *str_result
= NULL
;
1624 const char *str
[] = {
1627 N_("&Do backups with following extension:")
1630 #ifdef HAVE_ASSERT_H
1631 assert (option_backup_ext
!= NULL
);
1637 for (i
= 0; i
< 3; i
++)
1642 quick_widget_t quick_widgets
[] = {
1644 QUICK_RADIO (3, str
, &option_save_mode
, &edit_save_mode_radio_id
),
1645 QUICK_INPUT (option_backup_ext
, "edit-backup-ext", &str_result
,
1646 &edit_save_mode_input_id
, FALSE
, FALSE
, INPUT_COMPLETE_NONE
),
1647 QUICK_SEPARATOR (TRUE
),
1648 QUICK_CHECKBOX (N_("Check &POSIX new line"), &option_check_nl_at_eof
, NULL
),
1649 QUICK_BUTTONS_OK_CANCEL
,
1654 quick_dialog_t qdlg
= {
1656 N_("Edit Save Mode"), "[Edit Save Mode]",
1657 quick_widgets
, edit_save_mode_callback
, NULL
1660 if (quick_dialog (&qdlg
) != B_CANCEL
)
1662 g_free (option_backup_ext
);
1663 option_backup_ext
= str_result
;
1668 /* --------------------------------------------------------------------------------------------- */
1671 edit_set_filename (WEdit
* edit
, const vfs_path_t
* name_vpath
)
1673 vfs_path_free (edit
->filename_vpath
);
1674 edit
->filename_vpath
= vfs_path_clone (name_vpath
);
1676 if (edit
->dir_vpath
== NULL
)
1677 edit
->dir_vpath
= vfs_path_clone (vfs_get_raw_current_dir ());
1680 /* --------------------------------------------------------------------------------------------- */
1681 /* Here we want to warn the users of overwriting an existing file,
1682 but only if they have made a change to the filename */
1683 /* returns TRUE on success */
1685 edit_save_as_cmd (WEdit
* edit
)
1687 /* This heads the 'Save As' dialog box */
1688 vfs_path_t
*exp_vpath
;
1690 int different_filename
= 0;
1692 if (!edit_check_newline (&edit
->buffer
))
1695 exp_vpath
= edit_get_save_file_as (edit
);
1696 edit_push_undo_action (edit
, KEY_PRESS
+ edit
->start_display
);
1698 if (exp_vpath
!= NULL
)
1700 if (vfs_path_len (exp_vpath
) == 0)
1706 if (!vfs_path_equal (edit
->filename_vpath
, exp_vpath
))
1711 if (mc_stat (exp_vpath
, &sb
) == 0 && !S_ISREG (sb
.st_mode
))
1713 edit_error_dialog (_("Save as"),
1715 ("Cannot save: destination is not a regular file")));
1719 different_filename
= 1;
1720 file
= mc_open (exp_vpath
, O_RDONLY
| O_BINARY
);
1724 /* the file exists */
1726 /* Overwrite the current file or cancel the operation */
1727 if (edit_query_dialog2
1729 _("A file already exists with this name"), _("&Overwrite"), _("&Cancel")))
1734 edit
->stat1
.st_mode
|= S_IWUSR
;
1736 save_lock
= lock_file (exp_vpath
);
1740 /* filenames equal, check if already locked */
1741 if (!edit
->locked
&& !edit
->delete_file
)
1742 save_lock
= lock_file (exp_vpath
);
1745 if (different_filename
)
1748 * Allow user to write into saved (under another name) file
1749 * even if original file had r/o user permissions.
1751 edit
->stat1
.st_mode
|= S_IWRITE
;
1754 rv
= edit_save_file (edit
, exp_vpath
);
1758 /* Successful, so unlock both files */
1759 if (different_filename
)
1762 unlock_file (exp_vpath
);
1764 edit
->locked
= unlock_file (edit
->filename_vpath
);
1768 if (edit
->locked
|| save_lock
)
1769 edit
->locked
= unlock_file (edit
->filename_vpath
);
1772 edit_set_filename (edit
, exp_vpath
);
1773 if (edit
->lb
!= LB_ASIS
)
1774 edit_reload (edit
, exp_vpath
);
1776 edit
->delete_file
= 0;
1777 if (different_filename
)
1778 edit_load_syntax (edit
, NULL
, edit
->syntax_type
);
1779 vfs_path_free (exp_vpath
);
1780 edit
->force
|= REDRAW_COMPLETELY
;
1783 edit_error_dialog (_("Save as"), get_sys_error (_("Cannot save file")));
1786 /* Failed, so maintain modify (not save) lock */
1788 unlock_file (exp_vpath
);
1795 vfs_path_free (exp_vpath
);
1796 edit
->force
|= REDRAW_COMPLETELY
;
1800 /* {{{ Macro stuff starts here */
1801 /* --------------------------------------------------------------------------------------------- */
1804 edit_delete_macro_cmd (WEdit
* edit
)
1808 hotkey
= editcmd_dialog_raw_key_query (_("Delete macro"), _("Press macro hotkey:"), TRUE
);
1810 if (hotkey
!= 0 && !edit_delete_macro (edit
, hotkey
))
1811 message (D_ERROR
, _("Delete macro"), _("Macro not deleted"));
1814 /* --------------------------------------------------------------------------------------------- */
1816 /** returns FALSE on error */
1818 edit_execute_macro (WEdit
* edit
, int hotkey
)
1820 gboolean res
= FALSE
;
1824 const macros_t
*macros
;
1827 if (edit_get_macro (edit
, hotkey
, ¯os
, &indx
) &&
1828 macros
->macro
!= NULL
&& macros
->macro
->len
!= 0)
1832 edit
->force
|= REDRAW_PAGE
;
1834 for (i
= 0; i
< macros
->macro
->len
; i
++)
1836 const macro_action_t
*m_act
;
1838 m_act
= &g_array_index (macros
->macro
, struct macro_action_t
, i
);
1839 edit_execute_cmd (edit
, m_act
->action
, m_act
->ch
);
1848 /* --------------------------------------------------------------------------------------------- */
1850 /** returns FALSE on error */
1852 edit_store_macro_cmd (WEdit
* edit
)
1856 GString
*marcros_string
;
1857 mc_config_t
*macros_config
= NULL
;
1858 const char *section_name
= "editor";
1859 gchar
*macros_fname
;
1860 GArray
*macros
; /* current macro */
1862 gboolean have_macro
= FALSE
;
1863 char *skeyname
= NULL
;
1866 editcmd_dialog_raw_key_query (_("Save macro"), _("Press the macro's new hotkey:"), TRUE
);
1867 if (hotkey
== ESC_CHAR
)
1870 tmp_act
= keybind_lookup_keymap_command (editor_map
, hotkey
);
1872 /* return FALSE if try assign macro into restricted hotkeys */
1873 if (tmp_act
== CK_MacroStartRecord
1874 || tmp_act
== CK_MacroStopRecord
|| tmp_act
== CK_MacroStartStopRecord
)
1877 edit_delete_macro (edit
, hotkey
);
1879 macros_fname
= mc_config_get_full_path (MC_MACRO_FILE
);
1880 macros_config
= mc_config_init (macros_fname
, FALSE
);
1881 g_free (macros_fname
);
1883 if (macros_config
== NULL
)
1886 edit_push_undo_action (edit
, KEY_PRESS
+ edit
->start_display
);
1888 marcros_string
= g_string_sized_new (250);
1889 macros
= g_array_new (TRUE
, FALSE
, sizeof (macro_action_t
));
1891 skeyname
= lookup_key_by_code (hotkey
);
1893 for (i
= 0; i
< macro_index
; i
++)
1895 macro_action_t m_act
;
1896 const char *action_name
;
1898 action_name
= keybind_lookup_actionname (record_macro_buf
[i
].action
);
1900 if (action_name
== NULL
)
1903 m_act
.action
= record_macro_buf
[i
].action
;
1904 m_act
.ch
= record_macro_buf
[i
].ch
;
1905 g_array_append_val (macros
, m_act
);
1907 g_string_append_printf (marcros_string
, "%s:%i;", action_name
,
1908 (int) record_macro_buf
[i
].ch
);
1913 macro
.hotkey
= hotkey
;
1914 macro
.macro
= macros
;
1915 g_array_append_val (macros_list
, macro
);
1916 mc_config_set_string (macros_config
, section_name
, skeyname
, marcros_string
->str
);
1919 mc_config_del_key (macros_config
, section_name
, skeyname
);
1922 edit_macro_sort_by_hotkey ();
1924 g_string_free (marcros_string
, TRUE
);
1925 mc_config_save_file (macros_config
, NULL
);
1926 mc_config_deinit (macros_config
);
1930 /* --------------------------------------------------------------------------------------------- */
1933 edit_repeat_macro_cmd (WEdit
* edit
)
1940 f
= input_dialog (_("Repeat last commands"), _("Repeat times:"), MC_HISTORY_EDIT_REPEAT
, NULL
,
1941 INPUT_COMPLETE_NONE
);
1942 if (f
== NULL
|| *f
== '\0')
1948 count_repeat
= strtol (f
, &error
, 0);
1958 edit_push_undo_action (edit
, KEY_PRESS
+ edit
->start_display
);
1959 edit
->force
|= REDRAW_PAGE
;
1961 for (j
= 0; j
< count_repeat
; j
++)
1962 for (i
= 0; i
< macro_index
; i
++)
1963 edit_execute_cmd (edit
, record_macro_buf
[i
].action
, record_macro_buf
[i
].ch
);
1964 edit_update_screen (edit
);
1968 /* --------------------------------------------------------------------------------------------- */
1969 /** return FALSE on error */
1972 edit_load_macro_cmd (WEdit
* edit
)
1974 mc_config_t
*macros_config
= NULL
;
1975 gchar
**profile_keys
, **keys
;
1976 gchar
**values
, **curr_values
;
1977 const char *section_name
= "editor";
1978 gchar
*macros_fname
;
1982 if (macros_list
== NULL
|| macros_list
->len
!= 0)
1985 macros_fname
= mc_config_get_full_path (MC_MACRO_FILE
);
1986 macros_config
= mc_config_init (macros_fname
, TRUE
);
1987 g_free (macros_fname
);
1989 if (macros_config
== NULL
)
1992 keys
= mc_config_get_keys (macros_config
, section_name
, NULL
);
1994 for (profile_keys
= keys
; *profile_keys
!= NULL
; profile_keys
++)
1997 gboolean have_macro
= FALSE
;
2001 macros
= g_array_new (TRUE
, FALSE
, sizeof (macro_action_t
));
2002 values
= mc_config_get_string_list (macros_config
, section_name
, *profile_keys
, NULL
);
2003 hotkey
= lookup_key (*profile_keys
, NULL
);
2005 for (curr_values
= values
; *curr_values
!= NULL
&& *curr_values
[0] != '\0'; curr_values
++)
2007 char **macro_pair
= NULL
;
2009 macro_pair
= g_strsplit (*curr_values
, ":", 2);
2010 if (macro_pair
!= NULL
)
2012 macro_action_t m_act
;
2013 if (macro_pair
[0] == NULL
|| macro_pair
[0][0] == '\0')
2017 m_act
.action
= keybind_lookup_action (macro_pair
[0]);
2018 MC_PTR_FREE (macro_pair
[0]);
2020 if (macro_pair
[1] == NULL
|| macro_pair
[1][0] == '\0')
2024 m_act
.ch
= strtol (macro_pair
[1], NULL
, 0);
2025 MC_PTR_FREE (macro_pair
[1]);
2027 if (m_act
.action
!= 0)
2029 /* a shell command */
2030 if ((m_act
.action
/ CK_PipeBlock (0)) == 1)
2032 m_act
.action
= CK_PipeBlock (0) + (m_act
.ch
> 0 ? m_act
.ch
: 0);
2035 g_array_append_val (macros
, m_act
);
2038 g_strfreev (macro_pair
);
2044 macro
.hotkey
= hotkey
;
2045 macro
.macro
= macros
;
2046 g_array_append_val (macros_list
, macro
);
2048 g_strfreev (values
);
2051 mc_config_deinit (macros_config
);
2052 edit_macro_sort_by_hotkey ();
2056 /* }}} Macro stuff end here */
2058 /* --------------------------------------------------------------------------------------------- */
2059 /** returns TRUE on success */
2062 edit_save_confirm_cmd (WEdit
* edit
)
2064 if (edit
->filename_vpath
== NULL
)
2065 return edit_save_as_cmd (edit
);
2067 if (!edit_check_newline (&edit
->buffer
))
2070 if (edit_confirm_save
)
2075 f
= g_strdup_printf (_("Confirm save file: \"%s\""),
2076 vfs_path_as_str (edit
->filename_vpath
));
2077 ok
= (edit_query_dialog2 (_("Save file"), f
, _("&Save"), _("&Cancel")) == 0);
2082 return edit_save_cmd (edit
);
2085 /* --------------------------------------------------------------------------------------------- */
2087 * Ask file to edit and load it.
2089 * @return TRUE on success or cancel of ask.
2093 edit_load_cmd (WDialog
* h
)
2096 gboolean ret
= TRUE
; /* possible cancel */
2098 exp
= input_expand_dialog (_("Load"), _("Enter file name:"),
2099 MC_HISTORY_EDIT_LOAD
, INPUT_LAST_TEXT
,
2100 INPUT_COMPLETE_FILENAMES
| INPUT_COMPLETE_CD
);
2102 if (exp
!= NULL
&& *exp
!= '\0')
2104 vfs_path_t
*exp_vpath
;
2106 exp_vpath
= vfs_path_from_str (exp
);
2107 ret
= edit_load_file_from_filename (h
, exp_vpath
);
2108 vfs_path_free (exp_vpath
);
2116 /* --------------------------------------------------------------------------------------------- */
2118 * Load syntax file to edit.
2120 * @return TRUE on success
2124 edit_load_syntax_file (WDialog
* h
)
2126 vfs_path_t
*extdir_vpath
;
2128 gboolean ret
= FALSE
;
2130 if (geteuid () == 0)
2131 dir
= query_dialog (_("Syntax file edit"),
2132 _("Which syntax file you want to edit?"), D_NORMAL
, 2,
2133 _("&User"), _("&System wide"));
2136 vfs_path_build_filename (mc_global
.sysconfig_dir
, "syntax", "Syntax", (char *) NULL
);
2137 if (!exist_file (vfs_path_get_last_path_str (extdir_vpath
)))
2139 vfs_path_free (extdir_vpath
);
2141 vfs_path_build_filename (mc_global
.share_data_dir
, "syntax", "Syntax", (char *) NULL
);
2146 vfs_path_t
*user_syntax_file_vpath
;
2148 user_syntax_file_vpath
= mc_config_get_full_vpath (EDIT_SYNTAX_FILE
);
2149 check_for_default (extdir_vpath
, user_syntax_file_vpath
);
2150 ret
= edit_load_file_from_filename (h
, user_syntax_file_vpath
);
2151 vfs_path_free (user_syntax_file_vpath
);
2154 ret
= edit_load_file_from_filename (h
, extdir_vpath
);
2156 vfs_path_free (extdir_vpath
);
2161 /* --------------------------------------------------------------------------------------------- */
2163 * Load menu file to edit.
2165 * @return TRUE on success
2169 edit_load_menu_file (WDialog
* h
)
2171 vfs_path_t
*buffer_vpath
;
2172 vfs_path_t
*menufile_vpath
;
2177 dir
= query_dialog (_("Menu edit"),
2178 _("Which menu file do you want to edit?"), D_NORMAL
,
2179 geteuid () != 0 ? 2 : 3, _("&Local"), _("&User"), _("&System wide"));
2181 menufile_vpath
= vfs_path_build_filename (mc_global
.sysconfig_dir
, EDIT_GLOBAL_MENU
, NULL
);
2182 if (!exist_file (vfs_path_get_last_path_str (menufile_vpath
)))
2184 vfs_path_free (menufile_vpath
);
2185 menufile_vpath
= vfs_path_build_filename (mc_global
.share_data_dir
, EDIT_GLOBAL_MENU
, NULL
);
2191 buffer_vpath
= vfs_path_from_str (EDIT_LOCAL_MENU
);
2192 check_for_default (menufile_vpath
, buffer_vpath
);
2193 chmod (vfs_path_get_last_path_str (buffer_vpath
), 0600);
2197 buffer_vpath
= mc_config_get_full_vpath (EDIT_HOME_MENU
);
2198 check_for_default (menufile_vpath
, buffer_vpath
);
2202 buffer_vpath
= vfs_path_build_filename (mc_global
.sysconfig_dir
, EDIT_GLOBAL_MENU
, NULL
);
2203 if (!exist_file (vfs_path_get_last_path_str (buffer_vpath
)))
2205 vfs_path_free (buffer_vpath
);
2207 vfs_path_build_filename (mc_global
.share_data_dir
, EDIT_GLOBAL_MENU
, NULL
);
2212 vfs_path_free (menufile_vpath
);
2216 ret
= edit_load_file_from_filename (h
, buffer_vpath
);
2218 vfs_path_free (buffer_vpath
);
2219 vfs_path_free (menufile_vpath
);
2224 /* --------------------------------------------------------------------------------------------- */
2226 * Close window with opened file.
2228 * @return TRUE if file was closed.
2232 edit_close_cmd (WEdit
* edit
)
2236 ret
= (edit
!= NULL
) && edit_ok_to_exit (edit
);
2240 WDialog
*h
= WIDGET (edit
)->owner
;
2242 if (edit
->locked
!= 0)
2243 unlock_file (edit
->filename_vpath
);
2247 if (edit_widget_is_editor (WIDGET (h
->current
->data
)))
2248 edit
= (WEdit
*) h
->current
->data
;
2251 edit
= find_editor (h
);
2253 dlg_set_top_widget (edit
);
2258 edit
->force
|= REDRAW_COMPLETELY
;
2263 /* --------------------------------------------------------------------------------------------- */
2265 if mark2 is -1 then marking is from mark1 to the cursor.
2266 Otherwise its between the markers. This handles this.
2267 Returns FALSE if no text is marked.
2271 eval_marks (WEdit
* edit
, off_t
* start_mark
, off_t
* end_mark
)
2275 if (edit
->mark1
== edit
->mark2
)
2277 *start_mark
= *end_mark
= 0;
2278 edit
->column2
= edit
->column1
= 0;
2282 if (edit
->end_mark_curs
< 0)
2283 end_mark_curs
= edit
->buffer
.curs1
;
2285 end_mark_curs
= edit
->end_mark_curs
;
2287 if (edit
->mark2
>= 0)
2289 *start_mark
= min (edit
->mark1
, edit
->mark2
);
2290 *end_mark
= max (edit
->mark1
, edit
->mark2
);
2294 *start_mark
= min (edit
->mark1
, end_mark_curs
);
2295 *end_mark
= max (edit
->mark1
, end_mark_curs
);
2296 edit
->column2
= edit
->curs_col
+ edit
->over_col
;
2299 if (edit
->column_highlight
2300 && ((edit
->mark1
> end_mark_curs
&& edit
->column1
< edit
->column2
)
2301 || (edit
->mark1
< end_mark_curs
&& edit
->column1
> edit
->column2
)))
2303 off_t start_bol
, start_eol
;
2304 off_t end_bol
, end_eol
;
2308 start_bol
= edit_buffer_get_bol (&edit
->buffer
, *start_mark
);
2309 start_eol
= edit_buffer_get_eol (&edit
->buffer
, start_bol
- 1) + 1;
2310 end_bol
= edit_buffer_get_bol (&edit
->buffer
, *end_mark
);
2311 end_eol
= edit_buffer_get_eol (&edit
->buffer
, *end_mark
);
2312 col1
= min (edit
->column1
, edit
->column2
);
2313 col2
= max (edit
->column1
, edit
->column2
);
2315 diff1
= edit_move_forward3 (edit
, start_bol
, col2
, 0) -
2316 edit_move_forward3 (edit
, start_bol
, col1
, 0);
2317 diff2
= edit_move_forward3 (edit
, end_bol
, col2
, 0) -
2318 edit_move_forward3 (edit
, end_bol
, col1
, 0);
2320 *start_mark
-= diff1
;
2322 *start_mark
= max (*start_mark
, start_eol
);
2323 *end_mark
= min (*end_mark
, end_eol
);
2328 /* --------------------------------------------------------------------------------------------- */
2331 edit_block_copy_cmd (WEdit
* edit
)
2333 off_t start_mark
, end_mark
, current
= edit
->buffer
.curs1
;
2337 unsigned char *copy_buf
;
2339 edit_update_curs_col (edit
);
2340 if (!eval_marks (edit
, &start_mark
, &end_mark
))
2343 copy_buf
= edit_get_block (edit
, start_mark
, end_mark
, &size
);
2345 /* all that gets pushed are deletes hence little space is used on the stack */
2347 edit_push_markers (edit
);
2349 if (edit
->column_highlight
)
2353 col_delta
= labs (edit
->column2
- edit
->column1
);
2354 edit_insert_column_of_text (edit
, copy_buf
, size
, col_delta
, &mark1
, &mark2
, &c1
, &c2
);
2358 int size_orig
= size
;
2361 edit_insert_ahead (edit
, copy_buf
[size
]);
2363 /* Place cursor at the end of text selection */
2364 if (option_cursor_after_inserted_block
)
2365 edit_cursor_move (edit
, size_orig
);
2369 edit_scroll_screen_over_cursor (edit
);
2371 if (edit
->column_highlight
)
2372 edit_set_markers (edit
, edit
->buffer
.curs1
, mark2
, c1
, c2
);
2373 else if (start_mark
< current
&& end_mark
> current
)
2374 edit_set_markers (edit
, start_mark
, end_mark
+ end_mark
- start_mark
, 0, 0);
2376 edit
->force
|= REDRAW_PAGE
;
2380 /* --------------------------------------------------------------------------------------------- */
2383 edit_block_move_cmd (WEdit
* edit
)
2386 unsigned char *copy_buf
= NULL
;
2387 off_t start_mark
, end_mark
;
2389 if (!eval_marks (edit
, &start_mark
, &end_mark
))
2392 if (!edit
->column_highlight
&& edit
->buffer
.curs1
> start_mark
&& edit
->buffer
.curs1
< end_mark
)
2395 if (edit
->mark2
< 0)
2396 edit_mark_cmd (edit
, FALSE
);
2397 edit_push_markers (edit
);
2399 if (edit
->column_highlight
)
2403 long c1
, c2
, b_width
;
2406 c1
= min (edit
->column1
, edit
->column2
);
2407 c2
= max (edit
->column1
, edit
->column2
);
2410 edit_update_curs_col (edit
);
2413 x2
= x
+ edit
->over_col
;
2415 /* do nothing when cursor inside first line of selected area */
2416 if ((edit_buffer_get_eol (&edit
->buffer
, edit
->buffer
.curs1
) ==
2417 edit_buffer_get_eol (&edit
->buffer
, start_mark
)) && x2
> c1
&& x2
<= c2
)
2420 if (edit
->buffer
.curs1
> start_mark
2421 && edit
->buffer
.curs1
< edit_buffer_get_eol (&edit
->buffer
, end_mark
))
2425 else if (x
> c1
&& x
<= c2
)
2428 /* save current selection into buffer */
2429 copy_buf
= edit_get_block (edit
, start_mark
, end_mark
, &size
);
2431 /* remove current selection */
2432 edit_block_delete_cmd (edit
);
2434 edit
->over_col
= max (0, edit
->over_col
- b_width
);
2435 /* calculate the cursor pos after delete block */
2436 current
= edit_move_forward3 (edit
, edit_buffer_get_current_bol (&edit
->buffer
), x
, 0);
2437 edit_cursor_move (edit
, current
- edit
->buffer
.curs1
);
2438 edit_scroll_screen_over_cursor (edit
);
2440 /* add TWS if need before block insertion */
2441 if (option_cursor_beyond_eol
&& edit
->over_col
> 0)
2442 edit_insert_over (edit
);
2444 edit_insert_column_of_text (edit
, copy_buf
, size
, b_width
, &mark1
, &mark2
, &c1
, &c2
);
2445 edit_set_markers (edit
, mark1
, mark2
, c1
, c2
);
2449 off_t count
, count_orig
;
2451 current
= edit
->buffer
.curs1
;
2452 copy_buf
= g_malloc0 (end_mark
- start_mark
);
2453 edit_cursor_move (edit
, start_mark
- edit
->buffer
.curs1
);
2454 edit_scroll_screen_over_cursor (edit
);
2456 for (count
= start_mark
; count
< end_mark
; count
++)
2457 copy_buf
[end_mark
- count
- 1] = edit_delete (edit
, TRUE
);
2459 edit_scroll_screen_over_cursor (edit
);
2460 edit_cursor_move (edit
,
2461 current
- edit
->buffer
.curs1
-
2462 (((current
- edit
->buffer
.curs1
) > 0) ? end_mark
- start_mark
: 0));
2463 edit_scroll_screen_over_cursor (edit
);
2465 while (count
-- > start_mark
)
2466 edit_insert_ahead (edit
, copy_buf
[end_mark
- count
- 1]);
2468 edit_set_markers (edit
, edit
->buffer
.curs1
, edit
->buffer
.curs1
+ end_mark
- start_mark
, 0,
2471 /* Place cursor at the end of text selection */
2472 if (option_cursor_after_inserted_block
)
2473 edit_cursor_move (edit
, count_orig
- start_mark
);
2476 edit_scroll_screen_over_cursor (edit
);
2478 edit
->force
|= REDRAW_PAGE
;
2481 /* --------------------------------------------------------------------------------------------- */
2482 /** returns 1 if canceelled by user */
2485 edit_block_delete_cmd (WEdit
* edit
)
2487 off_t start_mark
, end_mark
;
2489 if (eval_marks (edit
, &start_mark
, &end_mark
))
2490 return edit_block_delete (edit
);
2492 edit_delete_line (edit
);
2496 /* --------------------------------------------------------------------------------------------- */
2497 /** call with edit = 0 before shutdown to close memory leaks */
2500 edit_replace_cmd (WEdit
* edit
, int again
)
2502 /* 1 = search string, 2 = replace with */
2503 static char *saved1
= NULL
; /* saved default[123] */
2504 static char *saved2
= NULL
;
2505 char *input1
= NULL
; /* user input from the dialog */
2506 char *input2
= NULL
;
2507 GString
*input2_str
= NULL
;
2510 long times_replaced
= 0;
2511 gboolean once_found
= FALSE
;
2512 edit_search_status_msg_t esm
;
2516 MC_PTR_FREE (saved1
);
2517 MC_PTR_FREE (saved2
);
2521 edit
->force
|= REDRAW_COMPLETELY
;
2523 if (again
&& !saved1
&& !saved2
)
2528 input1
= g_strdup (saved1
? saved1
: "");
2529 input2
= g_strdup (saved2
? saved2
: "");
2533 char *tmp_inp1
, *tmp_inp2
;
2535 disp1
= edit_replace_cmd__conv_to_display (saved1
? saved1
: (char *) "");
2536 disp2
= edit_replace_cmd__conv_to_display (saved2
? saved2
: (char *) "");
2538 edit_push_undo_action (edit
, KEY_PRESS
+ edit
->start_display
);
2540 editcmd_dialog_replace_show (edit
, disp1
, disp2
, &input1
, &input2
);
2545 if (input1
== NULL
|| *input1
== '\0')
2547 edit
->force
= REDRAW_COMPLETELY
;
2553 input1
= edit_replace_cmd__conv_to_input (input1
);
2554 input2
= edit_replace_cmd__conv_to_input (input2
);
2558 g_free (saved1
), saved1
= g_strdup (input1
);
2559 g_free (saved2
), saved2
= g_strdup (input2
);
2561 mc_search_free (edit
->search
);
2562 edit
->search
= NULL
;
2565 input2_str
= g_string_new (input2
);
2567 if (edit
->search
== NULL
)
2570 edit
->search
= mc_search_new (input1
, -1, cp_source
);
2572 edit
->search
= mc_search_new (input1
, -1, NULL
);
2574 if (edit
->search
== NULL
)
2576 edit
->search_start
= edit
->buffer
.curs1
;
2579 edit
->search
->search_type
= edit_search_options
.type
;
2581 edit
->search
->is_all_charsets
= edit_search_options
.all_codepages
;
2583 edit
->search
->is_case_sensitive
= edit_search_options
.case_sens
;
2584 edit
->search
->whole_words
= edit_search_options
.whole_words
;
2585 edit
->search
->search_fn
= edit_search_cmd_callback
;
2586 edit
->search
->update_fn
= edit_search_update_callback
;
2587 edit
->search_line_type
= edit_get_search_line_type (edit
->search
);
2588 edit_search_fix_search_start_if_selection (edit
);
2591 if (edit
->found_len
&& edit
->search_start
== edit
->found_start
+ 1
2592 && edit_search_options
.backwards
)
2593 edit
->search_start
--;
2595 if (edit
->found_len
&& edit
->search_start
== edit
->found_start
- 1
2596 && !edit_search_options
.backwards
)
2597 edit
->search_start
++;
2601 esm
.offset
= edit
->search_start
;
2603 status_msg_init (STATUS_MSG (&esm
), _("Search"), 1.0, simple_status_msg_init_cb
,
2604 edit_search_status_update_cb
, NULL
);
2610 if (!editcmd_find (&esm
, &len
))
2612 if (!(edit
->search
->error
== MC_SEARCH_E_OK
||
2613 (once_found
&& edit
->search
->error
== MC_SEARCH_E_NOTFOUND
)))
2615 edit_query_dialog (_("Search"), edit
->search
->error_str
);
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_error_dialog (_("Replace"), edit
->search
->error_str
);
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"), _("Search string not found"), 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
= ((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
, -1, cp_source
);
2794 edit
->search
= mc_search_new (edit
->last_search_string
, -1, 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
);
3177 if (e
== -1 || e
== 127)
3178 edit_error_dialog (_("Sort"), get_sys_error (_("Cannot execute sort command")));
3183 sprintf (q
, "%d ", e
);
3184 tmp
= g_strdup_printf (_("Sort returned non-zero: %s"), q
);
3185 edit_error_dialog (_("Sort"), tmp
);
3191 edit
->force
|= REDRAW_COMPLETELY
;
3193 if (edit_block_delete_cmd (edit
))
3197 vfs_path_t
*tmp_vpath
;
3199 tmp_vpath
= mc_config_get_full_vpath (EDIT_TEMP_FILE
);
3200 edit_insert_file (edit
, tmp_vpath
);
3201 vfs_path_free (tmp_vpath
);
3206 /* --------------------------------------------------------------------------------------------- */
3208 * Ask user for a command, execute it and paste its output back to the
3213 edit_ext_cmd (WEdit
* edit
)
3215 char *exp
, *tmp
, *tmp_edit_temp_file
;
3219 input_dialog (_("Paste output of external command"),
3220 _("Enter shell command(s):"), MC_HISTORY_EDIT_PASTE_EXTCMD
, INPUT_LAST_TEXT
,
3221 INPUT_COMPLETE_FILENAMES
| INPUT_COMPLETE_VARIABLES
| INPUT_COMPLETE_USERNAMES
3222 | INPUT_COMPLETE_HOSTNAMES
| INPUT_COMPLETE_CD
| INPUT_COMPLETE_COMMANDS
|
3223 INPUT_COMPLETE_SHELL_ESC
);
3228 tmp_edit_temp_file
= mc_config_get_full_path (EDIT_TEMP_FILE
);
3229 tmp
= g_strconcat (exp
, " > ", tmp_edit_temp_file
, (char *) NULL
);
3230 g_free (tmp_edit_temp_file
);
3237 edit_error_dialog (_("External command"), get_sys_error (_("Cannot execute command")));
3241 edit
->force
|= REDRAW_COMPLETELY
;
3244 vfs_path_t
*tmp_vpath
;
3246 tmp_vpath
= mc_config_get_full_vpath (EDIT_TEMP_FILE
);
3247 edit_insert_file (edit
, tmp_vpath
);
3248 vfs_path_free (tmp_vpath
);
3253 /* --------------------------------------------------------------------------------------------- */
3254 /** if block is 1, a block must be highlighted and the shell command
3255 processes it. If block is 0 the shell command is a straight system
3256 command, that just produces some output which is to be inserted */
3259 edit_block_process_cmd (WEdit
* edit
, int macro_number
)
3262 char *macros_fname
= NULL
;
3264 fname
= g_strdup_printf ("%s.%i.sh", MC_EXTMACRO_FILE
, macro_number
);
3265 macros_fname
= g_build_filename (mc_config_get_data_path (), fname
, (char *) NULL
);
3266 user_menu (edit
, macros_fname
, 0);
3268 g_free (macros_fname
);
3269 edit
->force
|= REDRAW_COMPLETELY
;
3272 /* --------------------------------------------------------------------------------------------- */
3275 edit_mail_dialog (WEdit
* edit
)
3277 char *mail_to
, *mail_subject
, *mail_cc
;
3279 quick_widget_t quick_widgets
[] = {
3281 QUICK_LABEL (N_("mail -s <subject> -c <cc> <to>"), NULL
),
3282 QUICK_LABELED_INPUT (N_("To"), input_label_above
,
3283 INPUT_LAST_TEXT
, "mail-dlg-input-3",
3284 &mail_to
, NULL
, FALSE
, FALSE
, INPUT_COMPLETE_USERNAMES
),
3285 QUICK_LABELED_INPUT (N_("Subject"), input_label_above
,
3286 INPUT_LAST_TEXT
, "mail-dlg-input-2",
3287 &mail_subject
, NULL
, FALSE
, FALSE
, INPUT_COMPLETE_NONE
),
3288 QUICK_LABELED_INPUT (N_("Copies to"), input_label_above
,
3289 INPUT_LAST_TEXT
, "mail-dlg-input",
3290 &mail_cc
, NULL
, FALSE
, FALSE
, INPUT_COMPLETE_USERNAMES
),
3291 QUICK_BUTTONS_OK_CANCEL
,
3296 quick_dialog_t qdlg
= {
3298 N_("Mail"), "[Input Line Keys]",
3299 quick_widgets
, NULL
, NULL
3302 if (quick_dialog (&qdlg
) != B_CANCEL
)
3304 pipe_mail (&edit
->buffer
, mail_to
, mail_subject
, mail_cc
);
3306 g_free (mail_subject
);
3311 /* --------------------------------------------------------------------------------------------- */
3313 /*******************/
3314 /* Word Completion */
3315 /*******************/
3318 * Complete current word using regular expression search
3319 * backwards beginning at the current cursor position.
3323 edit_complete_word_cmd (WEdit
* edit
)
3325 gsize i
, max_len
, word_len
= 0, num_compl
= 0;
3326 off_t word_start
= 0;
3327 GString
*match_expr
;
3328 GString
*compl[MAX_WORD_COMPLETIONS
]; /* completions */
3330 /* search start of word to be completed */
3331 if (!edit_find_word_start (&edit
->buffer
, &word_start
, &word_len
))
3334 /* prepare match expression */
3335 /* match_expr = g_strdup_printf ("\\b%.*s[a-zA-Z_0-9]+", word_len, bufpos); */
3336 match_expr
= g_string_new ("(^|\\s+|\\b)");
3337 for (i
= 0; i
< word_len
; i
++)
3338 g_string_append_c (match_expr
, edit_buffer_get_byte (&edit
->buffer
, word_start
+ i
));
3339 g_string_append (match_expr
,
3340 "[^\\s\\.=\\+\\[\\]\\(\\)\\,\\;\\:\\\"\\'\\-\\?\\/\\|\\\\\\{\\}\\*\\&\\^\\%%\\$#@\\!]+");
3342 /* collect the possible completions */
3343 /* start search from begin to end of file */
3345 edit_collect_completions (edit
, word_start
, word_len
, match_expr
->str
, (GString
**) & compl,
3350 /* insert completed word if there is only one match */
3352 edit_complete_word_insert_recoded_completion (edit
, compl[0]->str
, word_len
);
3353 /* more than one possible completion => ask the user */
3358 /* !!! usually only a beep is expected and when <ALT-TAB> is !!! */
3359 /* !!! pressed again the selection dialog pops up, but that !!! */
3360 /* !!! seems to require a further internal state !!! */
3363 /* let the user select the preferred completion */
3364 curr_compl
= editcmd_dialog_completion_show (edit
, max_len
,
3365 (GString
**) & compl, num_compl
);
3367 if (curr_compl
!= NULL
)
3369 edit_complete_word_insert_recoded_completion (edit
, curr_compl
, word_len
);
3370 g_free (curr_compl
);
3375 g_string_free (match_expr
, TRUE
);
3376 /* release memory before return */
3377 for (i
= 0; i
< num_compl
; i
++)
3378 g_string_free (compl[i
], TRUE
);
3381 /* --------------------------------------------------------------------------------------------- */
3385 edit_select_codepage_cmd (WEdit
* edit
)
3387 if (do_select_codepage ())
3388 edit_set_codeset (edit
);
3390 edit
->force
= REDRAW_PAGE
;
3391 widget_redraw (WIDGET (edit
));
3395 /* --------------------------------------------------------------------------------------------- */
3398 edit_insert_literal_cmd (WEdit
* edit
)
3400 int char_for_insertion
;
3402 char_for_insertion
= editcmd_dialog_raw_key_query (_("Insert literal"),
3403 _("Press any key:"), FALSE
);
3404 edit_execute_key_command (edit
, -1, ascii_alpha_to_cntrl (char_for_insertion
));
3407 /* --------------------------------------------------------------------------------------------- */
3410 edit_begin_end_macro_cmd (WEdit
* edit
)
3412 /* edit is a pointer to the widget */
3415 unsigned long command
= macro_index
< 0 ? CK_MacroStartRecord
: CK_MacroStopRecord
;
3416 edit_execute_key_command (edit
, command
, -1);
3420 /* --------------------------------------------------------------------------------------------- */
3423 edit_begin_end_repeat_cmd (WEdit
* edit
)
3425 /* edit is a pointer to the widget */
3428 unsigned long command
= macro_index
< 0 ? CK_RepeatStartRecord
: CK_RepeatStopRecord
;
3429 edit_execute_key_command (edit
, command
, -1);
3433 /* --------------------------------------------------------------------------------------------- */
3436 edit_load_forward_cmd (WEdit
* edit
)
3439 && edit_query_dialog2 (_("Warning"),
3440 _("Current text was modified without a file save.\n"
3441 "Continue discards these changes"), _("C&ontinue"),
3444 edit
->force
|= REDRAW_COMPLETELY
;
3448 if (edit_stack_iterator
+ 1 >= MAX_HISTORY_MOVETO
)
3451 if (edit_history_moveto
[edit_stack_iterator
+ 1].line
< 1)
3454 edit_stack_iterator
++;
3455 if (edit_history_moveto
[edit_stack_iterator
].filename_vpath
!= NULL
)
3456 return edit_reload_line (edit
, edit_history_moveto
[edit_stack_iterator
].filename_vpath
,
3457 edit_history_moveto
[edit_stack_iterator
].line
);
3462 /* --------------------------------------------------------------------------------------------- */
3465 edit_load_back_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 /* we are in the bottom of the stack, NO WAY! */
3478 if (edit_stack_iterator
== 0)
3481 edit_stack_iterator
--;
3482 if (edit_history_moveto
[edit_stack_iterator
].filename_vpath
!= NULL
)
3483 return edit_reload_line (edit
, edit_history_moveto
[edit_stack_iterator
].filename_vpath
,
3484 edit_history_moveto
[edit_stack_iterator
].line
);
3489 /* --------------------------------------------------------------------------------------------- */
3492 edit_get_match_keyword_cmd (WEdit
* edit
)
3494 gsize word_len
= 0, max_len
= 0;
3497 off_t word_start
= 0;
3498 GString
*match_expr
;
3501 char *tagfile
= NULL
;
3503 etags_hash_t def_hash
[MAX_DEFINITIONS
];
3505 for (i
= 0; i
< MAX_DEFINITIONS
; i
++)
3507 def_hash
[i
].filename
= NULL
;
3510 /* search start of word to be completed */
3511 if (!edit_find_word_start (&edit
->buffer
, &word_start
, &word_len
))
3514 /* prepare match expression */
3515 match_expr
= g_string_sized_new (word_len
);
3516 for (i
= 0; i
< word_len
; i
++)
3517 g_string_append_c (match_expr
, edit_buffer_get_byte (&edit
->buffer
, word_start
+ i
));
3519 ptr
= g_get_current_dir ();
3520 path
= g_strconcat (ptr
, PATH_SEP_STR
, (char *) NULL
);
3523 /* Recursive search file 'TAGS' in parent dirs */
3526 ptr
= g_path_get_dirname (path
);
3530 tagfile
= mc_build_filename (path
, TAGS_NAME
, (char *) NULL
);
3531 if (exist_file (tagfile
))
3534 while (strcmp (path
, PATH_SEP_STR
) != 0);
3539 etags_set_definition_hash (tagfile
, path
, match_expr
->str
, (etags_hash_t
*) & def_hash
);
3544 max_len
= MAX_WIDTH_DEF_DIALOG
;
3548 editcmd_dialog_select_definition_show (edit
, match_expr
->str
, max_len
, word_len
,
3549 (etags_hash_t
*) & def_hash
, num_def
);
3551 g_string_free (match_expr
, TRUE
);
3554 /* --------------------------------------------------------------------------------------------- */
3558 edit_suggest_current_word (WEdit
* edit
)
3562 off_t word_start
= 0;
3563 int retval
= B_SKIP_WORD
;
3564 GString
*match_word
;
3566 /* search start of word to spell check */
3567 match_word
= edit_buffer_get_word_from_pos (&edit
->buffer
, edit
->buffer
.curs1
, &word_start
,
3569 word_len
= match_word
->len
;
3572 if (mc_global
.source_codepage
>= 0 && (mc_global
.source_codepage
!= mc_global
.display_codepage
))
3576 tmp_word
= str_convert_to_display (match_word
->str
);
3577 g_string_free (match_word
, TRUE
);
3578 match_word
= tmp_word
;
3581 if (!aspell_check (match_word
->str
, (int) word_len
))
3587 suggest
= g_array_new (TRUE
, FALSE
, sizeof (char *));
3589 res
= aspell_suggest (suggest
, match_word
->str
, (int) word_len
);
3592 char *new_word
= NULL
;
3594 edit
->found_start
= word_start
;
3595 edit
->found_len
= word_len
;
3596 edit
->force
|= REDRAW_PAGE
;
3597 edit_scroll_screen_over_cursor (edit
);
3598 edit_render_keypress (edit
);
3600 retval
= spell_dialog_spell_suggest_show (edit
, match_word
->str
, &new_word
, suggest
);
3601 edit_cursor_move (edit
, word_len
- cut_len
);
3603 if (retval
== B_ENTER
&& new_word
!= NULL
)
3608 if (mc_global
.source_codepage
>= 0 &&
3609 (mc_global
.source_codepage
!= mc_global
.display_codepage
))
3613 tmp_word
= str_convert_to_input (new_word
);
3615 new_word
= g_string_free (tmp_word
, FALSE
);
3619 for (i
= 0; i
< word_len
; i
++)
3620 edit_backspace (edit
, TRUE
);
3621 for (; *new_word
; new_word
++)
3622 edit_insert (edit
, *new_word
);
3625 else if (retval
== B_ADD_WORD
&& match_word
!= NULL
)
3626 aspell_add_to_dict (match_word
->str
, (int) word_len
);
3630 for (i
= 0; i
< suggest
->len
; i
++)
3632 char *cur_sugg_word
;
3634 cur_sugg_word
= g_array_index (suggest
, char *, i
);
3635 g_free (cur_sugg_word
);
3637 g_array_free (suggest
, TRUE
);
3638 edit
->found_start
= 0;
3639 edit
->found_len
= 0;
3641 g_string_free (match_word
, TRUE
);
3645 /* --------------------------------------------------------------------------------------------- */
3648 edit_spellcheck_file (WEdit
* edit
)
3650 if (edit
->buffer
.curs_line
> 0)
3652 edit_cursor_move (edit
, -edit
->buffer
.curs1
);
3653 edit_move_to_prev_col (edit
, 0);
3654 edit_update_curs_row (edit
);
3661 c2
= edit_buffer_get_current_byte (&edit
->buffer
);
3665 if (edit
->buffer
.curs1
>= edit
->buffer
.size
)
3669 edit_cursor_move (edit
, 1);
3670 c2
= edit_buffer_get_current_byte (&edit
->buffer
);
3672 while (is_break_char (c1
) || is_break_char (c2
));
3674 while (edit_suggest_current_word (edit
) != B_CANCEL
);
3677 /* --------------------------------------------------------------------------------------------- */
3680 edit_set_spell_lang (void)
3684 lang_list
= g_array_new (TRUE
, FALSE
, sizeof (char *));
3685 if (aspell_get_lang_list (lang_list
) != 0)
3689 lang
= spell_dialog_lang_list_show (lang_list
);
3691 (void) aspell_set_lang (lang
);
3693 aspell_array_clean (lang_list
);
3695 #endif /* HAVE_ASPELL */
3697 /* --------------------------------------------------------------------------------------------- */